FeedsHTTPFetcher.inc

  1. cis7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  2. cle7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  3. ecd7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  4. elmsmedia7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  5. harmony7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  6. icor7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  7. meedjum_blog7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
  8. mooc7 sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc

Home of the FeedsHTTPFetcher and related classes.

Classes

Namesort descending Description
FeedsHTTPFetcher Fetches data via HTTP.
FeedsHTTPFetcherResult Result of FeedsHTTPFetcher::fetch().
PuSHEnvironment Provide environmental functions to the PuSHSubscriber library.
PuSHSubscription Implement a PuSHSubscriptionInterface.

File

sites/all/modules/ulmus/feeds/plugins/FeedsHTTPFetcher.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Home of the FeedsHTTPFetcher and related classes.
  5. */
  6. feeds_include_library('PuSHSubscriber.inc', 'PuSHSubscriber');
  7. /**
  8. * Result of FeedsHTTPFetcher::fetch().
  9. */
  10. class FeedsHTTPFetcherResult extends FeedsFetcherResult {
  11. protected $url;
  12. protected $file_path;
  13. /**
  14. * Constructor.
  15. */
  16. public function __construct($url = NULL) {
  17. $this->url = $url;
  18. parent::__construct('');
  19. }
  20. /**
  21. * Overrides FeedsFetcherResult::getRaw();
  22. */
  23. public function getRaw() {
  24. feeds_include_library('http_request.inc', 'http_request');
  25. $result = http_request_get($this->url);
  26. if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) {
  27. throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
  28. }
  29. return $this->sanitizeRaw($result->data);
  30. }
  31. }
  32. /**
  33. * Fetches data via HTTP.
  34. */
  35. class FeedsHTTPFetcher extends FeedsFetcher {
  36. /**
  37. * Implements FeedsFetcher::fetch().
  38. */
  39. public function fetch(FeedsSource $source) {
  40. $source_config = $source->getConfigFor($this);
  41. if ($this->config['use_pubsubhubbub'] && ($raw = $this->subscriber($source->feed_nid)->receive())) {
  42. return new FeedsFetcherResult($raw);
  43. }
  44. return new FeedsHTTPFetcherResult($source_config['source']);
  45. }
  46. /**
  47. * Clear caches.
  48. */
  49. public function clear(FeedsSource $source) {
  50. $source_config = $source->getConfigFor($this);
  51. $url = $source_config['source'];
  52. feeds_include_library('http_request.inc', 'http_request');
  53. http_request_clear_cache($url);
  54. }
  55. /**
  56. * Implements FeedsFetcher::request().
  57. */
  58. public function request($feed_nid = 0) {
  59. feeds_dbg($_GET);
  60. @feeds_dbg(file_get_contents('php://input'));
  61. // A subscription verification has been sent, verify.
  62. if (isset($_GET['hub_challenge'])) {
  63. $this->subscriber($feed_nid)->verifyRequest();
  64. }
  65. // No subscription notification has ben sent, we are being notified.
  66. else {
  67. try {
  68. feeds_source($this->id, $feed_nid)->existing()->import();
  69. }
  70. catch (Exception $e) {
  71. // In case of an error, respond with a 503 Service (temporary) unavailable.
  72. header('HTTP/1.1 503 "Not Found"', NULL, 503);
  73. drupal_exit();
  74. }
  75. }
  76. // Will generate the default 200 response.
  77. header('HTTP/1.1 200 "OK"', NULL, 200);
  78. drupal_exit();
  79. }
  80. /**
  81. * Override parent::configDefaults().
  82. */
  83. public function configDefaults() {
  84. return array(
  85. 'auto_detect_feeds' => FALSE,
  86. 'use_pubsubhubbub' => FALSE,
  87. 'designated_hub' => '',
  88. );
  89. }
  90. /**
  91. * Override parent::configForm().
  92. */
  93. public function configForm(&$form_state) {
  94. $form = array();
  95. $form['auto_detect_feeds'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Auto detect feeds'),
  98. '#description' => t('If the supplied URL does not point to a feed but an HTML document, attempt to extract a feed URL from the document.'),
  99. '#default_value' => $this->config['auto_detect_feeds'],
  100. );
  101. $form['use_pubsubhubbub'] = array(
  102. '#type' => 'checkbox',
  103. '#title' => t('Use PubSubHubbub'),
  104. '#description' => t('Attempt to use a <a href="http://en.wikipedia.org/wiki/PubSubHubbub">PubSubHubbub</a> subscription if available.'),
  105. '#default_value' => $this->config['use_pubsubhubbub'],
  106. );
  107. $form['designated_hub'] = array(
  108. '#type' => 'textfield',
  109. '#title' => t('Designated hub'),
  110. '#description' => t('Enter the URL of a designated PubSubHubbub hub (e. g. superfeedr.com). If given, this hub will be used instead of the hub specified in the actual feed.'),
  111. '#default_value' => $this->config['designated_hub'],
  112. '#dependency' => array(
  113. 'edit-use-pubsubhubbub' => array(1),
  114. ),
  115. );
  116. return $form;
  117. }
  118. /**
  119. * Expose source form.
  120. */
  121. public function sourceForm($source_config) {
  122. $form = array();
  123. $form['source'] = array(
  124. '#type' => 'textfield',
  125. '#title' => t('URL'),
  126. '#description' => t('Enter a feed URL.'),
  127. '#default_value' => isset($source_config['source']) ? $source_config['source'] : '',
  128. '#maxlength' => NULL,
  129. '#required' => TRUE,
  130. );
  131. return $form;
  132. }
  133. /**
  134. * Override parent::sourceFormValidate().
  135. */
  136. public function sourceFormValidate(&$values) {
  137. $values['source'] = trim($values['source']);
  138. if (!feeds_valid_url($values['source'], TRUE)) {
  139. $form_key = 'feeds][' . get_class($this) . '][source';
  140. form_set_error($form_key, t('The URL %source is invalid.', array('%source' => $values['source'])));
  141. }
  142. elseif ($this->config['auto_detect_feeds']) {
  143. feeds_include_library('http_request.inc', 'http_request');
  144. if ($url = http_request_get_common_syndication($values['source'])) {
  145. $values['source'] = $url;
  146. }
  147. }
  148. }
  149. /**
  150. * Override sourceSave() - subscribe to hub.
  151. */
  152. public function sourceSave(FeedsSource $source) {
  153. if ($this->config['use_pubsubhubbub']) {
  154. // If this is a feeds node we want to delay the subscription to
  155. // feeds_exit() to avoid transaction race conditions.
  156. if ($source->feed_nid) {
  157. $job = array('fetcher' => $this, 'source' => $source);
  158. feeds_set_subscription_job($job);
  159. }
  160. else {
  161. $this->subscribe($source);
  162. }
  163. }
  164. }
  165. /**
  166. * Override sourceDelete() - unsubscribe from hub.
  167. */
  168. public function sourceDelete(FeedsSource $source) {
  169. if ($this->config['use_pubsubhubbub']) {
  170. // If we're in a feed node, queue the unsubscribe,
  171. // else process immediately.
  172. if ($source->feed_nid) {
  173. $job = array(
  174. 'type' => $source->id,
  175. 'id' => $source->feed_nid,
  176. 'period' => 0,
  177. 'periodic' => FALSE,
  178. );
  179. JobScheduler::get('feeds_push_unsubscribe')->set($job);
  180. }
  181. else {
  182. $this->unsubscribe($source);
  183. }
  184. }
  185. }
  186. /**
  187. * Implement FeedsFetcher::subscribe() - subscribe to hub.
  188. */
  189. public function subscribe(FeedsSource $source) {
  190. $source_config = $source->getConfigFor($this);
  191. $this->subscriber($source->feed_nid)->subscribe($source_config['source'], url($this->path($source->feed_nid), array('absolute' => TRUE)), valid_url($this->config['designated_hub']) ? $this->config['designated_hub'] : '');
  192. }
  193. /**
  194. * Implement FeedsFetcher::unsubscribe() - unsubscribe from hub.
  195. */
  196. public function unsubscribe(FeedsSource $source) {
  197. $source_config = $source->getConfigFor($this);
  198. $this->subscriber($source->feed_nid)->unsubscribe($source_config['source'], url($this->path($source->feed_nid), array('absolute' => TRUE)));
  199. }
  200. /**
  201. * Implement FeedsFetcher::importPeriod().
  202. */
  203. public function importPeriod(FeedsSource $source) {
  204. if ($this->subscriber($source->feed_nid)->subscribed()) {
  205. return 259200; // Delay for three days if there is a successful subscription.
  206. }
  207. }
  208. /**
  209. * Convenience method for instantiating a subscriber object.
  210. */
  211. protected function subscriber($subscriber_id) {
  212. return PushSubscriber::instance($this->id, $subscriber_id, 'PuSHSubscription', PuSHEnvironment::instance());
  213. }
  214. }
  215. /**
  216. * Implement a PuSHSubscriptionInterface.
  217. */
  218. class PuSHSubscription implements PuSHSubscriptionInterface {
  219. public $domain;
  220. public $subscriber_id;
  221. public $hub;
  222. public $topic;
  223. public $status;
  224. public $secret;
  225. public $post_fields;
  226. public $timestamp;
  227. /**
  228. * Load a subscription.
  229. */
  230. public static function load($domain, $subscriber_id) {
  231. if ($v = db_query("SELECT * FROM {feeds_push_subscriptions} WHERE domain = :domain AND subscriber_id = :sid", array(':domain' => $domain, ':sid' => $subscriber_id))->fetchAssoc()) {
  232. $v['post_fields'] = unserialize($v['post_fields']);
  233. return new PuSHSubscription($v['domain'], $v['subscriber_id'], $v['hub'], $v['topic'], $v['secret'], $v['status'], $v['post_fields'], $v['timestamp']);
  234. }
  235. }
  236. /**
  237. * Create a subscription.
  238. */
  239. public function __construct($domain, $subscriber_id, $hub, $topic, $secret, $status = '', $post_fields = '') {
  240. $this->domain = $domain;
  241. $this->subscriber_id = $subscriber_id;
  242. $this->hub = $hub;
  243. $this->topic = $topic;
  244. $this->status = $status;
  245. $this->secret = $secret;
  246. $this->post_fields = $post_fields;
  247. }
  248. /**
  249. * Save a subscription.
  250. */
  251. public function save() {
  252. $this->timestamp = time();
  253. $this->delete($this->domain, $this->subscriber_id);
  254. drupal_write_record('feeds_push_subscriptions', $this);
  255. }
  256. /**
  257. * Delete a subscription.
  258. */
  259. public function delete() {
  260. db_delete('feeds_push_subscriptions')
  261. ->condition('domain', $this->domain)
  262. ->condition('subscriber_id', $this->subscriber_id)
  263. ->execute();
  264. }
  265. }
  266. /**
  267. * Provide environmental functions to the PuSHSubscriber library.
  268. */
  269. class PuSHEnvironment implements PuSHSubscriberEnvironmentInterface {
  270. /**
  271. * Singleton.
  272. */
  273. public static function instance() {
  274. static $env;
  275. if (empty($env)) {
  276. $env = new PuSHEnvironment();
  277. }
  278. return $env;
  279. }
  280. /**
  281. * Implements PuSHSubscriberEnvironmentInterface::msg().
  282. */
  283. public function msg($msg, $level = 'status') {
  284. drupal_set_message(check_plain($msg), $level);
  285. }
  286. /**
  287. * Implements PuSHSubscriberEnvironmentInterface::log().
  288. */
  289. public function log($msg, $level = 'status') {
  290. switch ($level) {
  291. case 'error':
  292. $severity = WATCHDOG_ERROR;
  293. break;
  294. case 'warning':
  295. $severity = WATCHDOG_WARNING;
  296. break;
  297. default:
  298. $severity = WATCHDOG_NOTICE;
  299. break;
  300. }
  301. feeds_dbg($msg);
  302. watchdog('FeedsHTTPFetcher', $msg, array(), $severity);
  303. }
  304. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.