From 3fd331e7df5098f287674411b56bacdcf9124a28 Mon Sep 17 00:00:00 2001
From: bcweaver <Brian.Weaver@highlights.com>
Date: Fri, 22 Jun 2012 11:14:11 -0400
Subject: [PATCH] Added configuration support for http auth and ignoring
 invalid ssl certificates

---
 plugins/FeedsHTTPFetcher.inc |   90 +++++++++++++++++++++++++++---------------
 1 files changed, 58 insertions(+), 32 deletions(-)

diff --git a/plugins/FeedsHTTPFetcher.inc b/plugins/FeedsHTTPFetcher.inc
index 1b00395..56db831 100644
--- a/plugins/FeedsHTTPFetcher.inc
+++ b/plugins/FeedsHTTPFetcher.inc
@@ -8,34 +8,6 @@
 feeds_include_library('PuSHSubscriber.inc', 'PuSHSubscriber');
 
 /**
- * Result of FeedsHTTPFetcher::fetch().
- */
-class FeedsHTTPFetcherResult extends FeedsFetcherResult {
-  protected $url;
-  protected $file_path;
-
-  /**
-   * Constructor.
-   */
-  public function __construct($url = NULL) {
-    $this->url = $url;
-    parent::__construct('');
-  }
-
-  /**
-   * Overrides FeedsFetcherResult::getRaw();
-   */
-  public function getRaw() {
-    feeds_include_library('http_request.inc', 'http_request');
-    $result = http_request_get($this->url);
-    if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) {
-      throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
-    }
-    return $this->sanitizeRaw($result->data);
-  }
-}
-
-/**
  * Fetches data via HTTP.
  */
 class FeedsHTTPFetcher extends FeedsFetcher {
@@ -44,11 +16,30 @@ class FeedsHTTPFetcher extends FeedsFetcher {
    * Implements FeedsFetcher::fetch().
    */
   public function fetch(FeedsSource $source) {
-    $source_config = $source->getConfigFor($this);
-    if ($this->config['use_pubsubhubbub'] && ($raw = $this->subscriber($source->feed_nid)->receive())) {
-      return new FeedsFetcherResult($raw);
+    $raw = NULL;
+    $result_object = NULL;  // will instantiate a result object in the if() below
+
+    if ($this->config['use_pubsubhubbub']) {
+      $raw = $this->subscriber($source->feed_nid)->receive(); 
     }
-    return new FeedsHTTPFetcherResult($source_config['source']);
+
+    if(!$raw) {
+      $source_config = $source->getConfigFor($this);
+      $url = $source_config["source"];
+      feeds_include_library('http_request.inc', 'http_request');
+      $result = http_request_get($url, 
+                                 $this->config['username'],
+                                 $this->config['password'],
+                                 $this->config['allow_invalid_certs']);
+
+      if (!in_array($result->code, array(200, 201, 202, 203, 204, 205, 206))) {
+        throw new Exception(t('Download of @url failed with code !code.', 
+                  array('@url' => $this->url, '!code' => $result->code)));
+      }
+      $raw = $result->data;
+    }
+
+    return new FeedsFetcherResult($raw);
   }
 
   /**
@@ -93,8 +84,12 @@ class FeedsHTTPFetcher extends FeedsFetcher {
   public function configDefaults() {
     return array(
       'auto_detect_feeds' => FALSE,
+      'allow_invalid_certs' => FALSE,
       'use_pubsubhubbub' => FALSE,
       'designated_hub' => '',
+      'use_auth_credentials' => TRUE,
+      'username' => '',
+      'password' => '',
     );
   }
 
@@ -109,6 +104,13 @@ class FeedsHTTPFetcher extends FeedsFetcher {
       '#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.'),
       '#default_value' => $this->config['auto_detect_feeds'],
     );
+    $form['allow_invalid_certs'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Allow invalid certificates'),
+      '#description' => t('Import new content items even if the feed URL has an
+      invalid SSL certificate (e.g. self-signed)'),
+      '#default_value' => $this->config['allow_invalid_certs'],
+    );
     $form['use_pubsubhubbub'] = array(
       '#type' => 'checkbox',
       '#title' => t('Use PubSubHubbub'),
@@ -124,6 +126,30 @@ class FeedsHTTPFetcher extends FeedsFetcher {
         'edit-use-pubsubhubbub' => array(1),
       ),
     );
+    $form['use_auth_credentials'] = array(
+      '#type'   => 'checkbox',
+      '#title'  => 'Authentication',
+      '#description'  => 'Log in with a username and password',
+      '#default_value'  => $this->config['use_auth_credentials'],
+    );
+    $form['username'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Username'),
+      '#description' => t(''),
+      '#default_value' => $this->config['username'],
+      '#dependency' => array(
+        'edit-use-auth-credentials' => array(1),
+      ),
+    );
+    $form['password'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Password'),
+      '#description' => t(''),
+      '#default_value' => $this->config['password'],
+      '#dependency' => array(
+        'edit-use-auth-credentials' => array(1),
+      ),
+    );
     return $form;
   }
 
-- 
1.7.4.4

