From 017401ddce4ee4bafabf74996b9ca1f2d3c51f91 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <fox@madoka.volgo-balt.ru>
Date: Sat, 23 Feb 2013 18:38:50 +0400
Subject: [PATCH] implement HOOK_FEED_FETCHED

---
 classes/pluginhost.php |  1 +
 include/rssfuncs.php   | 35 ++++++++++++++++++++++++++---------
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/classes/pluginhost.php b/classes/pluginhost.php
index c6fcf6b57..1748a067d 100644
--- a/classes/pluginhost.php
+++ b/classes/pluginhost.php
@@ -19,6 +19,7 @@ class PluginHost {
 	const HOOK_HOTKEY_MAP = 9;
 	const HOOK_RENDER_ARTICLE = 10;
 	const HOOK_RENDER_ARTICLE_CDM = 11;
+	const HOOK_FEED_FETCHED = 12;
 
 	const KIND_ALL = 1;
 	const KIND_SYSTEM = 2;
diff --git a/include/rssfuncs.php b/include/rssfuncs.php
index 4ad637342..af9a8b65c 100644
--- a/include/rssfuncs.php
+++ b/include/rssfuncs.php
@@ -204,15 +204,14 @@
 
 		$feed = db_escape_string($feed);
 
-		if ($auth_login && $auth_pass ){
+		/* if ($auth_login && $auth_pass ){
 			$url_parts = array();
 			preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
 
 			if ($url_parts[1] && $url_parts[2]) {
 				$fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
 			}
-
-		}
+		} */
 
 		if ($override_url)
 			$fetch_url = $override_url;
@@ -231,12 +230,32 @@
 			mkdir($simplepie_cache_dir);
 		}
 
+		$feed_data = fetch_file_contents($fetch_url, false,
+			$auth_login, $auth_pass, false, $no_cache ? 15 : 45);
+
+		if (!$feed_data) {
+			global $fetch_last_error;
+
+			if ($debug_enabled) {
+				_debug("update_rss_feed: unable to fetch: $fetch_last_error");
+			}
+
+			db_query($link,
+				"UPDATE ttrss_feeds SET last_error = '$fetch_last_error',
+					last_updated = NOW() WHERE id = '$feed'");
+
+			return;
+		}
+
+		$pluginhost = new PluginHost($link);
+
+		foreach ($pluginhost->get_hooks($pluginhost::HOOK_FEED_FETCHED) as $plugin) {
+			$feed_data = $plugin->hook_feed_fetched($feed_data);
+		}
+
 		$rss = new SimplePie();
-		$rss->set_useragent(SELF_USER_AGENT);
-		$rss->set_timeout($no_cache ? 15 : 60);
-		$rss->set_feed_url($fetch_url);
 		$rss->set_output_encoding('UTF-8');
-		$rss->force_feed(true);
+		$rss->set_raw_data($feed_data);
 
 		if ($debug_enabled) {
 			_debug("feed update interval (sec): " .
@@ -265,8 +284,6 @@
 			// We use local pluginhost here because we need to load different per-user feed plugins
 			$user_plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid);
 
-			$pluginhost = new PluginHost($link);
-
 			$pluginhost->load(PLUGINS, $pluginhost::KIND_ALL);
 			$pluginhost->load($user_plugins, $pluginhost::KIND_USER, $owner_uid);
 			$pluginhost->load_data();
-- 
GitLab