PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/simplepie/SimplePie/Locator.php

https://github.com/luthercollege/reason_package
PHP | 314 lines | 247 code | 24 blank | 43 comment | 61 complexity | f027211e893d5799434d196d3b856521 MD5 | raw file
  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-2009, 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. * @version 1.3-dev
  37. * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @todo phpDoc comments
  44. */
  45. class SimplePie_Locator
  46. {
  47. var $useragent;
  48. var $timeout;
  49. var $file;
  50. var $local = array();
  51. var $elsewhere = array();
  52. var $file_class = 'SimplePie_File';
  53. var $cached_entities = array();
  54. var $http_base;
  55. var $base;
  56. var $base_location = 0;
  57. var $checked_feeds = 0;
  58. var $max_checked_feeds = 10;
  59. var $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer';
  60. public function __construct(&$file, $timeout = 10, $useragent = null, $file_class = 'SimplePie_File', $max_checked_feeds = 10, $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer')
  61. {
  62. $this->file =& $file;
  63. $this->file_class = $file_class;
  64. $this->useragent = $useragent;
  65. $this->timeout = $timeout;
  66. $this->max_checked_feeds = $max_checked_feeds;
  67. $this->content_type_sniffer_class = $content_type_sniffer_class;
  68. }
  69. public function find($type = SIMPLEPIE_LOCATOR_ALL, &$working)
  70. {
  71. if ($this->is_feed($this->file))
  72. {
  73. return $this->file;
  74. }
  75. if ($this->file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  76. {
  77. $sniffer = new $this->content_type_sniffer_class($this->file);
  78. if ($sniffer->get_type() !== 'text/html')
  79. {
  80. return null;
  81. }
  82. }
  83. if ($type & ~SIMPLEPIE_LOCATOR_NONE)
  84. {
  85. $this->get_base();
  86. }
  87. if ($type & SIMPLEPIE_LOCATOR_AUTODISCOVERY && $working = $this->autodiscovery())
  88. {
  89. return $working[0];
  90. }
  91. if ($type & (SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY | SIMPLEPIE_LOCATOR_REMOTE_EXTENSION | SIMPLEPIE_LOCATOR_REMOTE_BODY) && $this->get_links())
  92. {
  93. if ($type & SIMPLEPIE_LOCATOR_LOCAL_EXTENSION && $working = $this->extension($this->local))
  94. {
  95. return $working;
  96. }
  97. if ($type & SIMPLEPIE_LOCATOR_LOCAL_BODY && $working = $this->body($this->local))
  98. {
  99. return $working;
  100. }
  101. if ($type & SIMPLEPIE_LOCATOR_REMOTE_EXTENSION && $working = $this->extension($this->elsewhere))
  102. {
  103. return $working;
  104. }
  105. if ($type & SIMPLEPIE_LOCATOR_REMOTE_BODY && $working = $this->body($this->elsewhere))
  106. {
  107. return $working;
  108. }
  109. }
  110. return null;
  111. }
  112. public function is_feed(&$file)
  113. {
  114. if ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE)
  115. {
  116. $sniffer = new $this->content_type_sniffer_class($file);
  117. $sniffed = $sniffer->get_type();
  118. if (in_array($sniffed, array('application/rss+xml', 'application/rdf+xml', 'text/rdf', 'application/atom+xml', 'text/xml', 'application/xml')))
  119. {
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. elseif ($file->method & SIMPLEPIE_FILE_SOURCE_LOCAL)
  128. {
  129. return true;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. public function get_base()
  137. {
  138. $this->http_base = $this->file->url;
  139. $this->base = $this->http_base;
  140. $elements = SimplePie_Misc::get_element('base', $this->file->body);
  141. foreach ($elements as $element)
  142. {
  143. if ($element['attribs']['href']['data'] !== '')
  144. {
  145. $this->base = SimplePie_Misc::absolutize_url(trim($element['attribs']['href']['data']), $this->http_base);
  146. $this->base_location = $element['offset'];
  147. break;
  148. }
  149. }
  150. }
  151. public function autodiscovery()
  152. {
  153. $links = array_merge(SimplePie_Misc::get_element('link', $this->file->body), SimplePie_Misc::get_element('a', $this->file->body), SimplePie_Misc::get_element('area', $this->file->body));
  154. $done = array();
  155. $feeds = array();
  156. foreach ($links as $link)
  157. {
  158. if ($this->checked_feeds === $this->max_checked_feeds)
  159. {
  160. break;
  161. }
  162. if (isset($link['attribs']['href']['data']) && isset($link['attribs']['rel']['data']))
  163. {
  164. $rel = array_unique(SimplePie_Misc::space_seperated_tokens(strtolower($link['attribs']['rel']['data'])));
  165. if ($this->base_location < $link['offset'])
  166. {
  167. $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->base);
  168. }
  169. else
  170. {
  171. $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->http_base);
  172. }
  173. if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !in_array('stylesheet', $rel) && !empty($link['attribs']['type']['data']) && in_array(strtolower(SimplePie_Misc::parse_mime($link['attribs']['type']['data'])), array('application/rss+xml', 'application/atom+xml'))) && !isset($feeds[$href]))
  174. {
  175. $this->checked_feeds++;
  176. $headers = array(
  177. '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',
  178. );
  179. $feed = new $this->file_class($href, $this->timeout, 5, $headers, $this->useragent);
  180. 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))
  181. {
  182. $feeds[$href] = $feed;
  183. }
  184. }
  185. $done[] = $href;
  186. }
  187. }
  188. if (!empty($feeds))
  189. {
  190. return array_values($feeds);
  191. }
  192. else
  193. {
  194. return null;
  195. }
  196. }
  197. public function get_links()
  198. {
  199. $links = SimplePie_Misc::get_element('a', $this->file->body);
  200. foreach ($links as $link)
  201. {
  202. if (isset($link['attribs']['href']['data']))
  203. {
  204. $href = trim($link['attribs']['href']['data']);
  205. $parsed = SimplePie_Misc::parse_url($href);
  206. if ($parsed['scheme'] === '' || preg_match('/^(http(s)|feed)?$/i', $parsed['scheme']))
  207. {
  208. if ($this->base_location < $link['offset'])
  209. {
  210. $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->base);
  211. }
  212. else
  213. {
  214. $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->http_base);
  215. }
  216. $current = SimplePie_Misc::parse_url($this->file->url);
  217. if ($parsed['authority'] === '' || $parsed['authority'] === $current['authority'])
  218. {
  219. $this->local[] = $href;
  220. }
  221. else
  222. {
  223. $this->elsewhere[] = $href;
  224. }
  225. }
  226. }
  227. }
  228. $this->local = array_unique($this->local);
  229. $this->elsewhere = array_unique($this->elsewhere);
  230. if (!empty($this->local) || !empty($this->elsewhere))
  231. {
  232. return true;
  233. }
  234. return null;
  235. }
  236. public function extension(&$array)
  237. {
  238. foreach ($array as $key => $value)
  239. {
  240. if ($this->checked_feeds === $this->max_checked_feeds)
  241. {
  242. break;
  243. }
  244. if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
  245. {
  246. $this->checked_feeds++;
  247. $headers = array(
  248. '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',
  249. );
  250. $feed = new $this->file_class($value, $this->timeout, 5, $headers, $this->useragent);
  251. 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))
  252. {
  253. return $feed;
  254. }
  255. else
  256. {
  257. unset($array[$key]);
  258. }
  259. }
  260. }
  261. return null;
  262. }
  263. public function body(&$array)
  264. {
  265. foreach ($array as $key => $value)
  266. {
  267. if ($this->checked_feeds === $this->max_checked_feeds)
  268. {
  269. break;
  270. }
  271. if (preg_match('/(rss|rdf|atom|xml)/i', $value))
  272. {
  273. $this->checked_feeds++;
  274. $headers = array(
  275. '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',
  276. );
  277. $feed = new $this->file_class($value, $this->timeout, 5, null, $this->useragent);
  278. 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))
  279. {
  280. return $feed;
  281. }
  282. else
  283. {
  284. unset($array[$key]);
  285. }
  286. }
  287. }
  288. return null;
  289. }
  290. }