]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
add index on ttrss_entries(date_entered), bump schema
[tt-rss.git] / functions.php
index 8157d12d2cf3f76179377d774c4e7109f7053afb..f3f82d312f27e398439d9bf7c55f0920da54cef9 100644 (file)
@@ -8,6 +8,17 @@
 
        require_once 'config.php';
 
+       function get_translations() {
+               $tr = array(
+                                       "auto"  => "Detect automatically",
+                                       "en_US" => "English",
+                                       "fr_FR" => "Français",
+                                       "ru_RU" => "Русский",
+                                       "zh_CN" => "Simplified Chinese");
+
+               return $tr;
+       }
+
        if (ENABLE_TRANSLATIONS == true) { 
                require_once "accept-to-gettext.php";
                require_once "gettext/gettext.inc";
                function startup_gettext() {
        
                        # Get locale from Accept-Language header
-                       $lang = al2gt(array("en_US", "ru_RU"), "text/html");
-       
+                       $lang = al2gt(array_keys(get_translations()), "text/html");
+
+                       if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
+                               $lang = _TRANSLATION_OVERRIDE_DEFAULT;
+                       }
+
+                       if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {                               
+                               $lang = $_COOKIE["ttrss_lang"];
+                       }
+
                        if ($lang) {
                                _setlocale(LC_MESSAGES, $lang);
                                _bindtextdomain("messages", "locale");
        define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
 
-       require_once "magpierss/rss_fetch.inc";
-       require_once 'magpierss/rss_utils.inc';
+       if (ENABLE_SIMPLEPIE) {
+               require_once "simplepie/simplepie.inc";
+       } else {
+               require_once "magpierss/rss_fetch.inc";
+               require_once 'magpierss/rss_utils.inc';
+       }
 
        include_once "tw/tw-config.php";
        include_once "tw/tw.php";
                }
 
                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
-                       _debug("update_rss_feed: fetching...");
+                       _debug("update_rss_feed: fetching [$fetch_url]...");
                }
 
-               if (!defined('DAEMON_EXTENDED_DEBUG')) {
+               if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
                        error_reporting(0);
                }
 
-               $rss = fetch_rss($fetch_url);
+               if (!ENABLE_SIMPLEPIE) {
+                       $rss = fetch_rss($fetch_url);
+               } else {
+                       $rss = new SimplePie();
+                       $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
+                       $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
+                       $rss->set_feed_url($fetch_url);
+                       $rss->set_output_encoding('UTF-8');
+                       $rss->init();
+               }
+
+//             print_r($rss);
 
                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                        _debug("update_rss_feed: fetch done, parsing...");
 
                $feed = db_escape_string($feed);
 
-               if ($rss) {
+               if (ENABLE_SIMPLEPIE) {
+                       $fetch_ok = !$rss->error();
+               } else {
+                       $fetch_ok = !!$rss;
+               }
+
+               if ($fetch_ok) {
 
                        if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                _debug("update_rss_feed: processing feed data...");
 
                        $owner_uid = db_fetch_result($result, 0, "owner_uid");
 
+                       if (ENABLE_SIMPLEPIE) {
+                               $site_url = $rss->get_link();
+                       } else {
+                               $site_url = $rss->channel["link"];
+                       }
+
                        if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {  
                                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: checking favicon...");
                                }
-                               check_feed_favicon($rss->channel["link"], $feed, $link);
+
+                               check_feed_favicon($site_url, $feed, $link);
                        }
 
                        if (!$registered_title || $registered_title == "[Unknown]") {
 
-                               $feed_title = db_escape_string($rss->channel["title"]);
+                               if (ENABLE_SIMPLEPIE) {
+                                       $feed_title = $rss->get_title();
+                               } else {
+                                       $feed_title = db_escape_string($rss->channel["title"]);
+                               }
 
                                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: registering title: $feed_title");
                                        title = '$feed_title' WHERE id = '$feed'");
                        }
 
-                       $site_url = $rss->channel["link"];
                        // weird, weird Magpie
-                       if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
+                       if (!ENABLE_SIMPLEPIE) {
+                               if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
+                       }
 
                        if ($site_url && $orig_site_url != db_escape_string($site_url)) {
                                db_query($link, "UPDATE ttrss_feeds SET 
 
 //                     print "I: " . $rss->channel["image"]["url"];
 
-                       $icon_url = $rss->image["url"];
+                       if (!ENABLE_SIMPLEPIE) {
+                               $icon_url = $rss->image["url"];
+                       } else {
+                               $icon_url = $rss->get_image_url();
+                       }
 
                        if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
                                $icon_url = db_escape_string($icon_url);
                                db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG' || $_GET['xdebug'])) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                _debug("update_rss_feed: loading filters...");
                        }
 
                                array_push($filters[$line["name"]], $filter);
                        }
 
-                       $iterator = $rss->items;
-
-                       if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
-                       if (!$iterator || !is_array($iterator)) $iterator = $rss;
+                       if (ENABLE_SIMPLEPIE) {
+                               $iterator = $rss->get_items();
+                       } else {
+                               $iterator = $rss->items;
+                               if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
+                               if (!$iterator || !is_array($iterator)) $iterator = $rss;
+                       }
 
                        if (!is_array($iterator)) {
                                /* db_query($link, "UPDATE ttrss_feeds 
                                // clear any errors and mark feed as updated if fetched okay
                                // even if it's blank
 
-                               if (defined('DAEMON_EXTENDED_DEBUG')) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: entry iterator is not an array, no articles?");
                                }
 
                                return; // no articles
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG')) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                _debug("update_rss_feed: processing articles...");
                        }
 
                        foreach ($iterator as $item) {
 
-                               $entry_guid = $item["id"];
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_guid = $item->get_id();
+                                       if (!$entry_guid) $entry_guid = $item->get_link();
+                                       if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
+
+                               } else {
 
-                               if (!$entry_guid) $entry_guid = $item["guid"];
-                               if (!$entry_guid) $entry_guid = $item["link"];
-                               if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
+                                       $entry_guid = $item["id"];
 
-                               if (defined('DAEMON_EXTENDED_DEBUG')) {
+                                       if (!$entry_guid) $entry_guid = $item["guid"];
+                                       if (!$entry_guid) $entry_guid = $item["link"];
+                                       if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
+                               }
+
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: guid $entry_guid");
                                }
 
 
                                $entry_timestamp = "";
 
-                               $rss_2_date = $item['pubdate'];
-                               $rss_1_date = $item['dc']['date'];
-                               $atom_date = $item['issued'];
-                               if (!$atom_date) $atom_date = $item['updated'];
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_timestamp = strtotime($item->get_date());
+                               } else {
+                                       $rss_2_date = $item['pubdate'];
+                                       $rss_1_date = $item['dc']['date'];
+                                       $atom_date = $item['issued'];
+                                       if (!$atom_date) $atom_date = $item['updated'];
                        
-                               if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
-                               if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
-                               if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
-                               
+                                       if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
+                                       if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
+                                       if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
+                               }
+
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: date $entry_timestamp");
+                               }
+
                                if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
                                        $entry_timestamp = time();
                                        $no_orig_date = 'true';
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               $entry_title = trim(strip_tags($item["title"]));
-
-                               // strange Magpie workaround
-                               $entry_link = $item["link_"];
-                               if (!$entry_link) $entry_link = $item["link"];
-
-                               if (!$entry_title) continue;
-#                                      if (!$entry_link) continue;
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_title = $item->get_title();
+                               } else {
+                                       $entry_title = trim(strip_tags($item["title"]));
+                               }
 
-                               $entry_link = strip_tags($entry_link);
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_link = $item->get_link();
+                               } else {
+                                       // strange Magpie workaround
+                                       $entry_link = $item["link_"];
+                                       if (!$entry_link) $entry_link = $item["link"];
+                               }
 
-                               $entry_content = $item["content:escaped"];
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: title $entry_title");
+                               }
 
-                               if (!$entry_content) $entry_content = $item["content:encoded"];
-                               if (!$entry_content) $entry_content = $item["content"];
-                               if (!$entry_content) $entry_content = $item["atom_content"];
-                               if (!$entry_content) $entry_content = $item["summary"];
-                               if (!$entry_content) $entry_content = $item["description"];
+                               if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
 
-//                             if (!$entry_content) continue;
+                               $entry_link = strip_tags($entry_link);
 
-                               // WTF
-                               if (is_array($entry_content)) {
-                                       $entry_content = $entry_content["encoded"];
-                                       if (!$entry_content) $entry_content = $entry_content["escaped"];
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_content = $item->get_description();
+                               } else {
+                                       $entry_content = $item["content:escaped"];
+
+                                       if (!$entry_content) $entry_content = $item["content:encoded"];
+                                       if (!$entry_content) $entry_content = $item["content"];
+                                       if (!$entry_content) $entry_content = $item["atom_content"];
+                                       if (!$entry_content) $entry_content = $item["summary"];
+                                       if (!$entry_content) $entry_content = $item["description"];
+
+                                       // WTF
+                                       if (is_array($entry_content)) {
+                                               $entry_content = $entry_content["encoded"];
+                                               if (!$entry_content) $entry_content = $entry_content["escaped"];
+                                       }
                                }
 
 //                             print_r($item);
 
                                $entry_content_unescaped = $entry_content;
 
-                               $entry_comments = strip_tags($item["comments"]);
-
-                               $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
-
-                               if ($item['author']) {
-
-                                       if (is_array($item['author'])) {
+                               if (ENABLE_SIMPLEPIE) {
+                                       $entry_comments = strip_tags($item->data["comments"]);
+                                       if ($item->get_author()) {
+                                               $entry_author = $item->get_author()->get_name();
+                                       }
+                               } else {
+                                       $entry_comments = strip_tags($item["comments"]);
+                               
+                                       $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
 
-                                               if (!$entry_author) {
-                                                       $entry_author = db_escape_string(strip_tags($item['author']['name']));
+                                       if ($item['author']) {
+       
+                                               if (is_array($item['author'])) {
+       
+                                                       if (!$entry_author) {
+                                                               $entry_author = db_escape_string(strip_tags($item['author']['name']));
+                                                       }
+       
+                                                       if (!$entry_author) {
+                                                               $entry_author = db_escape_string(strip_tags($item['author']['email']));
+                                                       }
                                                }
-
+       
                                                if (!$entry_author) {
-                                                       $entry_author = db_escape_string(strip_tags($item['author']['email']));
+                                                       $entry_author = db_escape_string(strip_tags($item['author']));
                                                }
                                        }
-
-                                       if (!$entry_author) {
-                                               $entry_author = db_escape_string(strip_tags($item['author']));
-                                       }
                                }
 
                                if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
                                $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
                                $entry_author = mb_substr($entry_author, 0, 250);
 
-                               $num_comments = db_escape_string($item["slash"]["comments"]);
+                               if (ENABLE_SIMPLEPIE) {
+                                       $num_comments = 0; #FIXME#
+                               } else {
+                                       $num_comments = db_escape_string($item["slash"]["comments"]);
+                               }
 
                                if (!$num_comments) $num_comments = 0;
 
                                // parse <category> entries into tags
 
-                               $t_ctr = $item['category#'];
+                               if (ENABLE_SIMPLEPIE) {
 
-                               $additional_tags = false;
-
-                               if ($t_ctr == 0) {
-                                       $additional_tags = false;
-                               } else if ($t_ctr == 1) {
-                                       $additional_tags = array($item['category']);
-                               } else {
                                        $additional_tags = array();
-                                       for ($i = 0; $i <= $t_ctr; $i++ ) {
-                                               if ($item["category#$i"]) {
-                                                       array_push($additional_tags, $item["category#$i"]);
+                                       $additional_tags_src = $item->get_categories();
+
+                                       if (is_array($additional_tags_src)) {
+                                               foreach ($additional_tags_src as $tobj) {
+                                                       array_push($additional_tags, $tobj->get_term());
                                                }
                                        }
-                               }
 
-                               // parse <dc:subject> elements
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                               _debug("update_rss_feed: category tags:");
+                                               print_r($additional_tags);
+                                       }
+
+                               } else {
 
-                               $t_ctr = $item['dc']['subject#'];
+                                       $t_ctr = $item['category#'];
 
-                               if ($t_ctr == 1) {
-                                       $additional_tags = array($item['dc']['subject']);
-                               } else if ($t_ctr > 1) {
-                                       $additional_tags = array();
-                                       for ($i = 0; $i <= $t_ctr; $i++ ) {
-                                               if ($item['dc']["subject#$i"]) {
-                                                       array_push($additional_tags, $item['dc']["subject#$i"]);
+                                       $additional_tags = false;
+       
+                                       if ($t_ctr == 0) {
+                                               $additional_tags = false;
+                                       } else if ($t_ctr == 1) {
+                                               $additional_tags = array($item['category']);
+                                       } else {
+                                               $additional_tags = array();
+                                               for ($i = 0; $i <= $t_ctr; $i++ ) {
+                                                       if ($item["category#$i"]) {
+                                                               array_push($additional_tags, $item["category#$i"]);
+                                                       }
+                                               }
+                                       }
+       
+                                       // parse <dc:subject> elements
+       
+                                       $t_ctr = $item['dc']['subject#'];
+       
+                                       if ($t_ctr == 1) {
+                                               $additional_tags = array($item['dc']['subject']);
+                                       } else if ($t_ctr > 1) {
+                                               $additional_tags = array();
+                                               for ($i = 0; $i <= $t_ctr; $i++ ) {
+                                                       if ($item['dc']["subject#$i"]) {
+                                                               array_push($additional_tags, $item['dc']["subject#$i"]);
+                                                       }
                                                }
                                        }
                                }
                                
 //                             $entry_content = sanitize_rss($entry_content);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG')) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
                                }
 
 
                                if (db_num_rows($result) == 0) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG')) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                                _debug("update_rss_feed: base guid not found");
                                        }
 
 
                                if (db_num_rows($result) == 1) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG')) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                                _debug("update_rss_feed: base guid found, checking for user record");
                                        }
 
                                        $article_filters = get_article_filters($filters, $entry_title, 
                                                        $entry_content, $entry_link);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG')) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                                _debug("update_rss_feed: article filters: ");
                                                if (count($article_filters) != 0) {
                                                        print_r($article_filters);
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               if (defined('DAEMON_EXTENDED_DEBUG')) {
+                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                                        _debug("update_rss_feed: user record not found, creating...");
                                                }
 
                                                } else {
                                                        $marked = 'false';
                                                }
-                                               
+
+                                               if (find_article_filter($article_filters, 'publish')) {
+                                                       $published = 'true';
+                                               } else {
+                                                       $published = 'false';
+                                               }
+
                                                $result = db_query($link,
                                                        "INSERT INTO ttrss_user_entries 
-                                                               (ref_id, owner_uid, feed_id, unread, last_read, marked) 
+                                                               (ref_id, owner_uid, feed_id, unread, last_read, marked, published
                                                        VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
-                                                               $last_read_qpart, $marked)");
+                                                               $last_read_qpart, $marked, $published)");
                                        }
                                        
                                        $post_needs_update = false;
 
                                db_query($link, "COMMIT");
 
-                               if (defined('DAEMON_EXTENDED_DEBUG')) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: looking for tags...");
                                }
 
 
 //                             print "<p>TAGS: "; print_r($entry_tags); print "</p>";
 
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       print_r($entry_tags);
+                               }
+
                                if (count($entry_tags) > 0) {
                                
                                        db_query($link, "BEGIN");
                                        }
                                        db_query($link, "COMMIT");
                                } 
+
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       _debug("update_rss_feed: article processed");
+                               }
                        } 
 
                        db_query($link, "UPDATE ttrss_feeds 
 //                     db_query($link, "COMMIT");
 
                } else {
-                       $error_msg = db_escape_string(magpie_error());
+
+                       if (ENABLE_SIMPLEPIE) {
+                               $error_msg = mb_substr($rss->error(), 0, 250);
+                       } else {
+                               $error_msg = mb_substr(magpie_error(), 0, 250);
+                       }
+
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               _debug("update_rss_feed: error fetching feed: $error_msg");
+                       }
+
+                       $error_msg = db_escape_string($error_msg);
+
                        db_query($link, 
                                "UPDATE ttrss_feeds SET last_error = '$error_msg', 
                                        last_updated = NOW() WHERE id = '$feed'");
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG')) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                        _debug("update_rss_feed: done");
                }
 
                return ((float)$usec + (float)$sec);
        }
 
-       function print_radio($id, $default, $values, $attributes = "") {
+       function print_radio($id, $default, $true_is, $values, $attributes = "") {
                foreach ($values as $v) {
                
                        if ($v == $default)
                         else
                                $sel = "";
 
-                       if ($v == "Yes") {
+                       if ($v == $true_is) {
                                $sel .= " value=\"1\"";
                        } else {
                                $sel .= " value=\"0\"";
                                if (authenticate_user($link, $login, $password)) {
                                        $_POST["password"] = "";
 
+                                       $_SESSION["language"] = $_POST["language"];
+
                                        header("Location: " . $_SERVER["REQUEST_URI"]);
                                        exit;
 
                                /* bump login timestamp */
                                db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . 
                                        $_SESSION["uid"]);
+
+                               if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
+                                       setcookie("ttrss_lang", $_SESSION["language"], 
+                                               time() + SESSION_COOKIE_LIFETIME);
+                               }
                        }
 
                } else {
                                                        WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
                                        }
 
+                                       if ($feed == -3) {
+
+                                               $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
+
+                                               if (DB_TYPE == "pgsql") {
+                                                       $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; 
+                                               } else {
+                                                       $match_part .= " AND date_entered > DATE_SUB(NOW(), 
+                                                               INTERVAL $intl HOUR) ";
+                                               }
+
+                                               db_query($link, "UPDATE ttrss_user_entries 
+                                                       SET unread = false,last_read = NOW()
+                                                       WHERE $match_part AND owner_uid = ".$_SESSION["uid"]);
+                                       }
+
                                } else if ($feed < -10) { // label
 
                                        // TODO make this more efficient
                        }
        }
 
-       function getAllCounters($link, $omode = "tflc") {
+       function getAllCounters($link, $omode = "flc") {
 /*             getLabelCounters($link);
                getFeedCounters($link);
                getTagCounters($link);
                        getCategoryCounters($link);
                } */
 
-               if (!$omode) $omode = "tflc";
+               if (!$omode) $omode = "flc";
 
                getGlobalCounters($link);
 
        }       
 
        function getCategoryCounters($link) {
+               # two special categories are -1 and -2 (all virtuals; all labels)
+
+               $ctr = getCategoryUnread($link, -1);
+
+               print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
+
+               $ctr = getCategoryUnread($link, -2);
+
+               print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
+
+               $age_qpart = getMaxAgeSubquery();
+
                $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) 
-                               FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id 
+                               FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id 
+                                       AND id = ref_id AND $age_qpart 
                                        AND unread = true)) AS unread FROM ttrss_feeds 
                        WHERE 
                                hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
 
        function getCategoryUnread($link, $cat) {
 
-               if ($cat != 0) {
-                       $cat_query = "cat_id = '$cat'";
-               } else {
-                       $cat_query = "cat_id IS NULL";
-               }
+               if ($cat >= 0) {
 
-               $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query 
-                               AND hidden = false
-                               AND owner_uid = " . $_SESSION["uid"]);
-
-               $cat_feeds = array();
-               while ($line = db_fetch_assoc($result)) {
-                       array_push($cat_feeds, "feed_id = " . $line["id"]);
-               }
+                       if ($cat != 0) {
+                               $cat_query = "cat_id = '$cat'";
+                       } else {
+                               $cat_query = "cat_id IS NULL";
+                       }
 
-               if (count($cat_feeds) == 0) return 0;
+                       $age_qpart = getMaxAgeSubquery();
 
-               $match_part = implode(" OR ", $cat_feeds);
+                       $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query 
+                                       AND hidden = false
+                                       AND owner_uid = " . $_SESSION["uid"]);
+       
+                       $cat_feeds = array();
+                       while ($line = db_fetch_assoc($result)) {
+                               array_push($cat_feeds, "feed_id = " . $line["id"]);
+                       }
+       
+                       if (count($cat_feeds) == 0) return 0;
+       
+                       $match_part = implode(" OR ", $cat_feeds);
+       
+                       $result = db_query($link, "SELECT COUNT(int_id) AS unread 
+                               FROM ttrss_user_entries,ttrss_entries 
+                               WHERE   unread = true AND ($match_part) AND id = ref_id 
+                               AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
+       
+                       $unread = 0;
+       
+                       # this needs to be rewritten
+                       while ($line = db_fetch_assoc($result)) {
+                               $unread += $line["unread"];
+                       }
+       
+                       return $unread;
+               } else if ($cat == -1) {
+                       return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
+               } else if ($cat == -2) {
 
-               $result = db_query($link, "SELECT COUNT(int_id) AS unread 
-                       FROM ttrss_user_entries 
-                       WHERE   unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
+                       $rv = getLabelCounters($link, false, true);
+                       $ctr = 0;
 
-               $unread = 0;
+                       foreach (array_keys($rv) as $k) {
+                               if ($k < -10) {
+                                       $ctr += $rv[$k]["counter"];
+                               }
+                       }
 
-               # this needs to be rewritten
-               while ($line = db_fetch_assoc($result)) {
-                       $unread += $line["unread"];
+                       return $ctr;
                }
+       }
 
-               return $unread;
-
+       function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
+               if (DB_TYPE == "pgsql") {
+                       return "ttrss_entries.date_entered > 
+                               NOW() - INTERVAL '$days days'";
+               } else {
+                       return "ttrss_entries.date_entered > 
+                               DATE_SUB(NOW(), INTERVAL $days DAY)";
+               }
        }
 
        function getFeedUnread($link, $feed, $is_cat = false) {
                $n_feed = sprintf("%d", $feed);
 
+               $age_qpart = getMaxAgeSubquery();
+
                if ($is_cat) {
                        return getCategoryUnread($link, $n_feed);               
                } else if ($n_feed == -1) {
                        $match_part = "marked = true";
                } else if ($n_feed == -2) {
                        $match_part = "published = true";
+               } else if ($n_feed == -3) {
+                       $match_part = "unread = true";
+
+                       $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
+
+                       if (DB_TYPE == "pgsql") {
+                               $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; 
+                       } else {
+                               $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
+                       }
+
                } else if ($n_feed > 0) {
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds 
                                $match_part = implode(" OR ", $linked_feeds);
 
                                $result = db_query($link, "SELECT COUNT(int_id) AS unread 
-                                       FROM ttrss_user_entries
-                                       WHERE   unread = true AND ($match_part) 
-                                       AND owner_uid = " . $_SESSION["uid"]);
+                                       FROM ttrss_user_entries,ttrss_entries
+                                       WHERE   unread = true AND
+                                       ttrss_user_entries.ref_id = ttrss_entries.id AND
+                                       $age_qpart AND
+                                       ($match_part) AND
+                                       owner_uid = " . $_SESSION["uid"]);
 
                                $unread = 0;
 
                                ttrss_user_entries.feed_id = ttrss_feeds.id AND
                                ttrss_user_entries.ref_id = ttrss_entries.id AND 
                                ttrss_feeds.hidden = false AND
+                               $age_qpart AND
                                unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
                                
                } else {
                
                        $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
-                               FROM ttrss_tags,ttrss_user_entries 
-                               WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
+                               FROM ttrss_tags,ttrss_user_entries,ttrss_entries 
+                               WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = id 
+                               AND unread = true AND $age_qpart AND
                                        ttrss_tags.owner_uid = " . $_SESSION["uid"]);
                }
                
                        $user_id = $_SESSION["uid"];
                }
 
+               $age_qpart = getMaxAgeSubquery();
+
                $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
                        WHERE unread = true AND 
                        ttrss_user_entries.feed_id = ttrss_feeds.id AND
                        ttrss_user_entries.ref_id = ttrss_entries.id AND 
                        hidden = false AND
+                       $age_qpart AND
                        ttrss_user_entries.owner_uid = '$user_id'");
                $c_id = db_fetch_result($result, 0, "c_id");
                return $c_id;
                        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) 
-                       FROM ttrss_user_entries WHERE int_id = post_int_id 
+                       FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id 
+                               AND ref_id = id AND $age_qpart
                                AND unread = true)) AS count FROM ttrss_tags 
-                       WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
+                               WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name 
+                               ORDER BY count DESC LIMIT 55");
                        
                $tags = array();
 
 
        function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
 
+               $age_qpart = getMaxAgeSubquery();
+
                if ($smart_mode) {
                        if (!$_SESSION["lctr_last_value"]) {
                                $_SESSION["lctr_last_value"] = array();
                $old_counters = $_SESSION["lctr_last_value"];
                $lctrs_modified = false;
 
-               $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                       WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                       hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
-
-               $count = db_fetch_result($result, 0, "count");
+               $count = getFeedUnread($link, -1);
 
                if (!$ret_mode) {
                        print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
                } else {
                        $ret_arr["-1"]["counter"] = $count;
-                       $ret_arr["-1"]["description"] = "Starred";
+                       $ret_arr["-1"]["description"] = __("Starred articles");
                }
 
-               $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                       WHERE published = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       ttrss_user_entries.feed_id = ttrss_feeds.id AND
-                       hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
-
-               $count = db_fetch_result($result, 0, "count");
+               $count = getFeedUnread($link, -2);
 
                if (!$ret_mode) {
                        print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
                } else {
                        $ret_arr["-2"]["counter"] = $count;
-                       $ret_arr["-2"]["description"] = "Published";
+                       $ret_arr["-2"]["description"] = __("Published articles");
+               }
+
+               $count = getFeedUnread($link, -3);
+
+               if (!$ret_mode) {
+                       print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
+               } else {
+                       $ret_arr["-3"]["counter"] = $count;
+                       $ret_arr["-3"]["description"] = __("Fresh articles");
                }
 
 
                        $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
                                WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
                                ttrss_feeds.hidden = false AND
+                               $age_qpart AND
                                ttrss_user_entries.feed_id = ttrss_feeds.id AND
                                ttrss_user_entries.ref_id = ttrss_entries.id AND 
                                ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
 
        function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
 
+               $age_qpart = getMaxAgeSubquery();
+
                if ($smart_mode) {
                        if (!$_SESSION["fctr_last_value"]) {
                                $_SESSION["fctr_last_value"] = array();
                        FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
                        AND parent_feed IS NULL"); */
 
-               $result = db_query($link, "SELECT ttrss_feeds.id,
+               $query = "SELECT ttrss_feeds.id,
+                               ttrss_feeds.title,
                                SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated, 
                                last_error, 
                                COUNT(ttrss_entries.id) AS count 
                                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) 
-                       WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
+                               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");
+                       GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
 
+               $result = db_query($link, $query);
                $fctrs_modified = false;
 
                $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
                                $last_updated = date($short_date, strtotime($line["last_updated"]));
                        }                               
 
+                       $last_updated = htmlspecialchars($last_updated);
+
                        $has_img = is_file(ICONS_DIR . "/$id.ico");
 
                        $tmp_result = db_query($link,
-                               "SELECT id,COUNT(unread) AS unread
+                               "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) 
-                               WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.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");
                        
                        if (db_num_rows($tmp_result) > 0) {                             
                                while ($l = db_fetch_assoc($tmp_result)) {
 
                print "<select id=\"$id\" name=\"$id\" $attributes>";
                if ($include_all_feeds) { 
-                       print "<option value=\"0\">All feeds</option>";
+                       print "<option value=\"0\">".__('All feeds')."</option>";
                }
        
                $result = db_query($link, "SELECT id,title FROM ttrss_feeds
                        return __("Starred articles");
                } else if ($id == -2) {
                        return __("Published articles");
+               } else if ($id == -3) {
+                       return __("Fresh articles");
                } else if ($id < -10) {
                        $label_id = -10 - $id;
                        $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
                print "<param key=\"infobox_disable_overlay\" value=\"" . 
                        get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
 
+               print "<param key=\"icons_location\" value=\"" . 
+                       ICONS_URL . "\"/>";
+
                print "</init-params>";
        }
 
 
                if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
                        
-                       if ($_SESSION["last_version_check"] + 600 < time()) {
+                       if ($_SESSION["last_version_check"] + 7200 < time()) {
                                $new_version_details = check_for_update($link);
 
                                print "<param key=\"new_version_available\" value=\"".
                        }
                }
 
+//             print "<param key=\"new_version_available\" value=\"1\"/>";
+
                print "</runtime-info>";
        }
 
                return $search_query_part;
        }
 
-       function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
+       function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
+
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                        if ($search) {
                        
                                $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 == -3) { // fresh virtual feed
+                               $query_strategy_part = "unread = true";
+
+                               $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
+
+                               if (DB_TYPE == "pgsql") {
+                                       $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; 
+                               } else {
+                                       $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
+                               }
+
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
                        } else if ($feed <= -10) { // labels
                                $label_id = -$feed - 11;
 
                                        if ($feed != 0) {                       
                                                $result = db_query($link, "SELECT title FROM ttrss_feed_categories
-                                                       WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+                                                       WHERE id = '$feed' AND owner_uid = $owner_uid");
                                                $feed_title = db_fetch_result($result, 0, "title");
                                        } else {
                                                $feed_title = __("Uncategorized");
                                } else {
                                        
                                        $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds 
-                                               WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+                                               WHERE id = '$feed' AND owner_uid = $owner_uid");
                
                                        $feed_title = db_fetch_result($result, 0, "title");
                                        $feed_site_url = db_fetch_result($result, 0, "site_url");
                                $feed_title = __("Starred articles");
                        } else if ($feed == -2) {
                                $feed_title = __("Published articles");
+                       } else if ($feed == -3) {
+                               $feed_title = __("Fresh articles");
                        } else if ($feed < -10) {
                                $label_id = -$feed - 11;
                                $result = db_query($link, "SELECT description FROM ttrss_labels
                                        ttrss_feeds.hidden = false AND
                                        ttrss_user_entries.feed_id = ttrss_feeds.id AND
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
-                                       ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
+                                       ttrss_user_entries.owner_uid = '$owner_uid' AND
                                        $search_query_part
                                        $view_query_part
                                        $query_strategy_part ORDER BY $order_by
                                                ttrss_entries,ttrss_user_entries,ttrss_tags
                                        WHERE
                                                ref_id = ttrss_entries.id AND
-                                               ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
+                                               ttrss_user_entries.owner_uid = '$owner_uid' AND
                                                post_int_id = int_id AND tag_name = '$feed' AND
                                                $view_query_part
                                                $search_query_part
                        
        }
 
-       function generate_syndicated_feed($link, $feed, $is_cat,
+       function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
                $search, $search_mode, $match_on) {
 
                $qfh_ret = queryFeedHeadlines($link, $feed, 
-                               30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
+                       30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
+                       $owner_uid);
 
                $result = $qfh_ret[0];
                $feed_title = htmlspecialchars($qfh_ret[1]);
                $feed_site_url = $qfh_ret[2];
                $last_error = $qfh_ret[3];
 
-             if (!$feed_site_url) $feed_site_url = "http://localhost/";
+//             if (!$feed_site_url) $feed_site_url = "http://localhost/";
 
-               print "<rss version=\"2.0\">
+               print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
+                       <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
+                       <rss version=\"2.0\">
                        <channel>
                        <title>$feed_title</title>
                        <link>$feed_site_url</link>
                        print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
                        print "<link>" . htmlspecialchars($line["link"]) . "</link>";
 
-                       $tags = get_article_tags($link, $line["id"]);
+                       $tags = get_article_tags($link, $line["id"], $owner_uid);
 
                        foreach ($tags as $tag) {
                                print "<category>" . htmlspecialchars($tag) . "</category>";
 
        function getCategoryTitle($link, $cat_id) {
 
-               $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
-                       id = '$cat_id'");
-
-               if (db_num_rows($result) == 1) {
-                       return db_fetch_result($result, 0, "title");
+               if ($cat_id == -1) {
+                       return __("Special");
+               } else if ($cat_id == -2) {
+                       return __("Labels");
                } else {
-                       return "Uncategorized";
+
+                       $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
+                               id = '$cat_id'");
+
+                       if (db_num_rows($result) == 1) {
+                               return db_fetch_result($result, 0, "title");
+                       } else {
+                               return "Uncategorized";
+                       }
                }
        }
 
                }
 
                error_reporting(0);
-               $rss = fetch_rss($releases_feed);
+               if (ENABLE_SIMPLEPIE) {
+                       $rss = new SimplePie();
+                       $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
+                       $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
+                       $rss->set_feed_url($fetch_url);
+                       $rss->set_output_encoding('UTF-8');
+                       $rss->init();
+               } else {
+                       $rss = fetch_rss($releases_feed);
+               }
                error_reporting (DEFAULT_ERROR_LEVEL);
 
                if ($rss) {
 
-                       $items = $rss->items;
+                       if (ENABLE_SIMPLEPIE) {
+                               $items = $rss->get_items();
+                       } else {
+                               $items = $rss->items;
 
-                       if (!$items || !is_array($items)) $items = $rss->entries;
-                       if (!$items || !is_array($items)) $items = $rss;
+                               if (!$items || !is_array($items)) $items = $rss->entries;
+                               if (!$items || !is_array($items)) $items = $rss;
+                       }
 
                        if (!is_array($items) || count($items) == 0) {
                                return;
 
                        $latest_item = $items[0];
 
-                       $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
+                       if (ENABLE_SIMPLEPIE) {
+                               $last_title = $latest_item->get_title();
+                       } else {
+                               $last_title = $latest_item["title"];
+                       }
 
-                       $release_url = sanitize_rss($link, $latest_item["link"]);
-                       $content = sanitize_rss($link, $latest_item["description"]);
+                       $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
+
+                       if (ENABLE_SIMPLEPIE) {
+                               $release_url = sanitize_rss($link, $latest_item->get_link());
+                               $content = sanitize_rss($link, $latest_item->get_description());
+                       } else {
+                               $release_url = sanitize_rss($link, $latest_item["link"]);
+                               $content = sanitize_rss($link, $latest_item["description"]);
+                       }
 
                        if (version_compare(VERSION, $latest_version) == -1) {
                                if ($brief_fmt) {
 
                        $catchup_page_link = "javascript:catchupPage()";
                        $catchup_feed_link = "javascript:catchupCurrentFeed()";
+                       $catchup_sel_link = "javascript:catchupSelection()";
 
                        if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
 
                                                <a href=\"$sel_unread_link\">".__('Unread')."</a>,
                                                <a href=\"$sel_none_link\">".__('None')."</a></li>
                                        <li class=\"vsep\">&nbsp;</li>
-                                       <li class=\"top\">Toggle<ul>
+                                       <li class=\"top\">".__('Toggle')."<ul>
                                                <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
                                                <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
                                                <li onclick=\"$tog_published_link\">".__('Published')."</li>
                                                </ul></li>
                                        <li class=\"vsep\">&nbsp;</li>
                                        <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
-                                               <li onclick=\"$catchup_page_link\">".__('This page')."</li>
+                                               <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
+                                               <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
+                                               <li><span class=\"insensitive\">--------</span></li>
+                                               <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
+                                               <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
+                                               <li><span class=\"insensitive\">--------</span></li>
                                                <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
                                        ";
 
                
                        if ($feed_site_url) {
                                if (!$bottom) {
-                                       $target = "target=\"_blank\"";
+                                       $target = "target=\"_new\"";
                                }
-                               print "<a $target href=\"$feed_site_url\">$feed_title</a>";
+                               print "<a $target href=\"$feed_site_url\">".
+                                       truncate_string($feed_title,30)."</a>";
                        } else {
                                print $feed_title;
                        }
 
                }
 
+       function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
+
+                       $tmp_category = getCategoryTitle($link, $cat_id);
+                       $cat_unread = getCategoryUnread($link, $cat_id);
+
+                       if ($hidden) {
+                               $holder_style = "display:none;";
+                               $ellipsis = "...";
+                       } else {
+                               $holder_style = "";
+                               $ellipsis = "";
+                       }
+
+                       $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
+
+                       print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
+                               <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
+
+                       if ($can_browse) {
+                               print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
+                       } else {
+                               print "<span id=\"FCAP-$cat_id\">";
+                       }
+
+                       print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" 
+                               class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
+
+                       if ($can_browse) {
+                               print "</a>";
+                       } else {
+                               print "</span>";
+                       }
+
+                       print "</li>";
+
+                       print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
+       }
+       
        function outputFeedList($link, $tags = false) {
 
                print "<ul class=\"feedList\" id=\"feedList\">";
                /* virtual feeds */
 
                if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                       print "<li class=\"feedCat\">".__('Special')."</li>";
-                       print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
+
+                       if ($_COOKIE["ttrss_vf_vclps"] == 1) {
+                               $cat_hidden = true;
+                       } else {
+                               $cat_hidden = false;
+                       }
+
+#                      print "<li class=\"feedCat\">".__('Special')."</li>";
+#                      print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";            
+#                      print "<li class=\"feedCat\">".
+#                              "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
+#                              __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
+#
+#                      print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
+#                              <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
+
+#                      $cat_unread = getCategoryUnread($link, -1);
+#                      $tmp_category = __("Special");
+#                      $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
+
+                       printCategoryHeader($link, -1, $cat_hidden, false);
                }
 
                $num_starred = getFeedUnread($link, -1);
                $num_published = getFeedUnread($link, -2);
+               $num_fresh = getFeedUnread($link, -3);
+
+               $class = "virt";
+
+               if ($num_fresh > 0) $class .= "Unread";
+
+               printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh, 
+                       "images/fresh.png", $link);
 
                $class = "virt";
 
                if ($num_starred > 0) $class .= "Unread";
 
+               $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
+
+               if ($is_ie) {
+                       $mark_img_ext = "gif";
+               } else {
+                       $mark_img_ext = "png";
+               }
+
                printFeedEntry(-1, $class, __("Starred articles"), $num_starred, 
-                       "images/mark_set.png", $link);
+                       "images/mark_set.$mark_img_ext", $link);
 
                $class = "virt";
 
                if ($num_published > 0) $class .= "Unread";
 
                printFeedEntry(-2, $class, __("Published articles"), $num_published, 
-                       "images/pub_set.png", $link);
+                       "images/pub_set.gif", $link);
 
                if (get_pref($link, 'ENABLE_FEED_CATS')) {
                        print "</ul>";
                
                                if (db_num_rows($result) > 0) {
                                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                                               print "<li class=\"feedCat\">".__('Labels')."</li>";
-                                               print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
+
+                                               if ($_COOKIE["ttrss_vf_lclps"] == 1) {
+                                                       $cat_hidden = true;
+                                               } else {
+                                                       $cat_hidden = false;
+                                               }
+
+                                               printCategoryHeader($link, -2, $cat_hidden, false);
+
+#                                              print "<li class=\"feedCat\">".
+#                                                      "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
+#                                                      __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
+#
+#                                              print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
                                        } else {
                                                print "<li><hr></li>";
                                        }
                                }
                        }
 
+                       $age_qpart = getMaxAgeSubquery();
+
                        $result = db_query($link, "SELECT ttrss_feeds.*,
                                SUBSTRING(last_updated,1,19) AS last_updated_noms,
                                (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
                                        WHERE feed_id = ttrss_feeds.id AND unread = true
+                                               AND $age_qpart
                                                AND ttrss_user_entries.ref_id = ttrss_entries.id
                                                AND owner_uid = '$owner_uid') as unread,
                                cat_id,last_error,
                                        }
 
                                        if ($collapsed == "t" || $collapsed == "1") {
-                                               $holder_class = "invisible";
+                                               $holder_class = "feedCatHolder";
+                                               $holder_style = "display:none;";
                                                $ellipsis = "...";
                                        } else {
                                                $holder_class = "feedCatHolder";
+                                               $holder_style = "";
                                                $ellipsis = "";
                                        }
 
                                                        class=\"$catctr_class\">($cat_unread)</span> $ellipsis
                                                        </a></li>";
 
-                                       // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
-                                       // -> keyboard navigation, etc.
-                                       print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
+                                       print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
                                }
        
                                printFeedEntry($feed_id, $class, $feed, $unread, 
                                print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
                        }
 
+                       $age_qpart = getMaxAgeSubquery();
+
                        $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
-                               FROM ttrss_user_entries WHERE int_id = post_int_id 
+                               FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id 
+                                       AND ref_id = id AND $age_qpart
                                        AND unread = true)) AS count FROM ttrss_tags 
-                               WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
+                                       WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name 
+                                       ORDER BY count DESC LIMIT 50");
 
                        $tags = array();
        
 
        }
 
-       function get_article_tags($link, $id) {
+       function get_article_tags($link, $id, $owner_uid = 0) {
 
                $a_id = db_escape_string($id);
 
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
                $tmp_result = db_query($link, "SELECT DISTINCT tag_name, 
                        owner_uid as owner FROM
                        ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
-                               ref_id = '$a_id' LIMIT 1) ORDER BY tag_name");
+                               ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
 
                $tags = array();        
        
 
        function format_warning($msg, $id = "") {
                return "<div class=\"warning\" id=\"$id\"> 
-                       <img src=\"images/sign_excl.png\">$msg</div>";
+                       <img src=\"images/sign_excl.gif\">$msg</div>";
        }
 
        function format_notice($msg) {
                return "<div class=\"notice\"> 
-                       <img src=\"images/sign_info.png\">$msg</div>";
+                       <img src=\"images/sign_info.gif\">$msg</div>";
        }
 
        function format_error($msg) {
                return "<div class=\"error\"> 
-                       <img src=\"images/sign_excl.png\">$msg</div>";
+                       <img src=\"images/sign_excl.gif\">$msg</div>";
        }
 
        function print_notice($msg) {
 
                        if ($line["link"]) {
                                print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . 
-                                       $line["title"] . "</a>$entry_author</div>";
+                                       $line["title"] . "</a><span class='author'>$entry_author</span></div>";
                        } else {
                                print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
-                       $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
+/*                     $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
                                ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
-                               ORDER BY tag_name");
+                               ORDER BY tag_name"); */
+
+                       $tags = get_article_tags($link, $id);
        
                        $tags_str = "";
                        $f_tags_str = "";
 
                        $num_tags = 0;
 
-                       while ($tmp_line = db_fetch_assoc($tmp_result)) {
+                       foreach ($tags as $tag) {
                                $num_tags++;
-                               $tag = $tmp_line["tag_name"];
                                $tag_escaped = str_replace("'", "\\'", $tag);
 
                                $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
                                
                                if ($num_tags == 6) {
-                                       $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
+                                       $tags_str .= "...";
+
                                } else if ($num_tags < 6) {
                                        $tags_str .= $tag_str;
                                }
                        $tags_str = preg_replace("/, $/", "", $tags_str);
                        $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
 
+                       $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
+                       $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
+
                        if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
 
                        if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
 
-                       print "<div style='float : right'>$tags_str 
+                       print "<div style='float : right'>
+                               <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
+                               $tags_str 
                                <a title=\"Edit tags for this article\" 
                                        href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
                                <div clear='both'>$entry_comments</div>";
                        print "<div class=\"postIcon\">" . $feed_icon . "</div>";
                        print "<div class=\"postContent\">";
                        
-                       if (db_num_rows($tmp_result) > 0) {
-                               print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
-                       }
+                       #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
 
                        $line["content"] = sanitize_rss($link, $line["content"]);
 
                $feed_title = $qfh_ret[1];
                $feed_site_url = $qfh_ret[2];
                $last_error = $qfh_ret[3];
-               
+
+               if ($feed == -2) {
+                       $feed_site_url = article_publish_url($link);
+               }
+
                /// STOP //////////////////////////////////////////////////////////////////////////////////
 
                if (!$offset) {
                        print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
                }
 
+               $headlines_count = db_num_rows($result);
+
                if (db_num_rows($result) > 0) {
 
 #                      print "\{$offset}";
                                        cellspacing=\"0\">";
                        }
 
-                       $lnum = 0;
-       
+                       $lnum = $limit*$offset;
+
                        error_reporting (DEFAULT_ERROR_LEVEL);
        
                        $num_unread = 0;
                                } else {
                                        $is_unread = false;
                                }
-       
+
+                               $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
+
+                               if ($is_ie) {
+                                       $mark_img_ext = "gif";
+                               } else {
+                                       $mark_img_ext = "png";
+                               }
+
                                if ($line["marked"] == "t" || $line["marked"] == "1") {
-                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\" 
+                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\" 
                                                class=\"markedPic\"
-                                               alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
+                                               alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
                                } else {
-                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\" 
+                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\" 
                                                class=\"markedPic\"
-                                               alt=\"Set mark\" onclick='javascript:tMark($id)'>";
+                                               alt=\"Star article\" onclick='javascript:tMark($id)'>";
                                }
 
                                if ($line["published"] == "t" || $line["published"] == "1") {
-                                       $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\" 
+                                       $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\" 
                                                class=\"markedPic\"
-                                               alt=\"Unpublish\" onclick='javascript:tPub($id)'>";
+                                               alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
                                } else {
-                                       $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\" 
+                                       $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\" 
                                                class=\"markedPic\"
-                                               alt=\"Publish\" onclick='javascript:tPub($id)'>";
+                                               alt=\"Publish article\" onclick='javascript:tPub($id)'>";
                                }
 
 #                              $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
                                        print "<td class='hlMarkedPic'>$marked_pic</td>";
                                        print "<td class='hlMarkedPic'>$published_pic</td>";
 
-                                       if ($line["feed_title"]) {                      
-                                               print "<td class='hlContent'>$content_link</td>";
-                                               print "<td class='hlFeed'>
-                                                       <a href=\"javascript:viewfeed($feed_id, '', false)\">".
-                                                               $line["feed_title"]."</a>&nbsp;</td>";
-                                       } else {                        
-                                               print "<td class='hlContent' valign='middle'>";
-
-                                               print "<a href=\"javascript:view($id,$feed_id);\">" .
-                                                       $line["title"];
-
-                                               if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
-                                                       if ($content_preview) {
-                                                               print "<span class=\"contentPreview\"> - $content_preview</span>";
-                                                       }
+#                                      if ($line["feed_title"]) {                      
+#                                              print "<td class='hlContent'>$content_link</td>";
+#                                              print "<td class='hlFeed'>
+#                                                      <a href=\"javascript:viewfeed($feed_id, '', false)\">".
+#                                                              truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
+#                                      } else {                        
+
+                                       print "<td class='hlContent' valign='middle'>";
+
+                                       print "<a href=\"javascript:view($id,$feed_id);\">" .
+                                               $line["title"];
+
+                                       if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
+                                               if ($content_preview) {
+                                                       print "<span class=\"contentPreview\"> - $content_preview</span>";
                                                }
-               
-                                               print "</a>";
-                                               print "</td>";
                                        }
+
+                                       print "</a>";
+
+#                                                      <a href=\"javascript:viewfeed($feed_id, '', false)\">".
+#                                                      $line["feed_title"]."</a>       
+
+                                       if ($line["feed_title"]) {                      
+                                               print "<span class=\"hlFeed\">
+                                                       (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
+                                                       $line["feed_title"]."</a>)
+                                               </span>";
+                                       }
+
+
+                                       print "</td>";
+                                       
+#                                      }
                                        
                                        print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
                
                                        
                                        print "<a class=\"title\" 
                                                onclick=\"javascript:toggleUnread($id, 0)\"
-                                               target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
+                                               target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
 
                                        print $entry_author;
 
 
                                        print "</div>";
 
+                                       if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
+                                               $line["content_preview"] = preg_replace("/href=/i", 
+                                                       "target=\"_new\" href=", $line["content_preview"]);
+                                       }
+
                                        print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
 
                                        print "<div class=\"cdmFooter\"><span class='s0'>";
                                        $tags = get_article_tags($link, $id);
 
                                        $tags_str = "";
+                                       $full_tags_str = "";
+                                       $num_tags = 0;
 
                                        foreach ($tags as $tag) {
                                                $num_tags++;
-                                               $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; 
+                                               $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 .= "...";
+                                               }
                                        }
 
                                        $tags_str = preg_replace("/, $/", "", $tags_str);
+                                       $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
+
+                                       $all_tags_div = "<span class='cdmAllTagsCtr'>...<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";
 
 //                                     print "<img src='images/tag.png' class='markedPic'>";
 
-                                       print "<span class='s1'>Tags: $tags_str <a title=\"Edit tags for this article\" 
+                                       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>";
 
                                        print "</span>";
                        print "</div>";
                }
 
-               return $topmost_article_ids;
+               return array($topmost_article_ids, $headlines_count);
        }
 
 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
                return sha1(uniqid(rand(), true));
        }
 
+       function article_publish_url($link) {
+
+               $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+
+               $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
+
+               return $url_path;
+       }
+
 ?>