]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
implement settings profiles (bump schema)
[tt-rss.git] / functions.php
index 26e57ab4a716dd51d07e5b6be1e55a52f3307218..672d9ef1d60b6c6348c69567ff78c469a20a12dc 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-/*     if ($_GET["debug"]) {
+/*     if ($_REQUEST["debug"]) {
                define('DEFAULT_ERROR_LEVEL', E_ALL);
        } else {
                define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
 
        function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
 
-               if (!$_GET["daemon"] && !$ignore_daemon) {
+               if (!$_REQUEST["daemon"] && !$ignore_daemon) {
                        return false;
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                        _debug("update_rss_feed: start");
                }
 
                } else {
 
                        $result = db_query($link, "SELECT id,update_interval,auth_login,
-                               auth_pass,cache_images,update_method,hidden,last_updated
+                               auth_pass,cache_images,update_method,last_updated
                                FROM ttrss_feeds WHERE id = '$feed'");
 
                }
 
                if (db_num_rows($result) == 0) {
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
                        }               
                        return false;
                }
 
-               $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");
 
                        $use_simplepie = ENABLE_SIMPLEPIE;
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                        _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n");
                }
 
 
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                        _debug("update_rss_feed: fetching [$fetch_url]...");
                }
 
-               if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
+               if (!defined('DAEMON_EXTENDED_DEBUG') && !$_REQUEST['xdebug']) {
                        error_reporting(0);
                }
 
                        $rss->set_output_encoding('UTF-8');
 
                        if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("enabling image cache");
                                }
 
                                $rss->set_image_handler('./image.php', 'i');
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("feed update interval (sec): " .
                                        get_feed_update_interval($link, $feed)*60);
                        }
 
 //             print_r($rss);
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                        _debug("update_rss_feed: fetch done, parsing...");
                } else {
                        error_reporting (DEFAULT_ERROR_LEVEL);
 
                if ($fetch_ok) {
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: processing feed data...");
                        }
 
                        }
 
                        if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {  
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: checking favicon...");
                                }
 
                                        $feed_title = db_escape_string($rss->channel["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: registering title: $feed_title");
                                }
                                
                                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') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: loading filters...");
                        }
 
                                // clear any errors and mark feed as updated if fetched okay
                                // even if it's blank
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: entry iterator is not an array, no articles?");
                                }
 
                                return; // no articles
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: processing articles...");
                        }
 
                        foreach ($iterator as $item) {
 
-                               if ($_GET['xdebug']) {
+                               if ($_REQUEST['xdebug'] == 2) {
                                        print_r($item);
-
                                }
 
                                if ($use_simplepie) {
                                        if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: guid $entry_guid");
                                }
 
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
                                }
 
                                        if (!$entry_link) $entry_link = $item["link"];
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: title $entry_title");
                                }
 
                                        } 
                                }
 
-                               if ($_GET["xdebug"]) {
+                               if ($_REQUEST["xdebug"] == 2) {
                                        print "update_rss_feed: content: ";
                                        print_r(htmlspecialchars($entry_content));
                                }
                                                }
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: category tags:");
                                                print_r($additional_tags);
                                        }
                                $entry_content = sanitize_article_content($entry_content);
                                $entry_title = sanitize_article_content($entry_title);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
                                }
 
 
                                if (db_num_rows($result) == 0) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: base guid not found");
                                        }
 
 
                                if (db_num_rows($result) == 1) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: base guid found, checking for user record");
                                        }
 
 
                                        // do we allow duplicate posts with same GUID in different feeds?
                                        if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
-                                               $dupcheck_qpart = "AND feed_id = '$feed'";
+                                               $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)";
                                        } else { 
                                                $dupcheck_qpart = "";
                                        }
                                        $article_filters = get_article_filters($filters, $entry_title, 
                                                        $entry_content, $entry_link, $entry_timestamp);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: article filters: ");
                                                if (count($article_filters) != 0) {
                                                        print_r($article_filters);
 
                                        $score = calculate_article_score($article_filters);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: initial score: $score");
                                        }
 
-                                       $result = db_query($link,
-                                               "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
+                                       $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
                                                        ref_id = '$ref_id' AND owner_uid = '$owner_uid'
-                                                       $dupcheck_qpart");
+                                                       $dupcheck_qpart";
+
+//                                     if ($_REQUEST["xdebug"]) print "$query\n";
+
+                                       $result = db_query($link, $query);
 
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                        _debug("update_rss_feed: user record not found, creating...");
                                                }
 
                                                        $entry_int_id = db_fetch_result($result, 0, "int_id");
                                                }
                                        } else {
+                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                                       _debug("update_rss_feed: user record FOUND");
+                                               }
+
                                                $entry_ref_id = db_fetch_result($result, 0, "ref_id");
                                                $entry_int_id = db_fetch_result($result, 0, "int_id");
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                                _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
                                        }
 
 
                                db_query($link, "COMMIT");
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: assigning labels...");
                                }
 
                                assign_article_to_labels($link, $entry_ref_id, $article_filters,
                                        $owner_uid);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: looking for enclosures...");
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        print_r($enclosures);
                                }
 
 
                                db_query($link, "COMMIT");
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: looking for tags...");
                                }
 
 
 //                             print "<p>TAGS: "; print_r($entry_tags); print "</p>";
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        print_r($entry_tags);
                                }
 
                                        db_query($link, "COMMIT");
                                } 
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: article processed");
                                }
                        } 
 
                        if (!$last_updated) {
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                        _debug("update_rss_feed: new feed, catching it up...");
                                }
                                catchup_feed($link, $feed, false, $owner_uid);
                        }
 
-                       if (!$hidden) {
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
-                                       _debug("update_rss_feed: updating counters cache...");
-                               }
-
-                               // disabled, purge_feed() does that...
-                               //ccache_update($link, $feed, $owner_uid);
-                       }
-
                        purge_feed($link, $feed, 0);
 
                        db_query($link, "UPDATE ttrss_feeds 
                                $error_msg = mb_substr(magpie_error(), 0, 250);
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: error fetching feed: $error_msg");
                        }
 
                        unset($rss);
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                        _debug("update_rss_feed: done");
                }
 
                $rtl_content = false, $last_updated = false, $last_error = false,
                $fg_content = false, $bg_content = false) {
 
+               if (!$feed_title) $feed_title = getFeedTitle($link, $feed_id, false);
+               if (!$unread) $unread = getFeedUnread($link, $feed_id); 
+
+               if ($unread > 0) $class .= "Unread";
+
+               if (!$icon_file) $icon_file = getFeedIcon($feed_id);
+
                if (file_exists($icon_file) && filesize($icon_file) > 0) {
                                $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
                } else {
                }
        }
 
-       function initialize_user_prefs($link, $uid) {
+       function initialize_user_prefs($link, $uid, $profile = false) {
 
                $uid = db_escape_string($uid);
 
+               if (!$profile) {
+                       $profile = "NULL";
+                       $profile_qpart = "profile IS NULL";
+               } else {
+                       $profile_qpart = "profile = '$profile'";
+               }
+
                db_query($link, "BEGIN");
 
                $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
                
                $u_result = db_query($link, "SELECT pref_name 
-                       FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
+                       FROM ttrss_user_prefs WHERE owner_uid = '$uid' AND $profile_qpart");
 
                $active_prefs = array();
 
 //                             print "adding " . $line["pref_name"] . "<br>";
 
                                db_query($link, "INSERT INTO ttrss_user_prefs
-                                       (owner_uid,pref_name,value) VALUES 
-                                       ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
+                                       (owner_uid,pref_name,value, profile) VALUES 
+                                       ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)");
 
                        }
                }
        
                                $user_theme = get_user_theme_path($link);
        
-                               $_SESSION["theme"] = $user_theme;
                                $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
                                $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
        
 
                        $user_theme = get_user_theme_path($link);
        
-                       $_SESSION["theme"] = $user_theme;
                        $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
        
                        initialize_user_prefs($link, $_SESSION["uid"]);
                                        $_SESSION["language"] = $_POST["language"];
                                        $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
 
+                                       if ($_POST["profile"]) {
+
+                                               $profile = db_escape_string($_POST["profile"]);
+
+                                               $result = db_query($link, "SELECT id FROM ttrss_settings_profiles
+                                                       WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]);
+
+                                               if (db_num_rows($result) != 0) {
+                                                       $_SESSION["profile"] = $profile;
+                                                       $_SESSION["prefs_cache"] = array();
+                                               }
+                                       }
+
                                        header("Location: " . $_SERVER["REQUEST_URI"]);
                                        exit;
 
        }
 
        function get_user_theme_path($link) {
+               $theme_id = get_pref($link, "_THEME_ID");
+
                $result = db_query($link, "SELECT theme_path 
-                       FROM 
-                               ttrss_themes,ttrss_users
-                       WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
+                       FROM ttrss_themes       WHERE id = '$theme_id'");
                if (db_num_rows($result) != 0) {
                        return db_fetch_result($result, 0, "theme_path");
                } else {
                        if (!$owner_uid) $owner_uid = $_SESSION['uid'];
 
                        if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
-                       
+
                                if ($cat_view) {
 
                                        if ($feed >= 0) {
                if (!$omode) $omode = "flc";
        
                getGlobalCounters($link);
-       
+               getVirtCounters($link);
+
                if (strchr($omode, "l")) getLabelCounters($link);
                if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
                if (strchr($omode, "t")) getTagCounters($link);
                        ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
 
                while ($line = db_fetch_assoc($result)) {
-                       $line["cat_id"] = sprintf("%d", $line["cat_id"]);
+                       $line["cat_id"] = (int) $line["cat_id"];
 
                        print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
                                $line["unread"]."\"/>";
                        $age_qpart = getMaxAgeSubquery();
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query 
-                                       AND hidden = false
                                        AND owner_uid = " . $owner_uid);
        
                        $cat_feeds = array();
        
                        return $unread;
                } else if ($cat == -1) {
-                       return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
+                       return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0);
                } else if ($cat == -2) {
 
                        $result = db_query($link, "
                                        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 unread = true AND feed_id = ttrss_feeds.id
                                        AND ttrss_user_entries.owner_uid = '$owner_uid'");
 
                        $unread = db_fetch_result($result, 0, "unread");
                                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 ttrss_labels2.id = '$label_id'
-                               AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
+                               AND unread = true AND feed_id = ttrss_feeds.id
                                AND ttrss_user_entries.owner_uid = '$owner_uid'");
 
                if (db_num_rows($result) != 0) {
        function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
                $owner_uid = false) {
 
-               $n_feed = sprintf("%d", $feed);
+               $n_feed = (int) $feed;
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
 
                if ($is_cat) {
                        return getCategoryUnread($link, $n_feed, $owner_uid);           
+               } if ($feed != "0" && $n_feed == 0) {
+
+                       $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
+                               FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id 
+                                       AND ref_id = id AND $age_qpart
+                                       AND $unread_qpart)) AS count FROM ttrss_tags 
+                               WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
+                       return db_fetch_result($result, 0, "count");
+
                } else if ($n_feed == -1) {
                        $match_part = "marked = true";
                } else if ($n_feed == -2) {
                        }
                } else if ($n_feed == -4) {
                        $match_part = "true";
-               } else if ($n_feed > 0) {
+               } else if ($n_feed >= 0) {
 
                        $result = db_query($link, "SELECT id FROM ttrss_feeds 
                                        WHERE parent_feed = '$n_feed'
-                                       AND hidden = false
                                        AND owner_uid = " . $owner_uid);
 
                        if (db_num_rows($result) > 0) {
                                
                                $match_part = implode(" OR ", $linked_feeds);
 
-                               $result = db_query($link, "SELECT COUNT(int_id) AS unread 
+                               $tmp_result = db_query($link, "SELECT COUNT(int_id) AS unread 
                                        FROM ttrss_user_entries,ttrss_entries
                                        WHERE   $unread_qpart AND
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                                $unread = 0;
 
                                # this needs to be rewritten
-                               while ($line = db_fetch_assoc($result)) {
+                               while ($line = db_fetch_assoc($tmp_result)) {
                                        $unread += $line["unread"];
                                }
 
                                return $unread;
 
                        } else {
-                               $match_part = "feed_id = '$n_feed'";
+                               if ($n_feed != 0) {
+                                       $match_part = "feed_id = '$n_feed'";
+                               } else {
+                                       $match_part = "feed_id IS NULL";
+                               }
                        }
                } else if ($feed < -10) {
 
                }
 
                if ($match_part) {
-               
-                       $result = db_query($link, "SELECT count(int_id) AS unread 
-                               FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
-                               ttrss_user_entries.feed_id = ttrss_feeds.id AND
+
+                       if ($n_feed != 0) {
+                               $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
+                               $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
+                       } else {
+                               $from_qpart = "ttrss_user_entries,ttrss_entries";
+                       }
+
+                       $query = "SELECT count(int_id) AS unread 
+                               FROM $from_qpart WHERE
                                ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                               ttrss_feeds.hidden = false AND
                                $age_qpart AND
-                               $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $owner_uid);
+                               $feeds_qpart
+                               $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
+
+                       $result = db_query($link, $query);
                                
                } else {
                
 
        }
 
-       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();
-                       }
-               }
+       function getVirtCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
 
                $ret_arr = array();
 
-               for ($i = -1; $i >= -4; $i--) {
+               for ($i = 0; $i >= -4; $i--) {
 
                        $count = getFeedUnread($link, $i);
        
                                $ret_arr[$i]["counter"] = $count;
                                $ret_arr[$i]["description"] = getFeedTitle($link, $i);
                        }
-       
+               } 
+
+               return $ret_arr;
+       }
+
+       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"];
                print "</rpc-reply>";
        }
 
-       function subscribe_to_feed($link, $feed_link, $cat_id = 0, 
+       function subscribe_to_feed($link, $url, $cat_id = 0, 
                        $auth_login = '', $auth_pass = '') {
 
-               # check for feed:http://url
-               $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
+               $parts = parse_url($url);
 
-               # check for feed://URL
-               if (strpos($feed_link, "//") === 0) {
-                       $feed_link = "http:$feed_link";
-               }
+               if (!validate_feed_url($url)) return 2;
+
+               if ($parts['scheme'] == 'feed') $parts['scheme'] = 'http';
 
-               if ($feed_link == "") return;
+               $url = make_url_from_parts($parts);
 
                if ($cat_id == "0" || !$cat_id) {
                        $cat_qpart = "NULL";
        
                $result = db_query($link,
                        "SELECT id FROM ttrss_feeds 
-                       WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
+                       WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
        
                if (db_num_rows($result) == 0) {
                        
                        $result = db_query($link,
                                "INSERT INTO ttrss_feeds 
                                        (owner_uid,feed_url,title,cat_id, auth_login,auth_pass) 
-                               VALUES ('".$_SESSION["uid"]."', '$feed_link', 
+                               VALUES ('".$_SESSION["uid"]."', '$url', 
                                '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
        
                        $result = db_query($link,
-                               "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link
+                               "SELECT id FROM ttrss_feeds WHERE feed_url = '$url
                                        AND owner_uid = " . $_SESSION["uid"]);
        
                        $feed_id = db_fetch_result($result, 0, "id");
        
                        if ($feed_id) {
-                               update_rss_feed($link, $feed_link, $feed_id, true);
+                               update_rss_feed($link, $url, $feed_id, true);
                        }
 
-                       return true;
+                       return 1;
                } else {
-                       return false;
+                       return 0;
                }
        }
 
 
        function getFeedIcon($id) {
                switch ($id) {
+               case 0:
+                       return "images/archive.png";
+                       break;
                case -1:
-                       return "images/mark_set.gif";
+                       return "images/mark_set.png";
                        break;
                case -2:
                        return "images/pub_set.gif";
                        return "images/tag.png";
                        break;
                default:
-                       return ICONS_URL . "/$id.ico";
+                       if ($id < -10) {
+                               return "images/label.png";
+                       } else {
+                               return ICONS_URL . "/$id.ico";
+                       }
                        break;
                }
        }
                        return __("Fresh articles");
                } else if ($id == -4) {
                        return __("All articles");
+               } else if ($id === 0 || $id === "0") {
+                       return __("Archived articles");
                } else if ($id < -10) {
                        $label_id = -$id - 11;
                        $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
                                return "Unknown feed ($id)";
                        }
                } else {
-                       if (preg_match("/^-?[0-9][0-9]*$/", $id)) {
-                               return "getFeedTitle($id) failed";
-                       } else {
-                               return $id;
-                       }
+                       return $id;
                }
-
        }
 
        function get_session_cookie_name() {
                        }
                }
 
-               print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
+               print "<param key=\"theme\" value=\"".get_user_theme_path($link)."\"/>";
                print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
                print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
                print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
                print "<param key=\"num_feeds\" value=\"".
                        (int)$num_feeds. "\"/>";
 
+               print "<param key=\"collapsed_feedlist\" value=\"" . 
+                       (int) get_pref($link, "_COLLAPSED_FEEDLIST") . "\"/>";
+
                print "</init-params>";
        }
 
 
                if (ENABLE_UPDATE_DAEMON) {
                        print "<param key=\"daemon_is_running\" value=\"".
-                               sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
+                               (int) file_is_locked("update_daemon.lock") . "\"/>";
 
                        if (time() - $_SESSION["daemon_stamp_check"] > 30) {
 
                        if ($search && $search_mode == "all_feeds") {
                                $query_strategy_part = "ttrss_entries.id > 0";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
+                       /* tags */
                        } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
                                $query_strategy_part = "ttrss_entries.id > 0";
                                $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
                                        id = feed_id) as feed_title,";
-                       } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
+                       } else if ($feed > 0 && $search && $search_mode == "this_cat") {
        
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
 
                                        $query_strategy_part = "ttrss_entries.id > 0";
                                }
                                
-                       } else if ($feed >= 0) {
+                       } else if ($feed > 0) {
        
                                if ($cat_view) {
 
                                                $query_strategy_part = "feed_id = '$feed'";
                                        }
                                }
+                       } else if ($feed == 0) { // starred virtual feed
+                               $query_strategy_part = "feed_id IS NULL";
                        } else if ($feed == -1) { // starred virtual feed
                                $query_strategy_part = "marked = true";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
        
                        $feed_title = "";
 
-                       if ($search && $search_mode == "all_feeds") {
-                               $feed_title = __("Search results")." ($search)";
-                       } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
-                               $feed_title = __("Search results")." ($search, $feed)";
-                       } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
-                               $feed_title = $feed;
-                       } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
-       
+                       if ($search) {
+                               $feed_title = "Search results";
+                       } else {
                                if ($cat_view) {
-
-                                       if ($feed != 0) {                       
-                                               $result = db_query($link, "SELECT title FROM ttrss_feed_categories
-                                                       WHERE id = '$feed' AND owner_uid = $owner_uid");
-                                               $feed_title = db_fetch_result($result, 0, "title");
-                                       } else {
-                                               $feed_title = __("Uncategorized");
-                                       }
-
-                                       if ($search) {
-                                               $feed_title = __("Searched for")." $search ($feed_title)";
-                                       }
-
+                                       $feed_title = getCategoryTitle($link, $feed);
                                } else {
-                                       
-                                       $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds 
-                                               WHERE id = '$feed' AND owner_uid = $owner_uid");
+                                       if ((int)$feed == $feed && $feed > 0) {
+                                               $result = db_query($link, "SELECT title,site_url,last_error 
+                                                       FROM ttrss_feeds 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");
-                                       $last_error = db_fetch_result($result, 0, "last_error");
-
-                                       if ($search) {
-                                               $feed_title = __("Searched for") . " $search ($feed_title)";
-                                       }
-                               }
-       
-                       } else if ($feed == -1) {
-                               $feed_title = __("Starred articles");
-                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
-                       } else if ($feed == -2) {
-                               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
-                                       WHERE id = '$label_id'");
-                               $feed_title = db_fetch_result($result, 0, "caption");
-
-                               if ($search) {
-                                       $feed_title = __("Searched for") . " $search ($feed_title)";
+                                               $feed_title = db_fetch_result($result, 0, "title");
+                                               $feed_site_url = db_fetch_result($result, 0, "site_url");
+                                               $last_error = db_fetch_result($result, 0, "last_error");
+                                       } else {
+                                               $feed_title = getFeedTitle($link, $feed);
+                                       } 
                                }
-                       } else {
-                               $feed_title = "?";
                        }
 
                        $content_query_part = "content as content_preview,";
                                        if (!$override_order) {
                                                $order_by = "ttrss_feeds.title, $order_by";     
                                        }
+                               }
 
-                                       // Special output for Fresh feed
+                               if ($feed != "0") {
+                                       $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part";
+                                       $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
 
-/*                                     if ($feed == -3) {
-                                               $group_limit_part = "(select count(*) from 
-                                                       ttrss_user_entries AS t1, ttrss_entries AS t2 where
-                                                               t1.ref_id = t2.id and t1.owner_uid = 2 and
-                                                               t1.feed_id = ttrss_user_entries.feed_id and
-                                                               t2.updated > ttrss_entries.updated) <= 5 AND";
-} */
+                               } else {
+                                       $from_qpart = "ttrss_entries,ttrss_user_entries$ext_tables_part
+                                               LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
                                }
 
                                $query = "SELECT DISTINCT 
                                                ttrss_entries.id,ttrss_entries.title,
                                                updated,
                                                note,
-                                               unread,feed_id,marked,published,link,last_read,
+                                               unread,feed_id,marked,published,link,last_read,orig_feed_id,
                                                ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
                                                $vfeed_query_part
                                                $content_query_part
                                                ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
                                                author,score
                                        FROM
-                                               ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part
+                                               $from_qpart
                                        WHERE
                                        $group_limit_part
-                                       ttrss_feeds.hidden = false AND 
-                                       ttrss_user_entries.feed_id = ttrss_feeds.id AND
+                                       $feed_check_qpart
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                                        ttrss_user_entries.owner_uid = '$owner_uid' AND
                                        $search_query_part
                                        $query_strategy_part ORDER BY $order_by
                                        $limit_query_part $offset_query_part";
 
-                               if ($_GET["debug"]) print $query;
+                               if ($_REQUEST["debug"]) print $query;
 
                                $result = db_query($link, $query);
        
                                        note,
                                        ttrss_entries.id as id,title,
                                        updated,
-                                       unread,feed_id,
+                                       unread,feed_id,orig_feed_id,
                                        marked,link,last_read,                          
                                        ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
                                        $vfeed_query_part
                        }
 
                        return array($result, $feed_title, $feed_site_url, $last_error);
-                       
+               
        }
 
        function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
                }
 
                if (get_pref($link, "STRIP_IMAGES", $owner)) {
-                       
                        $res = preg_replace('/<img[^>]+>/is', '', $res);
+               }
 
+               if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
+                       $res = preg_replace("/href=/i", "target=\"_blank\" href=", $res);
                }
 
                return $res;
                                ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id 
                                AND include_in_digest = true
                                AND $interval_query
-                               AND hidden = false
                                AND ttrss_user_entries.owner_uid = $user_id
                                AND unread = true 
                        ORDER BY ttrss_feeds.title, date_entered DESC
                        unread = NOT unread,last_read = NOW()
                        WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
                }
+
+               $feed_id = getArticleFeed($link, $id);
+               ccache_update($link, $feed_id, $_SESSION["uid"]);
        }
 
        function make_guid_from_title($title) {
                        $catchup_feed_link = "javascript:catchupCurrentFeed()";
                        $catchup_sel_link = "javascript:catchupSelection()";
 
+                       $archive_sel_link = "javascript:archiveSelection()";
+                       $delete_sel_link = "javascript:deleteSelection()";
+
                        if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
 
                                $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
                                print "<a $target href=\"$feed_site_url\">".
                                        truncate_string($feed_title,30)."</a>";
                        } else {
-                               print $feed_title;
+                               if ($feed_id < -10) {
+                                       $label_id = -11-$feed_id;
+
+                                       $result = db_query($link, "SELECT fg_color, bg_color
+                                               FROM ttrss_labels2 WHERE id = '$label_id' AND owner_uid = " .
+                                               $_SESSION["uid"]);
+
+                                       if (db_num_rows($result) != 0) {
+                                               $fg_color = db_fetch_result($result, 0, "fg_color");
+                                               $bg_color = db_fetch_result($result, 0, "bg_color");
+
+                                               print "<span style='background : $bg_color; color : $fg_color'>";
+                                               print $feed_title;
+                                               print "</span>";
+                                       } else {
+                                               print $feed_title;
+                                       }
+
+                               } else {
+                                       print $feed_title;
+                               }
                        }
 
                        if ($search) {
                                <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>";
+                               <li class=\"insensitive\">".__('Selection:')."</li>
+                               <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Mark as read')."</li>";
+
+//                     print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed').
+//                             "</li>";
 
-                       print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed').
-                               "</li>";
+                       if ($feed_id != "0") {
+                               print "<li onclick=\"$archive_sel_link\">&nbsp;&nbsp;".__('Archive')."</li>";
+                       } else {
+                               print "<li onclick=\"$archive_sel_link\">&nbsp;&nbsp;".__('Move back')."</li>";
+                               print "<li onclick=\"$delete_sel_link\">&nbsp;&nbsp;".__('Delete')."</li>";
+
+                       } 
 
                        //print "<li><span class=\"insensitive\">--------</span></li>";
                        print "<li class=\"insensitive\">".__('Assign label:')."</li>";
 
                if (get_pref($link, 'ENABLE_FEED_CATS')) {
 
-                       if ($_COOKIE["ttrss_vf_vclps"] == 1) {
-                               $cat_hidden = true;
-                       } else {
-                               $cat_hidden = false;
-                       }
+                       $cat_hidden = get_pref($link, "_COLLAPSED_SPECIAL");
 
                        printCategoryHeader($link, -1, $cat_hidden, false);
                }
 
-               $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 ($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";
+               foreach (array(-4, -3, -1, -2, 0) as $i) {
+                       printFeedEntry($i, "virt", false, false, 
+                               false, $link);
                }
 
-               printFeedEntry(-1, $class, __("Starred articles"), $num_starred, 
-                       "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.gif", $link);
-
                if (get_pref($link, 'ENABLE_FEED_CATS')) {
                        print "</ul></li>";
                }
                                if (db_num_rows($result) > 0) {
                                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
 
-                                               if ($_COOKIE["ttrss_vf_lclps"] == 1) {
-                                                       $cat_hidden = true;
-                                               } else {
-                                                       $cat_hidden = false;
-                                               }
+                                               $cat_hidden = get_pref($link, "_COLLAPSED_LABELS");
 
                                                printCategoryHeader($link, -2, $cat_hidden, true);
 
        
                                        $label_id = -$line['id'] - 11;
                                        $count = getFeedUnread($link, $label_id);
-
-                                       $class = "label";
-       
-                                       if ($count > 0) {
-                                               $class .= "Unread";
-                                       }
        
                                        printFeedEntry($label_id, 
-                                               $class, $line["caption"], 
-                                               $count, "images/label.png", $link, 
+                                               "label", $line["caption"], 
+                                               $count, false, $link, 
                                                false, false, false,
                                                $line['fg_color'], $line['bg_color']);
                
                                        ON
                                                (ttrss_feeds.id = feed_id)
                                WHERE 
-                                       ttrss_feeds.hidden = false AND
                                        ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
                                ORDER BY $order_by_qpart"; 
 
                        $result = db_query($link, $query);
 
-                       $actid = $_GET["actid"];
+                       $actid = $_REQUEST["actid"];
        
                        /* real feeds */
        
                                $feed_id = $line["id"];   
                                $unread = $line["unread"];
        
-                               $subop = $_GET["subop"];
+                               $subop = $_REQUEST["subop"];
 
                                if (get_pref($link, 'HEADLINES_SMART_DATE')) {
                                        $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
                                        $class = "feed";
                                }
        
-                               if ($unread > 0) $class .= "Unread";
-       
                                if ($actid == $feed_id) {
                                        $class .= "Selected";
                                }
 
                                        // workaround for NULL category
                                        if ($category == __("Uncategorized")) {
-                                               if ($_COOKIE["ttrss_vf_uclps"] == 1) {
-                                                       $collapsed = "t";
-                                               }
+                                               $collapsed = get_pref($link, "_COLLAPSED_UNCAT");
                                        }
 
-                                       $cat_id = sprintf("%d", $cat_id);
+                                       $cat_id = (int) $cat_id;
 
                                        printCategoryHeader($link, $cat_id, $collapsed, true);
 
                                }
        
                                printFeedEntry($feed_id, $class, $feed, $unread, 
-                                       ICONS_URL."/$feed_id.ico", $link, $rtl_content, 
+                                       false, $link, $rtl_content, 
                                        $last_updated, $line["last_error"]);
        
                                ++$lnum;
                        foreach (array_keys($tags) as $tag) {
        
                                $unread = $tags[$tag];
-       
                                $class = "tag";
-       
-                               if ($unread > 0) {
-                                       $class .= "Unread";
-                               }
-       
+
                                printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
        
                        } 
                $zoom_mode = false) {
 
                /* we can figure out feed_id from article id anyway, why do we
-                * pass feed_id here? */
+                * pass feed_id here? let's ignore the argument :( */
 
                $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
                        WHERE ref_id = '$id'");
 
-               $feed_id = db_fetch_result($result, 0, "feed_id");
+               $feed_id = (int) db_fetch_result($result, 0, "feed_id");
 
                if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
 
                        (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
                        num_comments,
                        author,
+                       orig_feed_id,
                        note
                        FROM ttrss_entries,ttrss_user_entries
                        WHERE   id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
 
                if ($result) {
 
-                       $link_target = "";
-
-                       if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
-                               $link_target = "target=\"_blank\"";
-                       }
-
                        $line = db_fetch_assoc($result);
 
                        if ($line["icon_url"]) {
                                } else {
                                        $comments_url = $line["link"];
                                }
-                               $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
+                               $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
                        } else {
                                if ($line["comments"] && $line["link"] != $line["comments"]) {
-                                       $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
+                                       $entry_comments = "<a target='_blank' href=\"".$line["comments"]."\">comments</a>";
                                }                               
                        }
 
                        print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
 
                        if ($line["link"]) {
-                               print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . 
+                               print "<div clear='both'><a target='_blank' href=\"" . $line["link"] . "\">" . 
                                        $line["title"] . "</a><span class='author'>$entry_author</span></div>";
                        } else {
                                print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        print "</div>";
                        print "<div clear='both'>$entry_comments</div>";
 
+                       if ($line["orig_feed_id"]) {
+
+                               $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
+                                       WHERE id = ".$line["orig_feed_id"]);
+
+                               if (db_num_rows($tmp_result) != 0) {
+
+                                       print "<div clear='both'>";
+                                       print __("Originally from:");
+
+                                       print "&nbsp;";
+
+                                       $tmp_line = db_fetch_assoc($tmp_result);
+
+                                       print "<a target='_blank' 
+                                               href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
+                                               $tmp_line['title'] . "</a>";
+
+                                       print "&nbsp;";
+
+                                       print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                       print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
+
+                                       print "</div>";
+                               }
+                       }
+
                        print "</div>";
 
                        print "<div class=\"postIcon\">" . $feed_icon . "</div>";
 
                        $article_content = sanitize_rss($link, $line["content"]);
 
-                       if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
-                               $article_content = preg_replace("/href=/i", "target=\"_blank\" href=", 
-                                       $article_content);
-                       }
-
                        print "<div id=\"POSTNOTE-$id\">";
                                if ($line['note']) {
                                        print format_article_note($id, $line['note']);
 
                                print "<div class=\"postEnclosures\">";
 
-                               if ($always_display_enclosures || !preg_match("/<img/i", $article_content)) {
-                                       foreach ($entries as $entry) {
-                                               if (preg_match("/image/", $entry["type"])) {
-                                                       print "<p><img 
-                                                               alt=\"".htmlspecialchars($entry["filename"])."\"
-                                                               src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
+                               if (!get_pref($link, "STRIP_IMAGES")) {
+                                       if ($always_display_enclosures || !preg_match("/<img/i", $article_content)) {
+                                               foreach ($entries as $entry) {
+                                                       if (preg_match("/image/", $entry["type"])) {
+                                                               print "<p><img 
+                                                                       alt=\"".htmlspecialchars($entry["filename"])."\"
+                                                                       src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
+                                                       }
                                                }
                                        }
                                }
                } else {
                        print "
                                <div style=\"text-align : center\">
-                               <input type=\"submit\" onclick=\"return window.close()\" 
-                               value=\"".__("Close this window")."\"></div>";
+                               <button onclick=\"return window.close()\">".
+                                       __("Close this window")."</button></div>";
                        print "</body></html>";
 
                }
                $subop_split = split(":", $subop);
 
                if ($subop == "CatchupSelected") {
-                       $ids = split(",", db_escape_string($_GET["ids"]));
-                       $cmode = sprintf("%d", $_GET["cmode"]);
+                       $ids = split(",", db_escape_string($_REQUEST["ids"]));
+                       $cmode = sprintf("%d", $_REQUEST["cmode"]);
 
                        catchupArticlesById($link, $ids, $cmode);
                }
 
                /// START /////////////////////////////////////////////////////////////////////////////////
 
-               $search = db_escape_string($_GET["query"]);
+               $search = db_escape_string($_REQUEST["query"]);
 
                if ($search) { 
                        $disable_cache = true;
                }
 
-               $search_mode = db_escape_string($_GET["search_mode"]);
-               $match_on = db_escape_string($_GET["match_on"]);
+               $search_mode = db_escape_string($_REQUEST["search_mode"]);
+               $match_on = db_escape_string($_REQUEST["match_on"]);
 
                if (!$match_on) {
                        $match_on = "both";
 
                $real_offset = $offset * $limit;
 
-               if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
+               if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
 
                $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, 
                        $search, $search_mode, $match_on, $override_order, $real_offset);
 
-               if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
+               if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
 
                $result = $qfh_ret[0];
                $feed_title = $qfh_ret[1];
                                        $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.$mark_img_ext\" 
+                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\" 
                                                class=\"markedPic\"
                                                alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
                                } else {
-                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\" 
+                                       $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\" 
                                                class=\"markedPic\"
                                                alt=\"Star article\" onclick='javascript:tMark($id)'>";
                                }
 #                                                              truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
 #                                      } else {                        
 
-                                       print "<td onclick='view($id,$feed_id)' class='hlContent$hlc_suffix' valign='middle' id='HLC-$id'>";
+                                       print "<td onclick='view($id)' class='hlContent$hlc_suffix' valign='middle' id='HLC-$id'>";
 
                                        print "<a id=\"RTITLE-$id\" 
                                                href=\"" . htmlspecialchars($line["link"]) . "\"
 
 #                                      }
                                        
-                                       print "<td class=\"hlUpdated\" onclick='view($id,$feed_id)'><nobr>$updated_fmt&nbsp;</nobr></td>";
+                                       print "<td class=\"hlUpdated\" onclick='view($id)'><nobr>$updated_fmt&nbsp;</nobr></td>";
 
                                        print "<td class='hlMarkedPic'>$score_pic</td>";
 
 
                                        print "</span></div>";
 
-                                       if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
-                                               $line["content_preview"] = preg_replace("/href=/i", 
-                                                       "target=\"_blank\" href=", $line["content_preview"]);
-                                       }
-
                                        if ($show_excerpt) {
                                                print "<div class=\"cdmExcerpt\" id=\"CEXC-$id\"
                                                        onclick=\"cdmExpandArticle($id)\"
                                                onclick=\"cdmClicked($id)\"
                                                id=\"CICD-$id\" $cdm_cstyle>";
 
+                                       if ($line["orig_feed_id"]) {
+
+                                               $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
+                                               WHERE id = ".$line["orig_feed_id"]);
+
+                                               if (db_num_rows($tmp_result) != 0) {
+
+                                               print "<div clear='both'>";
+                                               print __("Originally from:");
+
+                                               print "&nbsp;";
+
+                                               $tmp_line = db_fetch_assoc($tmp_result);
+
+                                               print "<a target='_blank' 
+                                                       href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
+                                                       $tmp_line['title'] . "</a>";
+
+                                               print "&nbsp;";
+
+                                               print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                               print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
+
+                                               print "</div>";
+                                       }
+                               }
+
 //                                     print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
 
                                        print "<div id=\"POSTNOTE-$id\">";
 
                                $always_display_enclosures = db_fetch_result($tmp_result, 0, "always_display_enclosures");
 
-                               if ($always_display_enclosures || !preg_match("/img/i", $article_content)) {
-                                       foreach ($entries as $entry) {
-                                               if (preg_match("/image/", $entry["type"])) {
-                                                       print "<p><img 
-                                                               alt=\"".htmlspecialchars($entry["filename"])."\"
-                                                               src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
+                               if (!get_pref($link, "STRIP_IMAGES")) {
+                                       if ($always_display_enclosures || !preg_match("/img/i", $article_content)) {
+                                               foreach ($entries as $entry) {
+                                                       if (preg_match("/image/", $entry["type"])) {
+                                                               print "<p><img 
+                                                                       alt=\"".htmlspecialchars($entry["filename"])."\"
+                                                                       src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
+                                                       }
                                                }
                                        }
                                }
                }
 
                $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
-               $url_path .= "/backend.php?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
+               $url_path .= "/backend.php?op=publish&key=" . 
+                       get_pref($link, "_PREFS_PUBLISH_KEY", $_SESSION["uid"]);
 
                return $url_path;
        }
         * @return void
         */
        function clear_feed_articles($link, $id) {
-               $result = db_query($link, "DELETE FROM ttrss_user_entries
+
+               if ($id != 0) {
+                       $result = db_query($link, "DELETE FROM ttrss_user_entries
                        WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
+               } else {
+                       $result = db_query($link, "DELETE FROM ttrss_user_entries
+                       WHERE feed_id IS NULL AND marked = false AND owner_uid = " . $_SESSION["uid"]);
+               }
 
                $result = db_query($link, "DELETE FROM ttrss_entries WHERE 
                        (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
 
                $num_tags = 0;
 
-               if ($_SESSION["theme"] == "3pane") {
+/*             if (get_user_theme_path($link) == "3pane") {
                        $tag_limit = 3;
                } else {
                        $tag_limit = 6;
-               }
+               } */
+
+               $tag_limit = 6;
 
                $formatted_tags = array();
 
 
                return $str;
        }
+
+       function toggle_collapse_cat($link, $cat_id) {
+               if ($cat_id > 0) {
+                       db_query($link, "UPDATE ttrss_feed_categories SET
+                               collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " . 
+                               $_SESSION["uid"]);
+               } else {
+                       $pref_name = '';
+
+                       switch ($cat_id) {
+                       case -1:
+                               $pref_name = '_COLLAPSED_SPECIAL';
+                               break;
+                       case -2:
+                               $pref_name = '_COLLAPSED_LABELS';
+                               break;
+                       case 0:
+                               $pref_name = '_COLLAPSED_UNCAT';
+                               break;
+                       }
+
+                       if ($pref_name) {
+                               if (get_pref($link, $pref_name)) {
+                                       set_pref($link, $pref_name, 'false');
+                               } else {
+                                       set_pref($link, $pref_name, 'true');
+                               }
+                       }
+               }
+       }
+
+       function remove_feed($link, $id, $owner_uid) {
+
+               if ($id > 0) {
+
+                       /* save starred articles in Archived feed */
+
+                       db_query($link, "BEGIN");
+
+                       /* prepare feed if necessary */
+
+                       $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
+                               WHERE id = '$id'");
+
+                       if (db_num_rows($result) == 0) {
+                               db_query($link, "INSERT INTO ttrss_archived_feeds 
+                                       (id, owner_uid, title, feed_url, site_url)
+                               SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
+                               WHERE id = '$id'");
+                       }
+
+                       db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL,
+                               orig_feed_id = '$id' WHERE feed_id = '$id' AND 
+                                       marked = true AND owner_uid = $owner_uid");
+
+                       /* remove the feed */
+
+                       db_query($link, "DELETE FROM ttrss_feeds 
+                                       WHERE id = '$id' AND owner_uid = $owner_uid");
+
+                       db_query($link, "COMMIT");
+
+/*                     if (file_exists(ICONS_DIR . "/$id.ico")) {
+                               unlink(ICONS_DIR . "/$id.ico");
+                       } */
+
+                       ccache_remove($link, $id, $owner_uid);
+
+               } else {
+                       label_remove($link, -11-$id, $owner_uid);
+                       ccache_remove($link, -11-$id, $owner_uid);
+               }
+       }
+
+       function remove_feed_category($link, $id, $owner_uid) {
+
+               db_query($link, "DELETE FROM ttrss_feed_categories
+                       WHERE id = '$id' AND owner_uid = $owner_uid");
+
+               ccache_remove($link, $id, $owner_uid, true);
+       }
+
+       function archive_article($link, $id, $owner_uid) {
+               db_query($link, "BEGIN");
+
+               $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
+                       WHERE ref_id = '$id' AND owner_uid = $owner_uid");
+
+               if (db_num_rows($result) != 0) {
+
+                       /* prepare the archived table */
+
+                       $feed_id = (int) db_fetch_result($result, 0, "feed_id");
+
+                       if ($feed_id) {
+                               $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
+                                       WHERE id = '$feed_id'");
+
+                               if (db_num_rows($result) == 0) {
+                                       db_query($link, "INSERT INTO ttrss_archived_feeds 
+                                               (id, owner_uid, title, feed_url, site_url)
+                                       SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
+                                       WHERE id = '$feed_id'");
+                               }
+
+                               db_query($link, "UPDATE ttrss_user_entries 
+                                       SET orig_feed_id = feed_id, feed_id = NULL
+                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+                       }
+               }
+
+               db_query($link, "COMMIT");
+       }
+
+       function getArticleFeed($link, $id) {
+               $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries 
+                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+
+               if (db_num_rows($result) != 0) {
+                       return db_fetch_result($result, 0, "feed_id");
+               } else {
+                       return 0;
+               }
+       }
+
+       function make_url_from_parts($parts) {
+               $url = $parts['scheme'] . '://' . $parts['host'];
+
+               if ($parts['path']) $url .= $parts['path'];
+               if ($parts['query']) $url .= '?' . $parts['query'];
+
+               return $url;
+       }
+
+       function validate_feed_url($url) {
+               $parts = parse_url($url);
+
+               return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https');
+
+       }
+
 ?>