]> git.wh0rd.org - tt-rss.git/blobdiff - classes/rssutils.php
Merge branch 'pdo-experimental' of git.fakecake.org:tt-rss into pdo-experimental
[tt-rss.git] / classes / rssutils.php
index e892c0bb2f2d3f10d43f161c11d4c9d142086dc5..f6326720de07c6110e881d93e3fb19279e463a15 100644 (file)
@@ -205,9 +205,11 @@ class RSSUtils {
 
                $feed = db_escape_string($feed);
 
-               $result = db_query("SELECT feed_url,auth_pass,auth_login,auth_pass_encrypted
+               $result = db_query("SELECT owner_uid,feed_url,auth_pass,auth_login,auth_pass_encrypted
                                        FROM ttrss_feeds WHERE id = '$feed'");
 
+               $owner_uid = db_fetch_result($result, 0, "owner_uid");
+
                $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result,
                        0, "auth_pass_encrypted"));
 
@@ -221,42 +223,59 @@ class RSSUtils {
 
                $fetch_url = db_fetch_result($result, 0, "feed_url");
 
-               $feed_data = fetch_file_contents($fetch_url, false,
-                       $auth_login, $auth_pass, false,
-                       FEED_FETCH_TIMEOUT,
-                       0);
-
-               global $fetch_curl_used;
+               $pluginhost = new PluginHost();
+               $user_plugins = get_pref("_ENABLED_PLUGINS", $owner_uid);
 
-               if (!$fetch_curl_used) {
-                       $tmp = @gzdecode($feed_data);
+               $pluginhost->load(PLUGINS, PluginHost::KIND_ALL);
+               $pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
+               $pluginhost->load_data();
 
-                       if ($tmp) $feed_data = $tmp;
+               $basic_info = array();
+               foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_BASIC_INFO) as $plugin) {
+                       $basic_info = $plugin->hook_feed_basic_info($basic_info, $fetch_url, $owner_uid, $feed, $auth_login, $auth_pass);
                }
 
-               $feed_data = trim($feed_data);
+               if (!$basic_info) {
+                       $feed_data = fetch_file_contents($fetch_url, false,
+                               $auth_login, $auth_pass, false,
+                               FEED_FETCH_TIMEOUT,
+                               0);
 
-               $rss = new FeedParser($feed_data);
-               $rss->init();
+                       global $fetch_curl_used;
 
-               if (!$rss->error()) {
+                       if (!$fetch_curl_used) {
+                               $tmp = @gzdecode($feed_data);
+
+                               if ($tmp) $feed_data = $tmp;
+                       }
+
+                       $feed_data = trim($feed_data);
+
+                       $rss = new FeedParser($feed_data);
+                       $rss->init();
+
+                       if (!$rss->error()) {
+                               $basic_info = array(
+                                       'title' => db_escape_string(mb_substr($rss->get_title(), 0, 199)),
+                                       'site_url' => db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245))
+                               );
+                       }
+               }
 
+               if ($basic_info && is_array($basic_info)) {
                        $result = db_query("SELECT title, site_url FROM ttrss_feeds WHERE id = '$feed'");
 
                        $registered_title = db_fetch_result($result, 0, "title");
                        $orig_site_url = db_fetch_result($result, 0, "site_url");
 
-                       $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
-                       $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199));
-
-                       if ($feed_title && (!$registered_title || $registered_title == "[Unknown]")) {
+                       if ($basic_info['title'] && (!$registered_title || $registered_title == "[Unknown]")) {
                                db_query("UPDATE ttrss_feeds SET
-                                       title = '$feed_title' WHERE id = '$feed'");
+                                       title = '${basic_info['title']}' WHERE id = '$feed'");
                        }
 
-                       if ($site_url && $orig_site_url != $site_url) {
+                       if ($basic_info['site_url'] && $orig_site_url != $basic_info['site_url']) {
                                db_query("UPDATE ttrss_feeds SET
-                                                       site_url = '$site_url' WHERE id = '$feed'");
+                                                       site_url = '${basic_info['site_url']}' WHERE id = '$feed'");
                        }
                }
        }
@@ -292,7 +311,9 @@ class RSSUtils {
                $result = db_query("SELECT id,update_interval,auth_login,
                        feed_url,auth_pass,cache_images,
                        mark_unread_on_update, owner_uid,
-                       auth_pass_encrypted, feed_language
+                       auth_pass_encrypted, feed_language, 
+                       last_modified, 
+                       ".SUBSTRING_FOR_DATE."(last_unconditional, 1, 19) AS last_unconditional                 
                        FROM ttrss_feeds WHERE id = '$feed'");
 
                $owner_uid = db_fetch_result($result, 0, "owner_uid");
@@ -312,6 +333,8 @@ class RSSUtils {
                        $auth_pass = decrypt_string($auth_pass);
                }
 
+               $stored_last_modified = db_fetch_result($result, 0, "last_modified");
+        $last_unconditional = db_fetch_result($result, 0, "last_unconditional");
                $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
                $fetch_url = db_fetch_result($result, 0, "feed_url");
                $feed_language = db_escape_string(mb_strtolower(db_fetch_result($result, 0, "feed_language")));
@@ -359,18 +382,33 @@ class RSSUtils {
                        _debug("local cache will not be used for this feed", $debug_enabled);
                }
 
+               global $fetch_last_modified;
+
                // fetch feed from source
                if (!$feed_data) {
-                       _debug("fetching [$fetch_url]...", $debug_enabled);
+                       _debug("last unconditional update request: $last_unconditional");
 
                        if (ini_get("open_basedir") && function_exists("curl_init")) {
                                _debug("not using CURL due to open_basedir restrictions");
                        }
 
-                       $feed_data = fetch_file_contents($fetch_url, false,
-                               $auth_login, $auth_pass, false,
-                               $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT,
-                               0);
+            if (time() - strtotime($last_unconditional) > MAX_CONDITIONAL_INTERVAL) {
+                _debug("maximum allowed interval for conditional requests exceeded, forcing refetch");
+
+                $force_refetch = true;
+            } else {
+                _debug("stored last modified for conditional request: $stored_last_modified", $debug_enabled);
+            }
+
+            _debug("fetching [$fetch_url] (force_refetch: $force_refetch)...", $debug_enabled);
+
+                       $feed_data = fetch_file_contents([
+                               "url" => $fetch_url,
+                               "login" => $auth_login,
+                               "pass" => $auth_pass,
+                               "timeout" => $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT,
+                               "last_modified" => $force_refetch ? "" : $stored_last_modified
+                       ]);
 
                        global $fetch_curl_used;
 
@@ -383,6 +421,14 @@ class RSSUtils {
                        $feed_data = trim($feed_data);
 
                        _debug("fetch done.", $debug_enabled);
+                       _debug("source last modified: " . $fetch_last_modified, $debug_enabled);
+
+                       if ($feed_data && $fetch_last_modified != $stored_last_modified) {
+                               $last_modified_escaped = db_escape_string(substr($fetch_last_modified, 0, 245));
+
+                               db_query("UPDATE ttrss_feeds SET last_modified = '$last_modified_escaped' WHERE id = '$feed'");
+
+                       }
 
                        // cache vanilla feed data for re-use
                        if ($feed_data && !$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) {
@@ -497,6 +543,10 @@ class RSSUtils {
 
                        $filters = load_filters($feed, $owner_uid);
 
+                       if ($debug_enabled) {
+                           print_r($filters);
+            }
+
                        _debug("" . count($filters) . " filters loaded.", $debug_enabled);
 
                        $items = $rss->get_items();
@@ -505,7 +555,7 @@ class RSSUtils {
                                _debug("no articles found.", $debug_enabled);
 
                                db_query("UPDATE ttrss_feeds
-                                       SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
+                                       SET last_updated = NOW(), last_unconditional = NOW(), last_error = '' WHERE id = '$feed'");
 
                                return; // no articles
                        }
@@ -631,6 +681,7 @@ class RSSUtils {
                                        "force_catchup" => false, // ugly hack for the time being
                                        "score_modifier" => 0, // no previous value, plugin should recalculate score modifier based on content if needed
                                        "language" => $entry_language,
+                                       "num_comments" => $num_comments, // read only
                                        "feed" => array("id" => $feed,
                                                "fetch_url" => $fetch_url,
                                                "site_url" => $site_url,
@@ -1091,7 +1142,7 @@ class RSSUtils {
                        purge_feed($feed, 0, $debug_enabled);
 
                        db_query("UPDATE ttrss_feeds
-                               SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
+                               SET last_updated = NOW(), last_unconditional = NOW(), last_error = '' WHERE id = '$feed'");
 
 //                     db_query("COMMIT");
 
@@ -1109,7 +1160,7 @@ class RSSUtils {
 
                        db_query(
                                "UPDATE ttrss_feeds SET last_error = '$error_msg',
-                               last_updated = NOW() WHERE id = '$feed'");
+                               last_updated = NOW(), last_unconditional = NOW()   WHERE id = '$feed'");
 
                        unset($rss);
                        return;
@@ -1468,6 +1519,10 @@ class RSSUtils {
                                                // 0       beshort         0xffd8          JPEG image data
                                                //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image");
                                        }
+                                       elseif (preg_match('/^BM/', $contents)) {
+                                               // 0    string          BM      PC bitmap (OS2, Windows BMP files)
+                                               //error_log("check_feed_favicon, favicon_url=$favicon_url isa BMP image");
+                                       }
                                        else {
                                                //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type");
                                                $contents = "";
@@ -1490,4 +1545,4 @@ class RSSUtils {
 
 
 
-}
\ No newline at end of file
+}