]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
update translations
[tt-rss.git] / functions.php
index 7eb8dd405edc55fccd569ae8d9e8edf7a3a00c51..648ab3438657dc4a4ef36c60846a3c6810469ec4 100644 (file)
@@ -38,7 +38,7 @@
 
        if (ENABLE_TRANSLATIONS == true) { // If translations are enabled.
                require_once "accept-to-gettext.php";
-               require_once "gettext/gettext.inc";
+               require_once "lib/gettext/gettext.inc";
 
                function startup_gettext() {
        
        require_once 'errors.php';
        require_once 'version.php';
 
-       require_once 'phpmailer/class.phpmailer.php';
+       require_once 'lib/phpmailer/class.phpmailer.php';
 
        define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
        define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes
 
-       require_once "simplepie/simplepie.inc";
-       require_once "magpierss/rss_fetch.inc";
-       require_once 'magpierss/rss_utils.inc';
+       require_once "lib/simplepie/simplepie.inc";
+       require_once "lib/magpierss/rss_fetch.inc";
+       require_once 'lib/magpierss/rss_utils.inc';
 
        /**
         * Print a timestamped debug message.
        function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
 
                if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
-
+               
                $rows = -1;
 
                $result = db_query($link, 
                        $owner_uid = db_fetch_result($result, 0, "owner_uid");
                }
 
+               if ($purge_interval == -1 || !$purge_interval) {
+                       if ($owner_uid) {
+                               ccache_update($link, $feed_id, $owner_uid);
+                       }
+                       return;
+               }
+
                if (!$owner_uid) return;
 
-               $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
-                       $owner_uid, false);
+               if (FORCE_ARTICLE_PURGE == 0) {
+                       $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
+                               $owner_uid, false);
+               } else {
+                       $purge_unread = true;
+                       $purge_interval = FORCE_ARTICLE_PURGE;
+               }
 
                if (!$purge_unread) $query_limit = " unread = false AND ";
 
 //                             print "Feed $feed_id: purge interval = $purge_interval\n";
                        }
 
-                       if ($purge_interval > 0) {
+                       if ($purge_interval > 0 || FORCE_ARTICLE_PURGE) {
                                purge_feed($link, $feed_id, $purge_interval, $do_output);
                        }
                }       
                        $owner_uid = db_fetch_result($result, 0, "owner_uid");
 
                        if ($purge_interval == 0) $purge_interval = get_pref($link, 
-                               'PURGE_OLD_DAYS', $user_id);
+                               'PURGE_OLD_DAYS', $owner_uid);
 
                        return $purge_interval;
 
                        }
                }       
 
+               purge_orphans($link);
+       }
+
+       function purge_orphans($link) {
                // purge orphaned posts in main content table
                db_query($link, "DELETE FROM ttrss_entries WHERE 
                        (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
                }
        }
 
-       function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
-
-               if (WEB_DEMO_MODE) return;
-
-               if (!$user_id) {
-                       $user_id = $_SESSION["uid"];
-                       purge_old_posts($link);
-               }
-
-//             db_query($link, "BEGIN");
-
-               if (MAX_UPDATE_TIME > 0) {
-                       if (DB_TYPE == "mysql") {
-                               $q_order = "RAND()";
-                       } else {
-                               $q_order = "RANDOM()";
-                       }
-               } else {
-                       $q_order = "last_updated DESC";
-               }
-
-               $result = db_query($link, "SELECT feed_url,id,
-                       ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
-                       update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
-                       ORDER BY $q_order");
-
-               $upd_start = time();
-
-               while ($line = db_fetch_assoc($result)) {
-                       $upd_intl = $line["update_interval"];
-
-                       if (!$upd_intl || $upd_intl == 0) {
-                               $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
-                       }
-
-                       if ($upd_intl < 0) { 
-                               // Updates for this feed are disabled
-                               continue; 
-                       }
-
-                       if ($fetch || (!$line["last_updated"] || 
-                               time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
-
-//                             print "<!-- feed: ".$line["feed_url"]." -->";
-
-                               update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
-
-                               $upd_elapsed = time() - $upd_start;
-
-                               if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
-                                       return;
-                               }
-                       }
-               }
-
-//             db_query($link, "COMMIT");
-
-       }
-
        function fetch_file_contents($url) {
                if (USE_CURL_FOR_ICONS) {
                        $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
                } else {
 
                        $result = db_query($link, "SELECT id,update_interval,auth_login,
-                               auth_pass,cache_images,update_method,hidden
+                               auth_pass,cache_images,update_method,hidden,last_updated
                                FROM ttrss_feeds WHERE id = '$feed'");
 
                }
 
                $hidden = sql_bool_to_bool(db_fetch_result($result, 0, "hidden"));
                $update_method = db_fetch_result($result, 0, "update_method");
+               $last_updated = db_fetch_result($result, 0, "last_updated");
 
                db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
                        WHERE id = '$feed'");
                                }
                        } 
 
+                       if (!$last_updated) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: new feed, catching it up...");
+                               }
+                               catchup_feed($link, $feed, false);
+                       }
+
                        if (!$hidden) {
                                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: updating counters cache...");
                                }
 
-                               ccache_update($link, $feed, $owner_uid);
+                               // disabled, purge_feed() does that...
+                               //ccache_update($link, $feed, $owner_uid);
                        }
 
+                       purge_feed($link, $feed, 0);
+
                        db_query($link, "UPDATE ttrss_feeds 
                                SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
 
                                $inverse = $filter["inverse"];
 
                                if ($inverse) {
-                                       if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
+                                       if (!preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
                                                array_push($matches, array($filter["action"], $filter["action_param"]));
                                        }
                                } else {
        }
 
        function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
-               $rtl_content = false, $last_updated = false, $last_error = false) {
+               $rtl_content = false, $last_updated = false, $last_error = false,
+               $fg_content = false, $bg_content = false) {
 
                if (file_exists($icon_file) && filesize($icon_file) > 0) {
                                $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
                $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" 
                        href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
 
+/*             if ($feed_id < -10) {
+                       $bg_color = "#00ccff";
+                       $fg_color = "white";
+               }
+
+               if ($fg_color || $bg_color) {
+                       $color_str = "<div class='labelColorIndicator'
+                               style='color : $fg_color; background-color : $bg_color'>l</div>";
+               } 
+
+               print $color_str; */
+
                print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
                if (get_pref($link, 'ENABLE_FEED_ICONS')) {
                        print "$feed_icon";
                        print "<div class=\"feedExtInfo\">
                                <span id=\"FLUPD-$feed_id\">$last_updated ($total total) $error_notify_msg</span></div>";
                }
-                        
+
                print "</li>";
 
        }
 
        function initialize_user($link, $uid) {
 
-               db_query($link, "INSERT INTO ttrss_labels2 (owner_uid, caption)
+/*             db_query($link, "INSERT INTO ttrss_labels2 (owner_uid, caption)
                        VALUES ('$uid', 'All Articles')");
 
                db_query($link, "INSERT INTO ttrss_filters 
                        (owner_uid, feed_id, filter_type, reg_exp, enabled, 
                                action_id, action_param, filter_param) 
-                       VALUES ('$uid', NULL, 1, '.', true, 7, 'All Articles', 'before')");
+                       VALUES ('$uid', NULL, 1, '.', true, 7, 'All Articles', 'before')"); */
 
                db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
                        values ('$uid', 'Tiny Tiny RSS: New Releases',
                                }
                        }
 
-//                     print session_id();
-//                     print_r($_SESSION);
-
                        if (!$_SESSION["uid"] || !validate_session($link)) {
                                render_login_form($link, $mobile);
+                               //header("Location: login.php");
                                exit;
                        } else {
                                /* bump login timestamp */
                }
        }
        
+       function bool_to_sql_bool($s) {
+               if ($s) {
+                       return "true";
+               } else {
+                       return "false";
+               }
+       }
 
        function toggleEvenOdd($a) {
                if ($a == "even") 
                        
                                if ($cat_view) {
 
-                                       if ($feed > 0) {
-                                               $cat_qpart = "cat_id = '$feed'";
-                                       } else {
-                                               $cat_qpart = "cat_id IS NULL";
-                                       }
+                                       if ($feed >= 0) {
+
+                                               if ($feed > 0) {
+                                                       $cat_qpart = "cat_id = '$feed'";
+                                               } else {
+                                                       $cat_qpart = "cat_id IS NULL";
+                                               }
                                        
-                                       $tmp_result = db_query($link, "SELECT id 
-                                               FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . 
-                                               $_SESSION["uid"]);
+                                               $tmp_result = db_query($link, "SELECT id 
+                                                       FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . 
+                                                       $_SESSION["uid"]);
 
-                                       while ($tmp_line = db_fetch_assoc($tmp_result)) {
+                                               while ($tmp_line = db_fetch_assoc($tmp_result)) {
+
+                                                       $tmp_feed = $tmp_line["id"];
+
+                                                       db_query($link, "UPDATE ttrss_user_entries 
+                                                               SET unread = false,last_read = NOW() 
+                                                               WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
+                                               }
+                                       } else if ($feed == -2) {
 
-                                               $tmp_feed = $tmp_line["id"];
 
                                                db_query($link, "UPDATE ttrss_user_entries 
-                                                       SET unread = false,last_read = NOW() 
-                                                       WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
+                                                       SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*) 
+                                                               FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0 
+                                                       AND unread = true AND owner_uid = " . $_SESSION["uid"]);
                                        }
 
                                } else if ($feed > 0) {
                                                catchupArticlesById($link, $affected_ids, 0);
                                        }
 
+                                       if ($feed == -4) {
+                                               db_query($link, "UPDATE ttrss_user_entries 
+                                                       SET unread = false,last_read = NOW()
+                                                       WHERE owner_uid = ".$_SESSION["uid"]);
+                                       }
+
                                } else if ($feed < -10) { // label
 
                                        $label_id = -$feed - 11;
 
                                        db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2 
-                                               SET unread = false WHERE label_id = '$label_id' AND unread = true
+                                               SET unread = false, last_read = NOW() 
+                                                       WHERE label_id = '$label_id' AND unread = true
                                                        AND owner_uid = '".$_SESSION["uid"]."' AND ref_id = article_id");
 
                                }
 
        function getAllCounters($link, $omode = "flc", $active_feed = false) {
 
-               /* getting all counters is a resource intensive operation, so we
-                * rate limit it a little bit */
-
-
-
-//             if (get_pref($link, "SYNC_COUNTERS") || 
-//                             time() - $_SESSION["get_all_counters_stamp"] > 5) {
-
-                       if (!$omode) $omode = "flc";
+               if (!$omode) $omode = "flc";
        
-                       getGlobalCounters($link);
+               getGlobalCounters($link);
        
-                       if (strchr($omode, "l")) getLabelCounters($link);
-                       if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
-                       if (strchr($omode, "t")) getTagCounters($link);
-                       if (strchr($omode, "c")) {                      
-                               if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                                       getCategoryCounters($link);
-                               }
+               if (strchr($omode, "l")) getLabelCounters($link);
+               if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
+               if (strchr($omode, "t")) getTagCounters($link);
+               if (strchr($omode, "c")) {                      
+                       if (get_pref($link, 'ENABLE_FEED_CATS')) {
+                               getCategoryCounters($link);
                        }
-
-                       $_SESSION["get_all_counters_stamp"] = time();
-//             }
-
+               }
        }       
 
        function getCategoryCounters($link) {
                # two special categories are -1 and -2 (all virtuals; all labels)
 
-               $ctr = getCategoryUnread($link, -1);
+/*             $ctr = getCategoryUnread($link, -1);
 
-               print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
+               print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; */
 
-               $ctr = getCategoryUnread($link, -2);
+               $ctr = getCategoryUnread($link, -2); 
 
                print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
 
                        return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
                } else if ($cat == -2) {
 
-                       // FIXME: NEW_LABELS
+                       $result = db_query($link, "
+                               SELECT COUNT(unread) AS unread FROM 
+                                       ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds 
+                               WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND 
+                                       ttrss_labels2.owner_uid = '$owner_uid'
+                                       AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
+                                       AND ttrss_user_entries.owner_uid = '$owner_uid'");
 
-/*                     $rv = getLabelCounters($link, false, true);
-                       $ctr = 0;
+                       $unread = db_fetch_result($result, 0, "unread");
 
-                       foreach (array_keys($rv) as $k) {
-                               if ($k < -10) {
-                                       $ctr += $rv[$k]["counter"];
-                               }
-                       }
+                       return $unread;
 
-                       return $ctr; */
                } 
        }
 
                        } else {
                                $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
                        }
-
+               } else if ($n_feed == -4) {
+                       $match_part = "true";
                } else if ($n_feed > 0) {
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds 
 
                $tctrs_modified = false;
 
-/*             $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
-                       FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
-                       ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
-                       post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
-               UNION
-                       select tag_name,0 as count FROM ttrss_tags
-                       WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
-
                $age_qpart = getMaxAgeSubquery();
 
                $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
 
                $ret_arr = array();
 
-               for ($i = -1; $i >= -3; $i--) {
+               for ($i = -1; $i >= -4; $i--) {
 
                        $count = getFeedUnread($link, $i);
        
 
                        $owner_uid = $_SESSION["uid"];
 
-                       $result = db_query($link,
-                               "SELECT ttrss_labels2.id, caption, COUNT(unread) AS unread FROM ttrss_labels2 
-                                       LEFT JOIN ttrss_user_labels2 ON (label_id = ttrss_labels2.id) 
-                                               LEFT JOIN ttrss_user_entries ON (ref_id = article_id AND
-                                                       unread = true AND
-                                                       ttrss_user_entries.owner_uid = '$owner_uid'),ttrss_feeds
-                                               WHERE ttrss_feeds.id = feed_id AND
-                                                       hidden = false AND                                              
-                                                       ttrss_labels2.owner_uid = '$owner_uid'
-                                       GROUP BY ttrss_labels2.id, caption");
+                       $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
+                               WHERE owner_uid = '$owner_uid'");
                
                        while ($line = db_fetch_assoc($result)) {
        
                                $id = -$line["id"] - 11;
        
                                $label_name = $line["caption"];
-                               $count = $line["unread"];
+                               $count = getFeedUnread($link, $id);
                
                                if (!$smart_mode || $old_counters[$id] != $count) {     
                                        $old_counters[$id] = $count;
 
                $old_counters = $_SESSION["fctr_last_value"];
 
-/*             $query = "SELECT ttrss_feeds.id,
-                               ttrss_feeds.title,
-                               ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, 
-                               last_error, 
-                               COUNT(ttrss_entries.id) AS count 
-                       FROM ttrss_feeds 
-                               LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id 
-                                       AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid 
-                                       AND ttrss_user_entries.unread = true) 
-                               LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
-                                       $age_qpart) 
-                       WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."  
-                               AND parent_feed IS NULL 
-                               GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error"; */
-
                $query = "SELECT ttrss_feeds.id,
                                ttrss_feeds.title,
                                ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, 
 
                        $has_img = feed_has_icon($id);
 
-/*                     $tmp_result = db_query($link,
-                               "SELECT ttrss_feeds.id,COUNT(unread) AS unread
-                               FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
-                                       ON (ttrss_feeds.id = ttrss_user_entries.feed_id) 
-                               LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id) 
-                               WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id"); */
-
                        $tmp_result = db_query($link,
                                "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache 
                                        WHERE parent_feed = '$id' AND feed_id = id");
        }
 
        function get_script_dt_add() {
-               if (strpos(VERSION, ".99") === false) {
+/*             if (strpos(VERSION, ".99") === false) {
                        return VERSION;
                } else {
                        return time();
-               }
+               } */
+               return time();
        }
 
        function get_pgsql_version($link) {
                        return __("Published articles");
                } else if ($id == -3) {
                        return __("Fresh articles");
+               } else if ($id == -4) {
+                       return __("All articles");
                } else if ($id < -10) {
                        $label_id = -$id - 11;
                        $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
                print "<param key=\"hide_read_feeds\" value=\"" . 
                        (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
 
+               print "<param key=\"enable_feed_cats\" value=\"" . 
+                       (int) get_pref($link, "ENABLE_FEED_CATS") . "\"/>";
+
                print "<param key=\"feeds_sort_by_unread\" value=\"" . 
                        (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
 
 
                print "<param key=\"sync_counters\" value=\"1\"/>";
 
+               print "<param key=\"offline_enabled\" value=\"".
+                       (int) get_pref($link, "ENABLE_OFFLINE_READING") . "\"/>";
+
+               $result = db_query($link, "SELECT COUNT(*) AS cf FROM
+                       ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
+
+               $num_feeds = db_fetch_result($result, 0, "cf");
+
+               print "<param key=\"num_feeds\" value=\"".
+                       (int)$num_feeds. "\"/>";
+
                print "</init-params>";
        }
 
        function print_runtime_info($link) {
                print "<runtime-info>";
 
+               $result = db_query($link, "SELECT COUNT(*) AS cf FROM
+                       ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
+
+               $num_feeds = db_fetch_result($result, 0, "cf");
+
+               print "<param key=\"num_feeds\" value=\"".
+                       (int)$num_feeds. "\"/>";
+
                if (ENABLE_UPDATE_DAEMON) {
                        print "<param key=\"daemon_is_running\" value=\"".
                                sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
 
                if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
                        
-                       if ($_SESSION["last_version_check"] + 86400 < time()) {
+                       if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
                                $new_version_details = check_for_update($link);
 
                                print "<param key=\"new_version_available\" value=\"".
                        if ($view_mode == "unread") {
                                $view_query_part = " unread = true AND ";
                        }
-       
+
+                       if ($view_mode == "updated") {
+                               $view_query_part = " (last_read is null and unread = false) AND ";
+                       }
+
                        if ($limit > 0) {
                                $limit_query_part = "LIMIT " . $limit;
                        } 
                        } else if ($feed == -1) { // starred virtual feed
                                $query_strategy_part = "marked = true";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
-                       } else if ($feed == -2) { // published virtual feed
-                               $query_strategy_part = "published = true";
-                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else if ($feed == -2) { // published virtual feed OR labels category
+
+                               if (!$cat_view) {
+                                       $query_strategy_part = "published = true";
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               } else {
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+
+                                       $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
+       
+                                       $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
+                                               ttrss_user_labels2.article_id = ref_id";
+
+                               }
+
                        } else if ($feed == -3) { // fresh virtual feed
                                $query_strategy_part = "unread = true";
 
                                        $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
                                }
 
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                       } else if ($feed == -4) { // all articles virtual feed
+                               $query_strategy_part = "true";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
                        } else if ($feed <= -10) { // labels
                                $label_id = -$feed - 11;
                                $feed_title = __("Starred articles");
                                if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed == -2) {
-                               $feed_title = __("Published articles");
-                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                               if (!$cat_view) {
+                                       $feed_title = __("Published articles");
+                                       if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                               } else {
+                                       $feed_title = __("Labels");
+                               }
                        } else if ($feed == -3) {
                                $feed_title = __("Fresh articles");
                                if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
+                       } else if ($feed == -4) {
+                               $feed_title = __("All articles");
+                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed < -10) {
                                $label_id = -$feed - 11;
                                $result = db_query($link, "SELECT caption FROM ttrss_labels2
                                $feed_title = "?";
                        }
 
-                       if ($feed < -10) error_reporting (0);
-
                        $content_query_part = "content as content_preview,";
 
                        if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
 } */
                                }
 
-                               $query = "SELECT 
+                               $query = "SELECT DISTINCT 
                                                guid,
                                                ttrss_entries.id,ttrss_entries.title,
                                                updated,
 
                                        $mail = new PHPMailer();
 
-                                       $mail->PluginDir = "phpmailer/";
-                                       $mail->SetLanguage("en", "phpmailer/language/");
+                                       $mail->PluginDir = "lib/phpmailer/";
+                                       $mail->SetLanguage("en", "lib/phpmailer/language/");
 
                                        $mail->CharSet = "UTF-8";
 
 
        function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
 
-               require_once "MiniTemplator.class.php";
+               require_once "lib/MiniTemplator.class.php";
 
                $tpl = new MiniTemplator;
                $tpl_t = new MiniTemplator;
                return array($tmp, $headlines_count, $affected_ids, $tmp_t);
        }
 
-       function check_for_update($link, $brief_fmt = true) {
+       function check_for_update($link) {
                $releases_feed = "http://tt-rss.org/releases.rss";
 
                if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
                        }
 
                        if (version_compare(VERSION, $latest_version) == -1) {
-                               if ($brief_fmt) {
-                                       return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">      
-                                               New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
-                                               <div id=\"milestoneDetails\">$content</div>");
-                               } else {
-                                       return "New version of Tiny-Tiny RSS ($latest_version) is available:
-                                               <div class='milestoneDetails'>$content</div>
-                                               Visit <a target=\"_blank\" href=\"http://tt-rss.org/\">official site</a> for
-                                               download and update information.";      
-                               }
-
-                       }                       
+                               return sprintf("New version of Tiny-Tiny RSS (%s) is available:", 
+                                       $latest_version)."<div class='milestoneDetails'>$content</div>";
+                       } else {
+                               return false;
+                       }       
                }
        }
 
        }
 
        function print_headline_subtoolbar($link, $feed_site_url, $feed_title, 
-                       $bottom = false, $rtl_content = false, $feed_id = 0,
-                       $is_cat = false, $search = false, $match_on = false,
-                       $search_mode = false, $offset = 0, $limit = 0, 
-                       $dashboard_menu = 0, $disable_feed = 0, $feed_small_icon = 0) {
-
-                       $user_page_offset = $offset + 1;
-
-                       if (!$bottom) {
-                               $class = "headlinesSubToolbar";
-                               $tid = "headlineActionsTop";
-                       } else {
-                               $class = "headlinesSubToolbar";
-                               $tid = "headlineActionsBottom";
-                       }
-
-                       print "<nobr><table class=\"$class\" id=\"$tid\"
-                               width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
+                       $feed_id, $is_cat, $search, $match_on,
+                       $search_mode) {
 
-                       if ($rtl_content) {
-                               $rtl_cpart = "RTL";
-                       } else {
-                               $rtl_cpart = "";
-                       }
+                       print "<div class=\"headlinesSubToolbar\">";
 
                        $page_prev_link = "javascript:viewFeedGoPage(-1)";
                        $page_next_link = "javascript:viewFeedGoPage(1)";
 
                        }
 
-                               if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
-
-                                       print "<td class=\"headlineActions$rtl_cpart\">
-                                               <ul class=\"headlineDropdownMenu\">
-                                               <li class=\"top2\">
-                                               ".__('Select:')."
-                                                       <a href=\"$sel_all_link\">".__('All')."</a>,
-                                                       <a href=\"$sel_unread_link\">".__('Unread')."</a>,
-                                                       <a href=\"$sel_inv_link\">".__('Invert')."</a>,
-                                                       <a href=\"$sel_none_link\">".__('None')."</a></li>
-                                               <li class=\"vsep\">&nbsp;</li>
-                                               <li class=\"top\">".__('Actions...')."<ul>
-                                                       <li><span class=\"insensitive\">".__('Selection toggle:')."</span></li>
-                                                       <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
-                                                       <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
-                                                       <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
-                                                       <!-- <li><span class=\"insensitive\">--------</span></li> -->
-                                                       <li><span class=\"insensitive\">".__('Mark as read:')."</span></li>
-                                                       <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
-
-/*                             if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
-       
-                                       print "
-                                               <li onclick=\"catchupRelativeToArticle(0)\">&nbsp;&nbsp;".__("Above active article")."</li>
-                                               <li onclick=\"catchupRelativeToArticle(1)\">&nbsp;&nbsp;".__("Below active article")."</li>";
-                               } else {
-                                       print "
-                                               <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Above active article")."</span></li>
-                                               <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Below active article")."</span></li>";
-
-                               } */
-
-                               print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed')."</li>";
-
-                               //print "<li><span class=\"insensitive\">--------</span></li>";
-                               print "<li><span class=\"insensitive\">".__('Assign label:')."</span></li>";
-
-                               $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
-                                       owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
-
-                               while ($line = db_fetch_assoc($result)) {
-
-                                       $label_id = $line["id"];
-                                       $label_caption = $line["caption"];
-
-                                       if ($feed_id < -10 && $feed_id == -11-$label_id) {
-                                               print "<li onclick=\"javascript:selectionRemoveLabel($label_id)\">
-                                                       &nbsp;&nbsp;$label_caption ".__('(remove)')."</li>";
-                                       } else {                                        
-                                               print "<li onclick=\"javascript:selectionAssignLabel($label_id)\">
-                                                       &nbsp;&nbsp;$label_caption</li>";
-                                       }
-                       }
-
-                               print   "</ul></li></ul>";
-                               print "</td>"; 
-       
-                               } else {
-                                       // old style subtoolbar:
-       
-                                       print "<td class=\"headlineActions$rtl_cpart\">".
-                                               __('Select:')."
-                                                                       <a href=\"$sel_all_link\">".__('All')."</a>,
-                                                                       <a href=\"$sel_unread_link\">".__('Unread')."</a>,
-                                                                       <a href=\"$sel_none_link\">".__('None')."</a>
-                                                       &nbsp;&nbsp;".
-                                                       __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
-                                                               <a href=\"$tog_marked_link\">".__('Starred')."</a>
-                                                       &nbsp;&nbsp;".
-                                                       __('Mark as read:')."
-                                                               <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
-                                                               <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
-
-                                       print "</td>";  
-       
-                               }
-
-                       print "<td class=\"headlineTitle$rtl_cpart\">";
-
-                       print "<span id=\"subtoolbar_search\" 
-                               style=\"display : none\"><input 
-                               id=\"subtoolbar_search_box\"
-                               onblur=\"javascript:enableHotkeys();\" 
-                               onfocus=\"javascript:disableHotkeys();\"
-                               onchange=\"subtoolbarSearch()\"
-                               onkeyup=\"subtoolbarSearch()\" type=\"search\"></span>";
-
-                       print "<span id=\"subtoolbar_ftitle\">";
+                       print "<div id=\"subtoolbar_ftitle\">";
 
                        if ($feed_site_url) {
                                if (!$bottom) {
                                $search_q = "&q=$search&m=$match_on&smode=$search_mode";
                        }
 
-                       if ($user_page_offset > 1) {
-                               print " [$user_page_offset] ";
-                       }
+                       print "
+                               <a target=\"_blank\" 
+                                       href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
+                                       <img class=\"noborder\" 
+                                               alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
+                                               </a>";
 
-                       if (!$bottom && !$disable_feed) {
-                               print "
-                                       <a target=\"_blank\" 
-                                               href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
-                                               <img class=\"noborder\" 
-                                                       alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
-                                       </a>";
-                       } else if ($feed_small_icon) {
-                               print "<img class=\"noborder\" alt=\"\" src=\"images/$feed_small_icon\">";
-                       }
+                       print "</div>";
 
-                       print "</span>";
+                       print __('Select:')."
+                               <a href=\"$sel_all_link\">".__('All')."</a>,
+                               <a href=\"$sel_unread_link\">".__('Unread')."</a>,
+                               <a href=\"$sel_inv_link\">".__('Invert')."</a>,
+                               <a href=\"$sel_none_link\">".__('None')."</a></li>";
+
+                       print "&nbsp;&nbsp;";
+
+                       print "<span 
+                               onmouseover=\"enable_selection(false)\" 
+            onmouseout=\"enable_selection(true)\"
+                               onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">".
+                               __("Actions...") . "&nbsp;&nbsp;<img src=\"images/down_arrow.png\">
+                               </span>";
+
+                       print "<ul id=\"headlineActionsBody\" style=\"display : none\">";
+
+                       print "<li class=\"insensitive\">".__('Selection toggle:')."</li>
+                               <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
+                               <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
+                               <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
+                               <!-- <li><span class=\"insensitive\">--------</span></li> -->
+                               <li class=\"insensitive\">".__('Mark as read:')."</li>
+                               <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
+
+                       print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed').
+                               "</li>";
 
-                       print "</td>";
-                       print "</tr></table></nobr>";
+                       //print "<li><span class=\"insensitive\">--------</span></li>";
+                       print "<li class=\"insensitive\">".__('Assign label:')."</li>";
 
+                       print_labels_headlines_dropdown($link, $feed_id);
+
+                       print "</ul>";
+
+                       print "</div>";
                }
 
        function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
 
                        if ($cat_id > 0) {
                                $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true);
-                       } else {
+                       } else if ($cat_id == 0 || $cat_id == -2) {
                                $cat_unread = getCategoryUnread($link, $cat_id);
                        }
 
                                $inner_title_class = "catTitleNL";
                        }
 
-                       if ($cat_id >= 0) {
-                               $cat_class = "feedCat";
-                       } else {
-                               $cat_class = "virtCat";
-                       }
+                       $cat_class = "feedCat";
 
                        print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\">
                                <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\"
                $num_starred = getFeedUnread($link, -1);
                $num_published = getFeedUnread($link, -2);
                $num_fresh = getFeedUnread($link, -3);
+               $num_total = getFeedUnread($link, -4);
+
+               $class = "virt";
+
+               if ($num_total > 0) $class .= "Unread";
+
+               printFeedEntry(-4, $class, __("All articles"), $num_total, 
+                       "images/tag.png", $link);
 
                $class = "virt";
 
                if (!$tags) {
 
 
-                               $result = db_query($link, "SELECT id,caption FROM                                       
+                               $result = db_query($link, "SELECT * FROM
                                        ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption");
                
                                if (db_num_rows($result) > 0) {
                                                        $cat_hidden = false;
                                                }
 
-                                               printCategoryHeader($link, -2, $cat_hidden, false);
+                                               printCategoryHeader($link, -2, $cat_hidden, true);
 
                                        } else {
                                                print "<li><hr></li>";
        
                                        printFeedEntry($label_id, 
                                                $class, $line["caption"], 
-                                               $count, "images/label.png", $link);
+                                               $count, "images/label.png", $link, 
+                                               false, false, false,
+                                               $line['fg_color'], $line['bg_color']);
                
                                }
 
                                ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
                                cat_id,last_error,
                                ttrss_feed_categories.title AS category,
-                               ttrss_feed_categories.collapsed 
-                               FROM ttrss_feeds LEFT JOIN ttrss_feed_categories 
-                                       ON (ttrss_feed_categories.id = cat_id)                          
+                               ttrss_feed_categories.collapsed,
+                               value AS unread 
+                               FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
+                                       ON (ttrss_feed_categories.id = cat_id)                  
+                               LEFT JOIN ttrss_counters_cache 
+                                       ON
+                                               (ttrss_feeds.id = feed_id)
                                WHERE 
                                        ttrss_feeds.hidden = false AND
                                        ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
                                if (!$feed) $feed = "[Untitled]";
 
                                $feed_id = $line["id"];   
+                               $unread = $line["unread"];
        
                                $subop = $_GET["subop"];
-                               
-                               $unread = ccache_find($link, $feed_id, $_SESSION["uid"]);
 
                                if (get_pref($link, 'HEADLINES_SMART_DATE')) {
                                        $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
                                $feed_icon = "&nbsp;";
                        }
 
-/*                     if ($line["comments"] && $line["link"] != $line["comments"]) {
-                               $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
-                       } else {
-                               $entry_comments = "";
-                       } */
-
                        $num_comments = $line["num_comments"];
                        $entry_comments = "";
 
                                print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
-/*                     $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
-                               ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
-                               ORDER BY tag_name"); */
-
-                       $tags = get_article_tags($link, $id);
-       
-                       $tags_str = "";
-                       $tags_nolinks_str = "";
-                       $f_tags_str = "";
-
-                       $num_tags = 0;
-
-                       if ($_SESSION["theme"] == "3pane") {
-                               $tag_limit = 3;
-                       } else {
-                               $tag_limit = 6;
-                       }
-
-                       foreach ($tags as $tag) {
-                               $num_tags++;
-                               $tag_escaped = str_replace("'", "\\'", $tag);
-
-                               $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
-                               
-                               if ($num_tags == $tag_limit) {
-                                       $tags_str .= "&hellip;";
-                                       $tags_nolinks_str .= "&hellip;";
-
-                               } else if ($num_tags < $tag_limit) {
-                                       $tags_str .= $tag_str;
-                                       $tags_nolinks_str .= "$tag, ";
-                               }
-                               $f_tags_str .= $tag_str;
-                       }
-
-                       $tags_str = preg_replace("/, $/", "", $tags_str);
-                       $tags_nolinks_str = preg_replace("/, $/", "", $tags_nolinks_str);
-                       $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
-
-                       $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
-                       $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
+                       $tags_str = format_tags_string(get_article_tags($link, $id), $id);
 
                        if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
 
-                       if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
-                       if (!$tags_nolinks_str) $tags_nolinks_str = '<span class="tagList">'.__('no tags').'</span>';
-
                        print "<div style='float : right'>
                                        <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
 
                        if (!$zoom_mode) {
-                               print "$tags_str 
+                               print "<span id=\"ATSTR-$id\">$tags_str</span>
                                        <a title=\"".__('Edit tags for this article')."\" 
                                        href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a>";
 
                                                style=\"cursor : pointer\" style=\"cursor : pointer\"
                                                onclick=\"zoomToArticle($id)\"
                                                alt='Zoom' title='".__('Show article summary in new window')."'>";
-                       } else {
-                               print "$tags_nolinks_str";
                        }
                        print "</div>";
                        print "<div clear='both'>$entry_comments</div>";
                        print "</div>";
 
                        print "<div class=\"postIcon\">" . $feed_icon . "</div>";
+
                        print "<div class=\"postContent\">";
-                       
-                       #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
 
                        $article_content = sanitize_rss($link, $line["content"]);
 
                                return;
                        }
 
-                       print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, 
-                               $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, 
-                               $offset, $limit);
+                       print_headline_subtoolbar($link, $feed_site_url, $feed_title,
+                               $feed, $cat_view, $search, $match_on, $search_mode);
 
                        print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
                }
                                $id = $line["id"];
                                $feed_id = $line["feed_id"];
 
+                               $labels = get_article_labels($link, $id);
+
+                               $labels_str = "<span id=\"HLLCTR-$id\">";
+                               $labels_str .= format_article_labels($labels, $id);
+                               $labels_str .= "</span>";
+       
                                if (count($topmost_article_ids) < 5) {
                                        array_push($topmost_article_ids, $id);
                                }
 
                                $score_pic = get_score_pic($score);
 
-                               $score_title = __("(Click to change)");
+/*                             $score_title = __("(Click to change)");
+                               $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" 
+                                       onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
 
                                $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" 
-                                       onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">";
+                                       title=\"$score\">";
 
                                if ($score > 500) {
                                        $hlc_suffix = "H";
 
                                        print "</a>";
 
+                                       print $labels_str;
+
 #                                                      <a href=\"javascript:viewfeed($feed_id, '', false)\">".
 #                                                      $line["feed_title"]."</a>       
 
                                                        (".__('Show article').")</a>";
                                        } */
 
+                                       print $labels_str;
 
                                        if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
                                                if ($line["feed_title"]) {      
                                                alt='Zoom' 
                                                title='".__('Show article summary in new window')."'></span>";
 
-                                       $tags = get_article_tags($link, $id);
-
-                                       $tags_str = "";
-                                       $full_tags_str = "";
-                                       $num_tags = 0;
-
-                                       foreach ($tags as $tag) {
-                                               $num_tags++;
-                                               $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; 
-                                               if ($num_tags < 5) {
-                                                       $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; 
-                                               } else if ($num_tags == 5) {
-                                                       $tags_str .= "&hellip;";
-                                               }
-                                       }
-
-                                       $tags_str = preg_replace("/, $/", "", $tags_str);
-                                       $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
-
-                                       $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
-
-                                       $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
-
-
-                                       if ($tags_str == "") $tags_str = "no tags";
+                                       $tags_str = format_tags_string(get_article_tags($link, $id), $id);
 
 //                                     print "<img src='images/tag.png' class='markedPic'>";
 
                                        print "<span class='s1'>
-                                               <img class='tagsPic' src='images/tag.png' alt='Tags' 
-                                                       title='Tags'> $tags_str <a title=\"Edit tags for this article\" 
-                                                       href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
+                                               <img class='tagsPic' src='images/tag.png' alt='Tags' title='Tags'>
+                                               <span id=\"ATSTR-$id\">$tags_str</span>
+                                               <a title=\"".__('Edit tags for this article')."\" 
+                                               href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
 
                                        print "</span>";
 
                                print "</table>";
                        }
 
-//                     print_headline_subtoolbar($link, 
-//                             "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
-
-
                } else {
                        $message = "";
 
                                case "unread":
                                        $message = __("No unread articles found to display.");
                                        break;
+                               case "updated":
+                                       $message = __("No updated articles found to display.");
+                                       break;
                                case "marked":
                                        $message = __("No starred articles found to display.");
                                        break;
                                default:
-                                       $message = __("No articles found to display.");
+                                       if ($feed < -10) {
+                                               $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
+                                       } else {
+                                               $message = __("No articles found to display.");
+                                       }
                        }
 
                        if (!$offset) print "<div class='whiteBox'>$message</div>";
 
        function printTagCloud($link) {
 
-               /* get first ref_id to count from */
-
-               /*
-
-               $query = "";
-
-               if (DB_TYPE == "pgsql") {
-                       $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries 
-                               WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
-                               AND date_entered > NOW() - INTERVAL '30 days'";
-               } else {
-                       $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries 
-                               WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." 
-                               AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
-               }
-
-               $result = db_query($link, $query);
-               $first_id = db_fetch_result($result, 0, "id"); */
-
-               //AND post_int_id >= '$first_id'
                $query = "SELECT tag_name, COUNT(post_int_id) AS count 
                        FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." 
                        GROUP BY tag_name ORDER BY count DESC LIMIT 50";
                        sleep(1); // prevent flood (FIXME make this an option?)
                }
 
-       // Send feed digests by email if needed.
-       if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
+               // Send feed digests by email if needed.
+               if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
+
+               purge_orphans($link);
 
        } // function update_daemon_common
 
                print "</table>";
        }
 
-       function print_label_dlg_common_examples() {
-
-               print __("Match ") . " ";
-
-/*             print "<select name=\"label_andor\">";
-               print "<option value=\"and\">AND</option>";
-               print "<option value=\"or\">OR</option>";
-               print "</select>"; */
-
-               print "<select name=\"label_fields\" onchange=\"labelFieldsCheck(this)\">";
-               print "<option value=\"unread\">".__("Unread articles")."</option>";
-               print "<option value=\"updated\">".__("Updated articles")."</option>";
-               print "<option value=\"kw_title\">".__("Title contains")."</option>";
-               print "<option value=\"kw_content\">".__("Content contains")."</option>";
-               print "<option value=\"scoreE\">".__("Score equals")."</option>";
-               print "<option value=\"scoreG\">".__("Score is greater than")."</option>";
-               print "<option value=\"scoreL\">".__("Score is less than")."</option>";
-               print "<option value=\"newerH\">".__("Articles newer than X hours")."</option>";
-               print "<option value=\"newerD\">".__("Articles newer than X days")."</option>";
-
-               print "</select>";
-
-               print "<input style=\"display : none\" name=\"label_fields_param\"
-                       size=\"10\">";
-
-               print " <input type=\"submit\" 
-                       onclick=\"return addLabelExample()\"
-                       value=\"".__("Add")."\">";
-       }
-
        function feed_has_icon($id) {
                return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
        }
 
        function update_feedbrowser_cache($link) {
 
-               $result = db_query($link, "SELECT feed_url,COUNT(id) AS subscribers
+               $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers
                        FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf 
                                WHERE tf.feed_url = ttrss_feeds.feed_url 
                                AND (private IS true OR feed_url LIKE '%:%@%/%')) 
-                               GROUP BY feed_url ORDER BY subscribers DESC LIMIT 200");
+                               GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000");
        
                db_query($link, "BEGIN");
        
                while ($line = db_fetch_assoc($result)) {
                        $subscribers = db_escape_string($line["subscribers"]);
                        $feed_url = db_escape_string($line["feed_url"]);
+                       $title = db_escape_string($line["title"]);
+
+                       $tmp_result = db_query($link, "SELECT subscribers FROM
+                               ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
+
+                       if (db_num_rows($tmp_result) == 0) {
+
+                               db_query($link, "INSERT INTO ttrss_feedbrowser_cache 
+                                       (feed_url, title, subscribers) VALUES ('$feed_url', 
+                                               '$title', '$subscribers')");
+
+                               ++$count;
+
+                       }
        
-                       db_query($link, "INSERT INTO ttrss_feedbrowser_cache 
-                               (feed_url, subscribers) VALUES ('$feed_url', '$subscribers')");
-       
-                       ++$count;
                }
        
                db_query($link, "COMMIT");
        function ccache_find($link, $feed_id, $owner_uid, $is_cat = false, 
                $no_update = false) {
 
+               if (!is_numeric($feed_id)) return;
+
                if (!$is_cat) {
                        $table = "ttrss_counters_cache";
                } else {
        function ccache_update($link, $feed_id, $owner_uid, $is_cat = false, 
                $update_pcat = true) {
 
+               if (!is_numeric($feed_id)) return;
+
                $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
 
                /* When updating a label, all we need to do is recalculate feed counters
                }
        }
 
+       function get_article_labels($link, $id) {
+               $result = db_query($link, 
+                       "SELECT DISTINCT label_id,caption,fg_color,bg_color 
+                               FROM ttrss_labels2, ttrss_user_labels2 
+                       WHERE id = label_id 
+                               AND article_id = '$id' 
+                               AND owner_uid = ".$_SESSION["uid"] . "
+                       ORDER BY caption");
+
+               $rv = array();
+
+               while ($line = db_fetch_assoc($result)) {
+                       $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
+                               $line["bg_color"]);
+                       array_push($rv, $rk);
+               }
+
+               return $rv;
+       }
+
+
        function label_find_caption($link, $label, $owner_uid) {
                $result = db_query($link, 
                        "SELECT caption FROM ttrss_labels2 WHERE id = '$label' 
                                (label_id, article_id) VALUES ('$label_id', '$id')");
                }
        }
+
+       function label_remove($link, $id, $owner_uid) {
+
+               db_query($link, "BEGIN");
+
+               $result = db_query($link, "SELECT caption FROM ttrss_labels2
+                       WHERE id = '$id'");
+
+               $caption = db_fetch_result($result, 0, "caption");
+
+               $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
+                       AND owner_uid = " . $_SESSION["uid"]);
+
+               if (db_affected_rows($link, $result) != 0 && $caption) {
+
+                       /* Disable filters that reference label being removed */
+       
+                       db_query($link, "UPDATE ttrss_filters SET
+                               enabled = false WHERE action_param = '$caption'
+                                       AND action_id = 7
+                                       AND owner_uid = " . $_SESSION["uid"]);
+                       }
+
+               db_query($link, "COMMIT");
+       }
+
+       function label_create($link, $caption) {
+
+               db_query($link, "BEGIN");
+
+               $result = false;
+
+               $result = db_query($link, "SELECT id FROM ttrss_labels2
+                       WHERE caption = '$caption' AND owner_uid =  ". $_SESSION["uid"]);
+
+               if (db_num_rows($result) == 0) {
+                       $result = db_query($link,
+                               "INSERT INTO ttrss_labels2 (caption,owner_uid) 
+                                       VALUES ('$caption', '".$_SESSION["uid"]."')");
+
+                       $result = db_affected_rows($link, $result) != 0;
+               }
+
+               db_query($link, "COMMIT");
+
+               return $result;
+       }
+
+       function print_labels_headlines_dropdown($link, $feed_id) {
+               print "<li onclick=\"javascript:addLabel()\">
+                       &nbsp;&nbsp;".__("Create label...")."</li>";
+
+               $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
+                       owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
+
+               while ($line = db_fetch_assoc($result)) {
+
+                       $label_id = $line["id"];
+                       $label_caption = $line["caption"];
+
+                       if ($feed_id < -10 && $feed_id == -11-$label_id) {
+                               print "<li id=\"LHDL-$id\" 
+                                       onclick=\"javascript:selectionRemoveLabel($label_id)\">
+                                       &nbsp;&nbsp;$label_caption ".__('(remove)')."</li>";
+                       } else {                                        
+                               print "<li id=\"LHDL-$id\" 
+                                       onclick=\"javascript:selectionAssignLabel($label_id)\">
+                                       &nbsp;&nbsp;$label_caption</li>";
+                       }
+               }
+       }
+
+       function format_tags_string($tags, $id) {
+
+               $tags_str = "";
+               $tags_nolinks_str = "";
+
+               $num_tags = 0;
+
+               if ($_SESSION["theme"] == "3pane") {
+                       $tag_limit = 3;
+               } else {
+                       $tag_limit = 6;
+               }
+
+               $formatted_tags = array();
+
+               foreach ($tags as $tag) {
+                       $num_tags++;
+                       $tag_escaped = str_replace("'", "\\'", $tag);
+
+                       $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>";
+
+                       array_push($formatted_tags, $tag_str);
+                               
+                       if ($num_tags == $tag_limit) {
+                               break;
+                       }
+               }
+
+               $tags_str = implode(", ", $formatted_tags);
+
+               if ($num_tags < count($tags)) {
+                       $tags_str .= ", &hellip;";
+               }
+
+               if ($num_tags == 0) {
+                       $tags_str = __("no tags");
+               }
+
+               return $tags_str;
+
+       }
+
+       function format_article_labels($labels, $id) {
+
+               $labels_str = "";
+
+               foreach ($labels as $l) {
+                       $labels_str .= sprintf("<span class='hlLabelRef' 
+                               style='color : %s; background-color : %s'>%s</span>",
+                                       $l[2], $l[3], $l[1]);
+                       }
+
+               return $labels_str;
+
+       }
 ?>