Locator.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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, Sam 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, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Used for feed auto-discovery
  45. *
  46. *
  47. * This class can be overloaded with {@see SimplePie::set_locator_class()}
  48. *
  49. * @package SimplePie
  50. */
  51. class SimplePie_Locator
  52. {
  53. var $useragent;
  54. var $timeout;
  55. var $file;
  56. var $local = array();
  57. var $elsewhere = array();
  58. var $cached_entities = array();
  59. var $http_base;
  60. var $base;
  61. var $base_location = 0;
  62. var $checked_feeds = 0;
  63. var $max_checked_feeds = 10;
  64. var $force_fsockopen = false;
  65. var $curl_options = array();
  66. protected $registry;
  67. public function __construct(SimplePie_File $file, $timeout = 10, $useragent = null, $max_checked_feeds = 10, $force_fsockopen = false, $curl_options = array())
  68. {
  69. $this->file = $file;
  70. $this->useragent = $useragent;
  71. $this->timeout = $timeout;
  72. $this->max_checked_feeds = $max_checked_feeds;
  73. $this->force_fsockopen = $force_fsockopen;
  74. $this->curl_options = $curl_options;
  75. if (class_exists('DOMDocument') && $this->file->body != '')
  76. {
  77. $this->dom = new DOMDocument();
  78. set_error_handler(array('SimplePie_Misc', 'silence_errors'));
  79. try
  80. {
  81. $this->dom->loadHTML($this->file->body);
  82. }
  83. catch (Throwable $ex)
  84. {
  85. $this->dom = null;
  86. }
  87. restore_error_handler();
  88. }
  89. else
  90. {
  91. $this->dom = null;
  92. }
  93. }
  94. public function set_registry(SimplePie_Registry $registry)
  95. {
  96. $this->registry = $registry;
  97. }
  98. public function find($type = SIMPLEPIE_LOCATOR_ALL, &$working = null)
  99. {
  100. if ($this->is_feed($this->file))
  101. {
  102. return $this->file;
  103. }
  104. if ($this->file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  105. {
  106. $sniffer = $this->registry->create('Content_Type_Sniffer', array($this->file));
  107. if ($sniffer->get_type() !== 'text/html')
  108. {
  109. return null;
  110. }
  111. }
  112. if ($type & ~SIMPLEPIE_LOCATOR_NONE)
  113. {
  114. $this->get_base();
  115. }
  116. if ($type & SIMPLEPIE_LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery())
  117. {
  118. return $working[0];
  119. }
  120. if ($type & (SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY | SIMPLEPIE_LOCATOR_REMOTE_EXTENSION | SIMPLEPIE_LOCATOR_REMOTE_BODY) && $this->get_links())
  121. {
  122. if ($type & SIMPLEPIE_LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local))
  123. {
  124. return $working[0];
  125. }
  126. if ($type & SIMPLEPIE_LOCATOR_LOCAL_BODY && $working = $this->body($this->local))
  127. {
  128. return $working[0];
  129. }
  130. if ($type & SIMPLEPIE_LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere))
  131. {
  132. return $working[0];
  133. }
  134. if ($type & SIMPLEPIE_LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere))
  135. {
  136. return $working[0];
  137. }
  138. }
  139. return null;
  140. }
  141. public function is_feed($file, $check_html = false)
  142. {
  143. if ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  144. {
  145. $sniffer = $this->registry->create('Content_Type_Sniffer', array($file));
  146. $sniffed = $sniffer->get_type();
  147. $mime_types = array('application/rss+xml', 'application/rdf+xml',
  148. 'text/rdf', 'application/atom+xml', 'text/xml',
  149. 'application/xml', 'application/x-rss+xml');
  150. if ($check_html)
  151. {
  152. $mime_types[] = 'text/html';
  153. }
  154. return in_array($sniffed, $mime_types);
  155. }
  156. elseif ($file->method & SIMPLEPIE_FILE_SOURCE_LOCAL)
  157. {
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. public function get_base()
  166. {
  167. if ($this->dom === null)
  168. {
  169. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  170. }
  171. $this->http_base = $this->file->url;
  172. $this->base = $this->http_base;
  173. $elements = $this->dom->getElementsByTagName('base');
  174. foreach ($elements as $element)
  175. {
  176. if ($element->hasAttribute('href'))
  177. {
  178. $base = $this->registry->call('Misc', 'absolutize_url', array(trim($element->getAttribute('href')), $this->http_base));
  179. if ($base === false)
  180. {
  181. continue;
  182. }
  183. $this->base = $base;
  184. $this->base_location = method_exists($element, 'getLineNo') ? $element->getLineNo() : 0;
  185. break;
  186. }
  187. }
  188. }
  189. public function autodiscovery()
  190. {
  191. $done = array();
  192. $feeds = array();
  193. $feeds = array_merge($feeds, $this->search_elements_by_tag('link', $done, $feeds));
  194. $feeds = array_merge($feeds, $this->search_elements_by_tag('a', $done, $feeds));
  195. $feeds = array_merge($feeds, $this->search_elements_by_tag('area', $done, $feeds));
  196. if (!empty($feeds))
  197. {
  198. return array_values($feeds);
  199. }
  200. return null;
  201. }
  202. protected function search_elements_by_tag($name, &$done, $feeds)
  203. {
  204. if ($this->dom === null)
  205. {
  206. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  207. }
  208. $links = $this->dom->getElementsByTagName($name);
  209. foreach ($links as $link)
  210. {
  211. if ($this->checked_feeds === $this->max_checked_feeds)
  212. {
  213. break;
  214. }
  215. if ($link->hasAttribute('href') && $link->hasAttribute('rel'))
  216. {
  217. $rel = array_unique($this->registry->call('Misc', 'space_separated_tokens', array(strtolower($link->getAttribute('rel')))));
  218. $line = method_exists($link, 'getLineNo') ? $link->getLineNo() : 1;
  219. if ($this->base_location < $line)
  220. {
  221. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
  222. }
  223. else
  224. {
  225. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
  226. }
  227. if ($href === false)
  228. {
  229. continue;
  230. }
  231. 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]))
  232. {
  233. $this->checked_feeds++;
  234. $headers = array(
  235. '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',
  236. );
  237. $feed = $this->registry->create('File', array($href, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  238. 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))
  239. {
  240. $feeds[$href] = $feed;
  241. }
  242. }
  243. $done[] = $href;
  244. }
  245. }
  246. return $feeds;
  247. }
  248. public function get_links()
  249. {
  250. if ($this->dom === null)
  251. {
  252. throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
  253. }
  254. $links = $this->dom->getElementsByTagName('a');
  255. foreach ($links as $link)
  256. {
  257. if ($link->hasAttribute('href'))
  258. {
  259. $href = trim($link->getAttribute('href'));
  260. $parsed = $this->registry->call('Misc', 'parse_url', array($href));
  261. if ($parsed['scheme'] === '' || preg_match('/^(https?|feed)?$/i', $parsed['scheme']))
  262. {
  263. if (method_exists($link, 'getLineNo') && $this->base_location < $link->getLineNo())
  264. {
  265. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->base));
  266. }
  267. else
  268. {
  269. $href = $this->registry->call('Misc', 'absolutize_url', array(trim($link->getAttribute('href')), $this->http_base));
  270. }
  271. if ($href === false)
  272. {
  273. continue;
  274. }
  275. $current = $this->registry->call('Misc', 'parse_url', array($this->file->url));
  276. if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority'])
  277. {
  278. $this->local[] = $href;
  279. }
  280. else
  281. {
  282. $this->elsewhere[] = $href;
  283. }
  284. }
  285. }
  286. }
  287. $this->local = array_unique($this->local);
  288. $this->elsewhere = array_unique($this->elsewhere);
  289. if (!empty($this->local) || !empty($this->elsewhere))
  290. {
  291. return true;
  292. }
  293. return null;
  294. }
  295. public function get_rel_link($rel)
  296. {
  297. if ($this->dom === null)
  298. {
  299. throw new SimplePie_Exception('DOMDocument not found, unable to use '.
  300. 'locator');
  301. }
  302. if (!class_exists('DOMXpath'))
  303. {
  304. throw new SimplePie_Exception('DOMXpath not found, unable to use '.
  305. 'get_rel_link');
  306. }
  307. $xpath = new DOMXpath($this->dom);
  308. $query = '//a[@rel and @href] | //link[@rel and @href]';
  309. foreach ($xpath->query($query) as $link)
  310. {
  311. $href = trim($link->getAttribute('href'));
  312. $parsed = $this->registry->call('Misc', 'parse_url', array($href));
  313. if ($parsed['scheme'] === '' ||
  314. preg_match('/^https?$/i', $parsed['scheme']))
  315. {
  316. if (method_exists($link, 'getLineNo') &&
  317. $this->base_location < $link->getLineNo())
  318. {
  319. $href =
  320. $this->registry->call('Misc', 'absolutize_url',
  321. array(trim($link->getAttribute('href')),
  322. $this->base));
  323. }
  324. else
  325. {
  326. $href =
  327. $this->registry->call('Misc', 'absolutize_url',
  328. array(trim($link->getAttribute('href')),
  329. $this->http_base));
  330. }
  331. if ($href === false)
  332. {
  333. return null;
  334. }
  335. $rel_values = explode(' ', strtolower($link->getAttribute('rel')));
  336. if (in_array($rel, $rel_values))
  337. {
  338. return $href;
  339. }
  340. }
  341. }
  342. return null;
  343. }
  344. public function extension(&$array)
  345. {
  346. foreach ($array as $key => $value)
  347. {
  348. if ($this->checked_feeds === $this->max_checked_feeds)
  349. {
  350. break;
  351. }
  352. if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
  353. {
  354. $this->checked_feeds++;
  355. $headers = array(
  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',
  357. );
  358. $feed = $this->registry->create('File', array($value, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen, $this->curl_options));
  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))
  360. {
  361. return array($feed);
  362. }
  363. else
  364. {
  365. unset($array[$key]);
  366. }
  367. }
  368. }
  369. return null;
  370. }
  371. public function body(&$array)
  372. {
  373. foreach ($array as $key => $value)
  374. {
  375. if ($this->checked_feeds === $this->max_checked_feeds)
  376. {
  377. break;
  378. }
  379. if (preg_match('/(feed|rss|rdf|atom|xml)/i', $value))
  380. {
  381. $this->checked_feeds++;
  382. $headers = array(
  383. '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',
  384. );
  385. $feed = $this->registry->create('File', array($value, $this->timeout, 5, null, $this->useragent, $this->force_fsockopen, $this->curl_options));
  386. 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))
  387. {
  388. return array($feed);
  389. }
  390. else
  391. {
  392. unset($array[$key]);
  393. }
  394. }
  395. }
  396. return null;
  397. }
  398. }