PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Feed.php

https://github.com/jverkoey/snaapilookup
PHP | 404 lines | 176 code | 40 blank | 188 comment | 23 complexity | cc8f2f6b10047151a9beb90ad3a0fff4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Feed.php 10383 2008-07-24 19:46:15Z matthew $
  20. */
  21. /**
  22. * Feed utility class
  23. *
  24. * Base Zend_Feed class, containing constants and the Zend_Http_Client instance
  25. * accessor.
  26. *
  27. * @category Zend
  28. * @package Zend_Feed
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Feed
  33. {
  34. /**
  35. * HTTP client object to use for retrieving feeds
  36. *
  37. * @var Zend_Http_Client
  38. */
  39. protected static $_httpClient = null;
  40. /**
  41. * Override HTTP PUT and DELETE request methods?
  42. *
  43. * @var boolean
  44. */
  45. protected static $_httpMethodOverride = false;
  46. /**
  47. * @var array
  48. */
  49. protected static $_namespaces = array(
  50. 'opensearch' => 'http://a9.com/-/spec/opensearchrss/1.0/',
  51. 'atom' => 'http://www.w3.org/2005/Atom',
  52. 'rss' => 'http://blogs.law.harvard.edu/tech/rss',
  53. );
  54. /**
  55. * Set the HTTP client instance
  56. *
  57. * Sets the HTTP client object to use for retrieving the feeds.
  58. *
  59. * @param Zend_Http_Client $httpClient
  60. * @return void
  61. */
  62. public static function setHttpClient(Zend_Http_Client $httpClient)
  63. {
  64. self::$_httpClient = $httpClient;
  65. }
  66. /**
  67. * Gets the HTTP client object. If none is set, a new Zend_Http_Client will be used.
  68. *
  69. * @return Zend_Http_Client_Abstract
  70. */
  71. public static function getHttpClient()
  72. {
  73. if (!self::$_httpClient instanceof Zend_Http_Client) {
  74. /**
  75. * @see Zend_Http_Client
  76. */
  77. require_once 'Zend/Http/Client.php';
  78. self::$_httpClient = new Zend_Http_Client();
  79. }
  80. return self::$_httpClient;
  81. }
  82. /**
  83. * Toggle using POST instead of PUT and DELETE HTTP methods
  84. *
  85. * Some feed implementations do not accept PUT and DELETE HTTP
  86. * methods, or they can't be used because of proxies or other
  87. * measures. This allows turning on using POST where PUT and
  88. * DELETE would normally be used; in addition, an
  89. * X-Method-Override header will be sent with a value of PUT or
  90. * DELETE as appropriate.
  91. *
  92. * @param boolean $override Whether to override PUT and DELETE.
  93. * @return void
  94. */
  95. public static function setHttpMethodOverride($override = true)
  96. {
  97. self::$_httpMethodOverride = $override;
  98. }
  99. /**
  100. * Get the HTTP override state
  101. *
  102. * @return boolean
  103. */
  104. public static function getHttpMethodOverride()
  105. {
  106. return self::$_httpMethodOverride;
  107. }
  108. /**
  109. * Get the full version of a namespace prefix
  110. *
  111. * Looks up a prefix (atom:, etc.) in the list of registered
  112. * namespaces and returns the full namespace URI if
  113. * available. Returns the prefix, unmodified, if it's not
  114. * registered.
  115. *
  116. * @return string
  117. */
  118. public static function lookupNamespace($prefix)
  119. {
  120. return isset(self::$_namespaces[$prefix]) ?
  121. self::$_namespaces[$prefix] :
  122. $prefix;
  123. }
  124. /**
  125. * Add a namespace and prefix to the registered list
  126. *
  127. * Takes a prefix and a full namespace URI and adds them to the
  128. * list of registered namespaces for use by
  129. * Zend_Feed::lookupNamespace().
  130. *
  131. * @param string $prefix The namespace prefix
  132. * @param string $namespaceURI The full namespace URI
  133. * @return void
  134. */
  135. public static function registerNamespace($prefix, $namespaceURI)
  136. {
  137. self::$_namespaces[$prefix] = $namespaceURI;
  138. }
  139. /**
  140. * Imports a feed located at $uri.
  141. *
  142. * @param string $uri
  143. * @throws Zend_Feed_Exception
  144. * @return Zend_Feed_Abstract
  145. */
  146. public static function import($uri)
  147. {
  148. $client = self::getHttpClient();
  149. $client->setUri($uri);
  150. $response = $client->request('GET');
  151. if ($response->getStatus() !== 200) {
  152. /**
  153. * @see Zend_Feed_Exception
  154. */
  155. require_once 'Zend/Feed/Exception.php';
  156. throw new Zend_Feed_Exception('Feed failed to load, got response code ' . $response->getStatus());
  157. }
  158. $feed = $response->getBody();
  159. return self::importString($feed);
  160. }
  161. /**
  162. * Imports a feed represented by $string.
  163. *
  164. * @param string $string
  165. * @throws Zend_Feed_Exception
  166. * @return Zend_Feed_Abstract
  167. */
  168. public static function importString($string)
  169. {
  170. // Load the feed as an XML DOMDocument object
  171. @ini_set('track_errors', 1);
  172. $doc = new DOMDocument;
  173. $status = @$doc->loadXML($string);
  174. @ini_restore('track_errors');
  175. if (!$status) {
  176. // prevent the class to generate an undefined variable notice (ZF-2590)
  177. if (!isset($php_errormsg)) {
  178. if (function_exists('xdebug_is_enabled')) {
  179. $php_errormsg = '(error message not available, when XDebug is running)';
  180. } else {
  181. $php_errormsg = '(error message not available)';
  182. }
  183. }
  184. /**
  185. * @see Zend_Feed_Exception
  186. */
  187. require_once 'Zend/Feed/Exception.php';
  188. throw new Zend_Feed_Exception("DOMDocument cannot parse XML: $php_errormsg");
  189. }
  190. // Try to find the base feed element or a single <entry> of an Atom feed
  191. if ($doc->getElementsByTagName('feed')->item(0) ||
  192. $doc->getElementsByTagName('entry')->item(0)) {
  193. /**
  194. * @see Zend_Feed_Atom
  195. */
  196. require_once 'Zend/Feed/Atom.php';
  197. // return a newly created Zend_Feed_Atom object
  198. return new Zend_Feed_Atom(null, $string);
  199. }
  200. // Try to find the base feed element of an RSS feed
  201. if ($doc->getElementsByTagName('channel')->item(0)) {
  202. /**
  203. * @see Zend_Feed_Rss
  204. */
  205. require_once 'Zend/Feed/Rss.php';
  206. // return a newly created Zend_Feed_Rss object
  207. return new Zend_Feed_Rss(null, $string);
  208. }
  209. // $string does not appear to be a valid feed of the supported types
  210. /**
  211. * @see Zend_Feed_Exception
  212. */
  213. require_once 'Zend/Feed/Exception.php';
  214. throw new Zend_Feed_Exception('Invalid or unsupported feed format');
  215. }
  216. /**
  217. * Imports a feed from a file located at $filename.
  218. *
  219. * @param string $filename
  220. * @throws Zend_Feed_Exception
  221. * @return Zend_Feed_Abstract
  222. */
  223. public static function importFile($filename)
  224. {
  225. @ini_set('track_errors', 1);
  226. $feed = @file_get_contents($filename);
  227. @ini_restore('track_errors');
  228. if ($feed === false) {
  229. /**
  230. * @see Zend_Feed_Exception
  231. */
  232. require_once 'Zend/Feed/Exception.php';
  233. throw new Zend_Feed_Exception("File could not be loaded: $php_errormsg");
  234. }
  235. return self::importString($feed);
  236. }
  237. /**
  238. * Attempts to find feeds at $uri referenced by <link ... /> tags. Returns an
  239. * array of the feeds referenced at $uri.
  240. *
  241. * @todo Allow findFeeds() to follow one, but only one, code 302.
  242. *
  243. * @param string $uri
  244. * @throws Zend_Feed_Exception
  245. * @return array
  246. */
  247. public static function findFeeds($uri)
  248. {
  249. // Get the HTTP response from $uri and save the contents
  250. $client = self::getHttpClient();
  251. $client->setUri($uri);
  252. $response = $client->request();
  253. if ($response->getStatus() !== 200) {
  254. /**
  255. * @see Zend_Feed_Exception
  256. */
  257. require_once 'Zend/Feed/Exception.php';
  258. throw new Zend_Feed_Exception("Failed to access $uri, got response code " . $response->getStatus());
  259. }
  260. $contents = $response->getBody();
  261. // Parse the contents for appropriate <link ... /> tags
  262. @ini_set('track_errors', 1);
  263. $pattern = '~(<link[^>]+)/?>~i';
  264. $result = @preg_match_all($pattern, $contents, $matches);
  265. @ini_restore('track_errors');
  266. if ($result === false) {
  267. /**
  268. * @see Zend_Feed_Exception
  269. */
  270. require_once 'Zend/Feed/Exception.php';
  271. throw new Zend_Feed_Exception("Internal error: $php_errormsg");
  272. }
  273. // Try to fetch a feed for each link tag that appears to refer to a feed
  274. $feeds = array();
  275. if (isset($matches[1]) && count($matches[1]) > 0) {
  276. foreach ($matches[1] as $link) {
  277. // force string to be an utf-8 one
  278. if (!mb_check_encoding($link, 'UTF-8')) {
  279. $link = mb_convert_encoding($link, 'UTF-8');
  280. }
  281. $xml = @simplexml_load_string(rtrim($link, ' /') . ' />');
  282. if ($xml === false) {
  283. continue;
  284. }
  285. $attributes = $xml->attributes();
  286. if (!isset($attributes['rel']) || !@preg_match('~^(?:alternate|service\.feed)~i', $attributes['rel'])) {
  287. continue;
  288. }
  289. if (!isset($attributes['type']) ||
  290. !@preg_match('~^application/(?:atom|rss|rdf)\+xml~', $attributes['type'])) {
  291. continue;
  292. }
  293. if (!isset($attributes['href'])) {
  294. continue;
  295. }
  296. try {
  297. // checks if we need to canonize the given uri
  298. try {
  299. $uri = Zend_Uri::factory((string) $attributes['href']);
  300. } catch (Zend_Uri_Exception $e) {
  301. // canonize the uri
  302. $path = (string) $attributes['href'];
  303. $query = $fragment = '';
  304. if (substr($path, 0, 1) != '/') {
  305. // add the current root path to this one
  306. $path = rtrim($client->getUri()->getPath(), '/') . '/' . $path;
  307. }
  308. if (strpos($path, '?') !== false) {
  309. list($path, $query) = explode('?', $path, 2);
  310. }
  311. if (strpos($query, '#') !== false) {
  312. list($query, $fragment) = explode('#', $query, 2);
  313. }
  314. $uri = Zend_Uri::factory($client->getUri(true));
  315. $uri->setPath($path);
  316. $uri->setQuery($query);
  317. $uri->setFragment($fragment);
  318. }
  319. $feed = self::import($uri);
  320. } catch (Exception $e) {
  321. continue;
  322. }
  323. $feeds[] = $feed;
  324. }
  325. }
  326. // Return the fetched feeds
  327. return $feeds;
  328. }
  329. /**
  330. * Construct a new Zend_Feed_Abstract object from a custom array
  331. *
  332. * @param array $data
  333. * @param string $format (rss|atom) the requested output format
  334. * @return Zend_Feed_Abstract
  335. */
  336. public static function importArray(array $data, $format = 'atom')
  337. {
  338. $obj = 'Zend_Feed_' . ucfirst(strtolower($format));
  339. /**
  340. * @see Zend_Loader
  341. */
  342. require_once 'Zend/Loader.php';
  343. Zend_Loader::loadClass($obj);
  344. Zend_Loader::loadClass('Zend_Feed_Builder');
  345. return new $obj(null, null, new Zend_Feed_Builder($data));
  346. }
  347. /**
  348. * Construct a new Zend_Feed_Abstract object from a Zend_Feed_Builder_Interface data source
  349. *
  350. * @param Zend_Feed_Builder_Interface $builder this object will be used to extract the data of the feed
  351. * @param string $format (rss|atom) the requested output format
  352. * @return Zend_Feed_Abstract
  353. */
  354. public static function importBuilder(Zend_Feed_Builder_Interface $builder, $format = 'atom')
  355. {
  356. $obj = 'Zend_Feed_' . ucfirst(strtolower($format));
  357. /**
  358. * @see Zend_Loader
  359. */
  360. require_once 'Zend/Loader.php';
  361. Zend_Loader::loadClass($obj);
  362. return new $obj(null, null, $builder);
  363. }
  364. }