MDL-56001 lib: Upgrade simplepie to 1.4.2
[moodle.git] / lib / simplepie / library / SimplePie / Locator.php
1 <?php
2 /**
3  * SimplePie
4  *
5  * A PHP-Based RSS and Atom Feed Framework.
6  * Takes the hard work out of managing a complete RSS/Atom solution.
7  *
8  * Copyright (c) 2004-2016, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification, are
12  * permitted provided that the following conditions are met:
13  *
14  *      * Redistributions of source code must retain the above copyright notice, this list of
15  *        conditions and the following disclaimer.
16  *
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.
20  *
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
23  *        written permission.
24  *
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.
34  *
35  * @package SimplePie
36  * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
37  * @author Ryan Parman
38  * @author Geoffrey Sneddon
39  * @author Ryan McCue
40  * @link http://simplepie.org/ SimplePie
41  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42  */
44 /**
45  * Used for feed auto-discovery
46  *
47  *
48  * This class can be overloaded with {@see SimplePie::set_locator_class()}
49  *
50  * @package SimplePie
51  */
52 class SimplePie_Locator
53 {
54         var $useragent;
55         var $timeout;
56         var $file;
57         var $local = array();
58         var $elsewhere = array();
59         var $cached_entities = array();
60         var $http_base;
61         var $base;
62         var $base_location = 0;
63         var $checked_feeds = 0;
64         var $max_checked_feeds = 10;
65         protected $registry;
67         public function __construct(SimplePie_File $file, $timeout = 10, $useragent = null, $max_checked_feeds = 10)
68         {
69                 $this->file = $file;
70                 $this->useragent = $useragent;
71                 $this->timeout = $timeout;
72                 $this->max_checked_feeds = $max_checked_feeds;
74                 if (class_exists('DOMDocument'))
75                 {
76                         $this->dom = new DOMDocument();
78                         set_error_handler(array('SimplePie_Misc', 'silence_errors'));
79                         $this->dom->loadHTML($this->file->body);
80                         restore_error_handler();
81                 }
82                 else
83                 {
84                         $this->dom = null;
85                 }
86         }
88         public function set_registry(SimplePie_Registry $registry)
89         {
90                 $this->registry = $registry;
91         }
93         public function find($type = SIMPLEPIE_LOCATOR_ALL, &$working)
94         {
95                 if ($this->is_feed($this->file))
96                 {
97                         return $this->file;
98                 }
100                 if ($this->file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
101                 {
102                         $sniffer = $this->registry->create('Content_Type_Sniffer', array($this->file));
103                         if ($sniffer->get_type() !== 'text/html')
104                         {
105                                 return null;
106                         }
107                 }
109                 if ($type & ~SIMPLEPIE_LOCATOR_NONE)
110                 {
111                         $this->get_base();
112                 }
114                 if ($type & SIMPLEPIE_LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery())
115                 {
116                         return $working[0];
117                 }
119                 if ($type & (SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY | SIMPLEPIE_LOCATOR_REMOTE_EXTENSION | SIMPLEPIE_LOCATOR_REMOTE_BODY) && $this->get_links())
120                 {
121                         if ($type & SIMPLEPIE_LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local))
122                         {
123                                 return $working[0];
124                         }
126                         if ($type & SIMPLEPIE_LOCATOR_LOCAL_BODY && $working = $this->body($this->local))
127                         {
128                                 return $working[0];
129                         }
131                         if ($type & SIMPLEPIE_LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere))
132                         {
133                                 return $working[0];
134                         }
136                         if ($type & SIMPLEPIE_LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere))
137                         {
138                                 return $working[0];
139                         }
140                 }
141                 return null;
142         }
144         public function is_feed($file, $check_html = false)
145         {
146                 if ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
147                 {
148                         $sniffer = $this->registry->create('Content_Type_Sniffer', array($file));
149                         $sniffed = $sniffer->get_type();
150                         $mime_types = array('application/rss+xml', 'application/rdf+xml',
151                                             'text/rdf', 'application/atom+xml', 'text/xml',
152                                             'application/xml', 'application/x-rss+xml');
153                         if ($check_html)
154                         {
155                                 $mime_types[] = 'text/html';
156                         }
157                         if (in_array($sniffed, $mime_types))
158                         {
159                                 return true;
160                         }
161                         else
162                         {
163                                 return false;
164                         }
165                 }
166                 elseif ($file->method & SIMPLEPIE_FILE_SOURCE_LOCAL)
167                 {
168                         return true;
169                 }
170                 else
171                 {
172                         return false;
173                 }
174         }
176         public function get_base()
177         {
178                 if ($this->dom === null)
179                 {
180                         throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
181                 }
182                 $this->http_base = $this->file->url;
183                 $this->base = $this->http_base;
184                 $elements = $this->dom->getElementsByTagName('base');
185                 foreach ($elements as $element)
186                 {
187                         if ($element->hasAttribute('href'))
188                         {
189                                 $base = $this->registry->call('Misc', 'absolutize_url', array(trim($element->getAttribute('href')), $this->http_base));
190                                 if ($base === false)
191                                 {
192                                         continue;
193                                 }
194                                 $this->base = $base;
195                                 $this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0;
196                                 break;
197                         }
198                 }
199         }
201         public function autodiscovery()
202         {
203                 $done = array();
204                 $feeds = array();
205                 $feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds));
206                 $feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds));
207                 $feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds));
209                 if (!empty($feeds))
210                 {
211                         return array_values($feeds);
212                 }
213                 else
214                 {
215                         return null;
216                 }
217         }
219         protected function search_elements_by_tag($name, &$done, $feeds)
220         {
221                 if ($this->dom === null)
222                 {
223                         throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
224                 }
226                 $links = $this->dom->getElementsByTagName($name);
227                 foreach ($links as $link)
228                 {
229                         if ($this->checked_feeds === $this->max_checked_feeds)
230                         {
231                                 break;
232                         }
233                         if ($link->hasAttribute('href') && $link->hasAttribute('rel'))
234                         {
235                                 $rel = array_unique($this->registry->call('Misc', 'space_seperated_tokens', array(strtolower($link->getAttribute('rel')))));
236                                 $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1;
238                                 if ($this->base_location < $line)
239                                 {
240                                         $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
241                                 }
242                                 else
243                                 {
244                                         $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
245                                 }
246                                 if ($href === false)
247                                 {
248                                         continue;
249                                 }
251                                 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('text/html', 'application/rss+xml', 'application/atom+xml'))) && !isset($feeds[$href]))
252                                 {
253                                         $this->checked_feeds++;
254                                         $headers = array(
255                                                 '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',
256                                         );
257                                         $feed = $this->registry->create('File', array($href, $this->timeout, 5, $headers, $this->useragent));
258                                         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, true))
259                                         {
260                                                 $feeds[$href] = $feed;
261                                         }
262                                 }
263                                 $done[] = $href;
264                         }
265                 }
267                 return $feeds;
268         }
270         public function get_links()
271         {
272                 if ($this->dom === null)
273                 {
274                         throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
275                 }
277                 $links = $this->dom->getElementsByTagName('a');
278                 foreach ($links as $link)
279                 {
280                         if ($link->hasAttribute('href'))
281                         {
282                                 $href = trim($link->getAttribute('href'));
283                                 $parsed = $this->registry->call('Misc', 'parse_url', array($href));
284                                 if ($parsed['scheme'] === '' || preg_match('/^(https?|feed)?$/i', $parsed['scheme']))
285                                 {
286                                         if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo())
287                                         {
288                                                 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
289                                         }
290                                         else
291                                         {
292                                                 $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
293                                         }
294                                         if ($href === false)
295                                         {
296                                                 continue;
297                                         }
299                                         $current = $this->registry->call('Misc', 'parse_url', array($this->file->url));
301                                         if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority'])
302                                         {
303                                                 $this->local[] = $href;
304                                         }
305                                         else
306                                         {
307                                                 $this->elsewhere[] = $href;
308                                         }
309                                 }
310                         }
311                 }
312                 $this->local = array_unique($this->local);
313                 $this->elsewhere = array_unique($this->elsewhere);
314                 if (!empty($this->local) || !empty($this->elsewhere))
315                 {
316                         return true;
317                 }
318                 return null;
319         }
321         public function get_rel_link($rel)
322         {
323                 if ($this->dom === null)
324                 {
325                         throw new SimplePie_Exception('DOMDocument not found, unable to use '.
326                                                       'locator');
327                 }
328                 if (!class_exists('DOMXpath'))
329                 {
330                         throw new SimplePie_Exception('DOMXpath not found, unable to use '.
331                                                       'get_rel_link');
332                 }
334                 $xpath = new DOMXpath($this->dom);
335                 $query = '//a[@rel and @href] | //link[@rel and @href]';
336                 foreach ($xpath->query($query) as $link)
337                 {
338                         $href = trim($link->getAttribute('href'));
339                         $parsed = $this->registry->call('Misc', 'parse_url', array($href));
340                         if ($parsed['scheme'] === '' ||
341                             preg_match('/^https?$/i', $parsed['scheme']))
342                         {
343                                 if (method_exists($link, 'getLineNo') &&
344                                     $this->base_location < $link->getLineNo())
345                                 {
346                                         $href =
347                                                 $this->registry->call('Misc', 'absolutize_url',
348                                                                       array(trim($link->getAttribute('href')),
349                                                                             $this->base));
350                                 }
351                                 else
352                                 {
353                                         $href =
354                                                 $this->registry->call('Misc', 'absolutize_url',
355                                                                       array(trim($link->getAttribute('href')),
356                                                                             $this->http_base));
357                                 }
358                                 if ($href === false)
359                                 {
360                                         return null;
361                                 }
362                                 $rel_values = explode(' ', strtolower($link->getAttribute('rel')));
363                                 if (in_array($rel, $rel_values))
364                                 {
365                                         return $href;
366                                 }
367                         }
368                 }
369                 return null;
370         }
372         public function extension(&$array)
373         {
374                 foreach ($array as $key => $value)
375                 {
376                         if ($this->checked_feeds === $this->max_checked_feeds)
377                         {
378                                 break;
379                         }
380                         if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
381                         {
382                                 $this->checked_feeds++;
384                                 $headers = array(
385                                         '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',
386                                 );
387                                 $feed = $this->registry->create('File', array($value, $this->timeout, 5, $headers, $this->useragent));
388                                 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))
389                                 {
390                                         return array($feed);
391                                 }
392                                 else
393                                 {
394                                         unset($array[$key]);
395                                 }
396                         }
397                 }
398                 return null;
399         }
401         public function body(&$array)
402         {
403                 foreach ($array as $key => $value)
404                 {
405                         if ($this->checked_feeds === $this->max_checked_feeds)
406                         {
407                                 break;
408                         }
409                         if (preg_match('/(rss|rdf|atom|xml)/i', $value))
410                         {
411                                 $this->checked_feeds++;
412                                 $headers = array(
413                                         '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',
414                                 );
415                                 $feed = $this->registry->create('File', array($value, $this->timeout, 5, null, $this->useragent));
416                                 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))
417                                 {
418                                         return array($feed);
419                                 }
420                                 else
421                                 {
422                                         unset($array[$key]);
423                                 }
424                         }
425                 }
426                 return null;
427         }