Locator.php 13 KB

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