]> git.wh0rd.org - tt-rss.git/blobdiff - include/rssfuncs.php
fix schema for favicon_avg_color
[tt-rss.git] / include / rssfuncs.php
index d9a907c1840eea880b6cd64331b9e74cc922a37d..49870090615304338c84e48d84c5c70206a69866 100644 (file)
@@ -77,6 +77,7 @@
                if (DB_TYPE == "pgsql") {
                        $update_limit_qpart = "AND ((
                                        ttrss_feeds.update_interval = 0
+                                       AND ttrss_user_prefs.value != '-1'
                                        AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
                                ) OR (
                                        ttrss_feeds.update_interval > 0
@@ -86,6 +87,7 @@
                } else {
                        $update_limit_qpart = "AND ((
                                        ttrss_feeds.update_interval = 0
+                                       AND ttrss_user_prefs.value != '-1'
                                        AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
                                ) OR (
                                        ttrss_feeds.update_interval > 0
                $random_qpart = sql_random_function();
 
                // We search for feed needing update.
-               $result = db_query($link, "SELECT DISTINCT ttrss_feeds.feed_url
+               $result = db_query($link, "SELECT DISTINCT ttrss_feeds.feed_url,$random_qpart
                        FROM
                                ttrss_feeds, ttrss_users, ttrss_user_prefs
                        WHERE
                                AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
                                $login_thresh_qpart $update_limit_qpart
                                $updstart_thresh_qpart
-                       ORDER BY feed_url $query_limit");
+                       ORDER BY $random_qpart $query_limit");
 
                $user_prefs_cache = array();
 
                expire_cached_files($debug);
                expire_lock_files($debug);
 
+               $nf = 0;
+
                // For each feed, we call the feed update function.
                foreach ($feeds_to_update as $feed) {
                        if($debug) _debug("Base feed: $feed");
 
                        // since we have the data cached, we can deal with other feeds with the same url
 
-                       $tmp_result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id,last_updated
-                       FROM ttrss_feeds, ttrss_users WHERE
-                               ttrss_users.id = ttrss_feeds.owner_uid AND
+                       $tmp_result = db_query($link, "SELECT DISTINCT ttrss_feeds.id,last_updated
+                       FROM ttrss_feeds, ttrss_users, ttrss_user_prefs WHERE
+                               ttrss_user_prefs.owner_uid = ttrss_feeds.owner_uid AND
+                               ttrss_users.id = ttrss_user_prefs.owner_uid AND
+                               ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' AND
                                feed_url = '".db_escape_string($link, $feed)."' AND
-                               ttrss_feeds.update_interval != -1
+                               (ttrss_feeds.update_interval > 0 OR
+                                       ttrss_user_prefs.value != '-1')
                                $login_thresh_qpart
-                       ORDER BY feed_url $query_limit");
+                       ORDER BY ttrss_feeds.id $query_limit");
 
                        if (db_num_rows($tmp_result) > 0) {
                                while ($tline = db_fetch_assoc($tmp_result)) {
-                                       if($debug) _debug(" => " . $tline["feed_url"] . ", " . $tline["last_updated"] . ", " . $tline["id"]);
+                                       if($debug) _debug(" => " . $tline["last_updated"] . ", " . $tline["id"]);
                                        update_rss_feed($link, $tline["id"], true);
+                                       ++$nf;
                                }
                        }
                }
                // Send feed digests by email if needed.
                send_headlines_digests($link, $debug);
 
+               return $nf;
+
        } // function update_daemon_common
 
        // ignore_daemon is not used
                $result = db_query($link, "SELECT id,update_interval,auth_login,
                        feed_url,auth_pass,cache_images,last_updated,
                        mark_unread_on_update, owner_uid,
-                       pubsub_state
+                       pubsub_state, auth_pass_encrypted
                        FROM ttrss_feeds WHERE id = '$feed'");
 
                if (db_num_rows($result) == 0) {
                $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result,
                        0, "mark_unread_on_update"));
                $pubsub_state = db_fetch_result($result, 0, "pubsub_state");
+               $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result,
+                       0, "auth_pass_encrypted"));
 
                db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
                        WHERE id = '$feed'");
                $auth_login = db_fetch_result($result, 0, "auth_login");
                $auth_pass = db_fetch_result($result, 0, "auth_pass");
 
+               if ($auth_pass_encrypted) {
+                       require_once "crypt.php";
+                       $auth_pass = decrypt_string($auth_pass);
+               }
+
                $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
                $fetch_url = db_fetch_result($result, 0, "feed_url");
 
 
                $rss = false;
                $rss_hash = false;
+               $cache_timestamp = file_exists($cache_filename) ? filemtime($cache_filename) : 0;
+               $last_updated_timestamp = strtotime($last_updated);
 
                if (file_exists($cache_filename) &&
                        is_readable($cache_filename) &&
                                        _debug("update_rss_feed: using local cache.");
                                }
 
-                               @$rss_data = file_get_contents($cache_filename);
+                               if ($cache_timestamp > $last_updated_timestamp) {
+                                       @$rss_data = file_get_contents($cache_filename);
 
-                               if ($rss_data) {
-                                       $rss_hash = sha1($rss_data);
-                                       @$rss = unserialize($rss_data);
+                                       if ($rss_data) {
+                                               $rss_hash = sha1($rss_data);
+                                               @$rss = unserialize($rss_data);
+                                       }
+                               } else {
+                                       if ($debug_enabled) {
+                                               _debug("update_rss_feed: local cache valid and older than last_updated, nothing to do.");
+                                       }
+                                       return;
                                }
                }
 
 
                        if (!$feed_data) {
                                if ($debug_enabled) {
-                                       _debug("update_rss_feed: fetching [$fetch_url]...");
+                                       _debug("update_rss_feed: fetching [$fetch_url] (ts: $cache_timestamp/$last_updated_timestamp)");
                                }
 
+                               $force_refetch = isset($_REQUEST["force_refetch"]);
+
                                $feed_data = fetch_file_contents($fetch_url, false,
-                                       $auth_login, $auth_pass, false, $no_cache ? 15 : 45);
+                                       $auth_login, $auth_pass, false,
+                                       $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT,
+                                       $force_refetch ? 0 : max($last_updated_timestamp, $cache_timestamp));
 
                                if ($debug_enabled) {
                                        _debug("update_rss_feed: fetch done.");
 
                        if (!$feed_data) {
                                global $fetch_last_error;
+                               global $fetch_last_error_code;
 
                                if ($debug_enabled) {
-                                       _debug("update_rss_feed: unable to fetch: $fetch_last_error");
+                                       _debug("update_rss_feed: unable to fetch: $fetch_last_error [$fetch_last_error_code]");
                                }
 
-                               $error_escaped = db_escape_string($link, $fetch_last_error);
+                               $error_escaped = '';
+
+                               // If-Modified-Since
+                               if ($fetch_last_error_code != 304) {
+                                       $error_escaped = db_escape_string($link, $fetch_last_error);
+                               } else {
+                                       if ($debug_enabled) {
+                                               _debug("update_rss_feed: source claims data not modified, nothing to do.");
+                                       }
+                               }
 
                                db_query($link,
                                        "UPDATE ttrss_feeds SET last_error = '$error_escaped',
                        }
 
                        if ($favicon_needs_check) {
-                               check_feed_favicon($site_url, $feed, $link);
-
-                               db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW()
+                               $favicon_file = check_feed_favicon($site_url, $feed, $link);
+                if ($favicon_file) {
+                                   $favicon_color = calculate_avg_color($favicon_file);
+                    if (is_array($favicon_color))
+                        $favicon_colorstring = ",favicon_avg_color = '" . implode("|", array_slice($favicon_color, 0, 3)) . "'";
+                }
+                               
+                               db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW() $favicon_colorstring
                                        WHERE id = '$feed'");
                        }
 
                                        _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
                                }
 
-                               $entry_title = $item->get_title();
+                               $entry_title = html_entity_decode($item->get_title());
 
                                $entry_link = rewrite_relative_url($site_url, $item->get_link());
 
        }
 
        function expire_cached_files($debug) {
-               foreach (array("simplepie", "images", "export") as $dir) {
+               foreach (array("simplepie", "images", "export", "upload") as $dir) {
                        $cache_dir = CACHE_DIR . "/$dir";
 
                        if ($debug) _debug("Expiring $cache_dir");
                        if ($filter_match) {
                                foreach ($filter["actions"] AS $action) {
                                        array_push($matches, $action);
+
+                                       // if Stop action encountered, perform no further processing
+                                       if ($action["type"] == "stop") return $matches;
                                }
                        }
                }