PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Feed.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 411 lines | 184 code | 41 blank | 186 comment | 27 complexity | 7c0ca68a7c4b23e42843555fd8669341 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. $libxml_errflag = libxml_use_internal_errors(true);
  172. $doc = new DOMDocument;
  173. if (trim($string) == '') {
  174. require_once 'Zend/Feed/Exception.php';
  175. throw new Zend_Feed_Exception('Document/string being imported'
  176. . ' is an Empty string or comes from an empty HTTP response');
  177. }
  178. $status = $doc->loadXML($string);
  179. libxml_use_internal_errors($libxml_errflag);
  180. if (!$status) {
  181. // prevent the class to generate an undefined variable notice (ZF-2590)
  182. // Build error message
  183. $error = libxml_get_last_error();
  184. if ($error && $error->message) {
  185. $errormsg = "DOMDocument cannot parse XML: {$error->message}";
  186. } else {
  187. $errormsg = "DOMDocument cannot parse XML";
  188. }
  189. /**
  190. * @see Zend_Feed_Exception
  191. */
  192. require_once 'Zend/Feed/Exception.php';
  193. throw new Zend_Feed_Exception($errormsg);
  194. }
  195. // Try to find the base feed element or a single <entry> of an Atom feed
  196. if ($doc->getElementsByTagName('feed')->item(0) ||
  197. $doc->getElementsByTagName('entry')->item(0)) {
  198. /**
  199. * @see Zend_Feed_Atom
  200. */
  201. require_once 'Zend/Feed/Atom.php';
  202. // return a newly created Zend_Feed_Atom object
  203. return new Zend_Feed_Atom(null, $string);
  204. }
  205. // Try to find the base feed element of an RSS feed
  206. if ($doc->getElementsByTagName('channel')->item(0)) {
  207. /**
  208. * @see Zend_Feed_Rss
  209. */
  210. require_once 'Zend/Feed/Rss.php';
  211. // return a newly created Zend_Feed_Rss object
  212. return new Zend_Feed_Rss(null, $string);
  213. }
  214. // $string does not appear to be a valid feed of the supported types
  215. /**
  216. * @see Zend_Feed_Exception
  217. */
  218. require_once 'Zend/Feed/Exception.php';
  219. throw new Zend_Feed_Exception('Invalid or unsupported feed format');
  220. }
  221. /**
  222. * Imports a feed from a file located at $filename.
  223. *
  224. * @param string $filename
  225. * @throws Zend_Feed_Exception
  226. * @return Zend_Feed_Abstract
  227. */
  228. public static function importFile($filename)
  229. {
  230. @ini_set('track_errors', 1);
  231. $feed = @file_get_contents($filename);
  232. @ini_restore('track_errors');
  233. if ($feed === false) {
  234. /**
  235. * @see Zend_Feed_Exception
  236. */
  237. require_once 'Zend/Feed/Exception.php';
  238. throw new Zend_Feed_Exception("File could not be loaded: $php_errormsg");
  239. }
  240. return self::importString($feed);
  241. }
  242. /**
  243. * Attempts to find feeds at $uri referenced by <link ... /> tags. Returns an
  244. * array of the feeds referenced at $uri.
  245. *
  246. * @todo Allow findFeeds() to follow one, but only one, code 302.
  247. *
  248. * @param string $uri
  249. * @throws Zend_Feed_Exception
  250. * @return array
  251. */
  252. public static function findFeeds($uri)
  253. {
  254. // Get the HTTP response from $uri and save the contents
  255. $client = self::getHttpClient();
  256. $client->setUri($uri);
  257. $response = $client->request();
  258. if ($response->getStatus() !== 200) {
  259. /**
  260. * @see Zend_Feed_Exception
  261. */
  262. require_once 'Zend/Feed/Exception.php';
  263. throw new Zend_Feed_Exception("Failed to access $uri, got response code " . $response->getStatus());
  264. }
  265. $contents = $response->getBody();
  266. // Parse the contents for appropriate <link ... /> tags
  267. @ini_set('track_errors', 1);
  268. $pattern = '~(<link[^>]+)/?>~i';
  269. $result = @preg_match_all($pattern, $contents, $matches);
  270. @ini_restore('track_errors');
  271. if ($result === false) {
  272. /**
  273. * @see Zend_Feed_Exception
  274. */
  275. require_once 'Zend/Feed/Exception.php';
  276. throw new Zend_Feed_Exception("Internal error: $php_errormsg");
  277. }
  278. // Try to fetch a feed for each link tag that appears to refer to a feed
  279. $feeds = array();
  280. if (isset($matches[1]) && count($matches[1]) > 0) {
  281. foreach ($matches[1] as $link) {
  282. // force string to be an utf-8 one
  283. if (!mb_check_encoding($link, 'UTF-8')) {
  284. $link = mb_convert_encoding($link, 'UTF-8');
  285. }
  286. $xml = @simplexml_load_string(rtrim($link, ' /') . ' />');
  287. if ($xml === false) {
  288. continue;
  289. }
  290. $attributes = $xml->attributes();
  291. if (!isset($attributes['rel']) || !@preg_match('~^(?:alternate|service\.feed)~i', $attributes['rel'])) {
  292. continue;
  293. }
  294. if (!isset($attributes['type']) ||
  295. !@preg_match('~^application/(?:atom|rss|rdf)\+xml~', $attributes['type'])) {
  296. continue;
  297. }
  298. if (!isset($attributes['href'])) {
  299. continue;
  300. }
  301. try {
  302. // checks if we need to canonize the given uri
  303. try {
  304. $uri = Zend_Uri::factory((string) $attributes['href']);
  305. } catch (Zend_Uri_Exception $e) {
  306. // canonize the uri
  307. $path = (string) $attributes['href'];
  308. $query = $fragment = '';
  309. if (substr($path, 0, 1) != '/') {
  310. // add the current root path to this one
  311. $path = rtrim($client->getUri()->getPath(), '/') . '/' . $path;
  312. }
  313. if (strpos($path, '?') !== false) {
  314. list($path, $query) = explode('?', $path, 2);
  315. }
  316. if (strpos($query, '#') !== false) {
  317. list($query, $fragment) = explode('#', $query, 2);
  318. }
  319. $uri = Zend_Uri::factory($client->getUri(true));
  320. $uri->setPath($path);
  321. $uri->setQuery($query);
  322. $uri->setFragment($fragment);
  323. }
  324. $feed = self::import($uri);
  325. } catch (Exception $e) {
  326. continue;
  327. }
  328. $feeds[$uri->getUri()] = $feed;
  329. }
  330. }
  331. // Return the fetched feeds
  332. return $feeds;
  333. }
  334. /**
  335. * Construct a new Zend_Feed_Abstract object from a custom array
  336. *
  337. * @param array $data
  338. * @param string $format (rss|atom) the requested output format
  339. * @return Zend_Feed_Abstract
  340. */
  341. public static function importArray(array $data, $format = 'atom')
  342. {
  343. $obj = 'Zend_Feed_' . ucfirst(strtolower($format));
  344. if (!class_exists($obj)) {
  345. require_once 'Zend/Loader.php';
  346. Zend_Loader::loadClass($obj);
  347. }
  348. /**
  349. * @see Zend_Feed_Builder
  350. */
  351. require_once 'Zend/Feed/Builder.php';
  352. return new $obj(null, null, new Zend_Feed_Builder($data));
  353. }
  354. /**
  355. * Construct a new Zend_Feed_Abstract object from a Zend_Feed_Builder_Interface data source
  356. *
  357. * @param Zend_Feed_Builder_Interface $builder this object will be used to extract the data of the feed
  358. * @param string $format (rss|atom) the requested output format
  359. * @return Zend_Feed_Abstract
  360. */
  361. public static function importBuilder(Zend_Feed_Builder_Interface $builder, $format = 'atom')
  362. {
  363. $obj = 'Zend_Feed_' . ucfirst(strtolower($format));
  364. if (!class_exists($obj)) {
  365. require_once 'Zend/Loader.php';
  366. Zend_Loader::loadClass($obj);
  367. }
  368. return new $obj(null, null, $builder);
  369. }
  370. }