]> git.wh0rd.org - tt-rss.git/blobdiff - backend.php
force scripts refresh on every reload
[tt-rss.git] / backend.php
index 775c17c39b281f84790f6225810af2ca4ec0d0a4..bc3b388dfe1a56a74f582f32cac3bba6ec8d4648 100644 (file)
@@ -1,16 +1,33 @@
 <?
-       session_start();
-
-       if ($_GET["debug"]) {
+       require_once "sessions.php";
+       require_once "backend-rpc.php";
+       
+       header("Cache-Control: no-cache, must-revalidate");
+       header("Pragma: no-cache");
+       header("Expires: -1");
+       
+/*     if ($_GET["debug"]) {
                define('DEFAULT_ERROR_LEVEL', E_ALL);
        } else {
                define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
        }
-
-       error_reporting(DEFAULT_ERROR_LEVEL);
+       
+       error_reporting(DEFAULT_ERROR_LEVEL); */
 
        $op = $_REQUEST["op"];
 
+       define('SCHEMA_VERSION', 7);
+
+       require_once "sanity_check.php";
+       require_once "config.php";
+
+       $err_msg = check_configuration_variables();
+
+       if ($err_msg) {
+               print "Fatal error: $err_msg";
+               exit;
+       }
+
        if ((!$op || $op == "rpc" || $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
                header("Content-Type: application/xml");
        }
 
                if ($op == "rpc") {
                        print "<error error-code=\"6\"/>";
+               } else {
+                       print "
+                       <html><body>
+                               <p>Error: Not logged in.</p>
+                               <script type=\"text/javascript\">
+                                       if (parent.window != 'undefined') {
+                                               parent.window.location = \"login.php\";         
+                                       } else {
+                                               window.location = \"login.php\";
+                                       }
+                               </script>
+                       </body></html>
+                       ";
                }
                exit;
        }
                exit;
        }
 
-       define('SCHEMA_VERSION', 4);
-
-       require_once "sanity_check.php";
-       require_once "config.php";
        require_once "db.php";
        require_once "db-prefs.php";
        require_once "functions.php";
        require_once "magpierss/rss_fetch.inc";
 
+       $purge_intervals = array(
+               0  => "Default",
+               -1 => "Never purge",
+               5  => "1 week",
+               14 => "2 weeks",
+               31 => "1 month",
+               60 => "2 months",
+               90 => "3 months");
+
+       $update_intervals = array(
+               0   => "Default",
+               -1  => "Disable updates",
+               30  => "30 minutes",
+               60  => "1 hour",
+               240 => "4 hours",
+               720 => "12 hours",
+               1440 => "Daily",
+               10080 => "Weekly");
+
        $script_started = getmicrotime();
+       $script_dt_add = time();
 
        $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
 
                pg_query("set client_encoding = 'utf-8'");
        }
 
-       $fetch = $_GET["fetch"];
-
-       setcookie("ttrss_icons_url", ICONS_URL);
-
-       if (!sanity_check($link)) { return; }
-
-       function getAllCounters($link) {
-               getLabelCounters($link);
-               getFeedCounters($link);
-               getTagCounters($link);
-               getGlobalCounters($link);
-               if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                       getCategoryCounters($link);
-               }
-       }       
-
-       function getCategoryCounters($link) {
-               $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) 
-                               FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id 
-                                       AND unread = true)) AS unread FROM ttrss_feeds 
-                       WHERE 
-                               owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
-
-               while ($line = db_fetch_assoc($result)) {
-                       $line["cat_id"] = sprintf("%d", $line["cat_id"]);
-                       print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
-                               $line["unread"]."\"/>";
-               }
-       }
-
-       function getFeedUnread($link, $feed) {
-               $n_feed = sprintf("%d", $feed);
-       
-               if ($n_feed == -1) {
-                       $match_part = "marked = true";
-               } else if ($feed > 0) {
-                       $match_part = "feed_id = '$n_feed'";
-               } else if ($feed < -10) {
-                       $label_id = -$feed - 11;
-
-                       $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
-                               id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
-
-                       $match_part = db_fetch_result($result, 0, "sql_exp");
-               }
-
-               if ($match_part) {
-               
-                       $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"]);
-                               
+       if ($_SESSION["uid"]) {
+               if (get_pref($link, "HIDE_READ_FEEDS") == "true") {
+                       setcookie("ttrss_vf_hreadf", 1);
                } 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
-                                       ttrss_tags.owner_uid = " . $_SESSION["uid"]);
-               }
-               
-               $unread = db_fetch_result($result, 0, "unread");
-               return $unread;
-       }
-
-       /* FIXME this needs reworking */
-
-       function getGlobalCounters($link) {
-               $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
-                       WHERE unread = true AND 
-                       ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       owner_uid = " . $_SESSION["uid"]);
-               $c_id = db_fetch_result($result, 0, "c_id");
-               print "<counter type=\"global\" id='global-unread' counter='$c_id'/>";
-       }
-
-       function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
-
-               if ($smart_mode) {
-                       if (!$_SESSION["tctr_last_value"]) {
-                               $_SESSION["tctr_last_value"] = array();
-                       }
-               }
-
-               $old_counters = $_SESSION["tctr_last_value"];
-
-               $tctrs_modified = false;
-
-/*             $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
-                       FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
-                       ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
-                       post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name 
-               UNION
-                       select tag_name,0 as count FROM ttrss_tags
-                       WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
-
-               $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) 
-                       FROM ttrss_user_entries WHERE int_id = post_int_id 
-                               AND unread = true)) AS count FROM ttrss_tags 
-                       WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
-                       
-               $tags = array();
-
-               while ($line = db_fetch_assoc($result)) {
-                       $tags[$line["tag_name"]] += $line["count"];
-               }
-
-               foreach (array_keys($tags) as $tag) {
-                       $unread = $tags[$tag];                  
-
-                       $tag = htmlspecialchars($tag);
-
-                       if (!$smart_mode || $old_counters[$tag] != $unread) {                   
-                               $old_counters[$tag] = $unread;
-                               $tctrs_modified = true;
-                               print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
-                       }
-
-               } 
-
-               if ($smart_mode && $tctrs_modified) {
-                       $_SESSION["tctr_last_value"] = $old_counters;
-               }
-
-       }
-
-       function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
-
-               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(id) as count FROM ttrss_entries,ttrss_user_entries
-                       WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                       unread = true AND owner_uid = ".$_SESSION["uid"]);
-
-               $count = db_fetch_result($result, 0, "count");
-
-               print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
-
-               $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
-                       ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
-       
-               while ($line = db_fetch_assoc($result)) {
-
-                       $id = -$line["id"] - 11;
-
-                       error_reporting (0);
-
-                       $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
-                               WHERE (" . $line["sql_exp"] . ") AND unread = true AND 
-                               ttrss_user_entries.ref_id = ttrss_entries.id AND 
-                               owner_uid = ".$_SESSION["uid"]);
-
-                       $count = db_fetch_result($tmp_result, 0, "count");
-
-                       if (!$smart_mode || $old_counters[$id] != $count) {     
-                               $old_counters[$id] = $count;
-                               $lctrs_modified = true;
-                               print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
-                       }
-
-                       error_reporting (DEFAULT_ERROR_LEVEL);
-               }
+                       setcookie("ttrss_vf_hreadf", 0);
+               }  
 
-               if ($smart_mode && $lctrs_modified) {
-                       $_SESSION["lctr_last_value"] = $old_counters;
-               }
+               setcookie('ttrss_vf_refresh', FEEDS_FRAME_REFRESH);
+               setcookie('ttrss_vf_daemon', ENABLE_UPDATE_DAEMON);
        }
 
-/*     function getFeedCounter($link, $id) {
-       
-               $result = db_query($link, "SELECT 
-                               count(id) as count,last_error
-                       FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
-                       WHERE feed_id = '$id' AND unread = true
-                       AND ttrss_user_entries.feed_id = ttrss_feeds.id
-                       AND ttrss_user_entries.ref_id = ttrss_entries.id");
-       
-                       $count = db_fetch_result($result, 0, "count");
-                       $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
-                       
-                       print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";           
-       } */
-
-       function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
-
-               if ($smart_mode) {
-                       if (!$_SESSION["fctr_last_value"]) {
-                               $_SESSION["fctr_last_value"] = array();
-                       }
-               }
-
-               $old_counters = $_SESSION["fctr_last_value"];
-
-               $result = db_query($link, "SELECT id,last_error,parent_feed,
-                       (SELECT count(id) 
-                               FROM ttrss_entries,ttrss_user_entries 
-                               WHERE feed_id = ttrss_feeds.id AND 
-                                       ttrss_user_entries.ref_id = ttrss_entries.id
-                               AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
-                       FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
-                               AND parent_feed IS NULL");
-
-               $fctrs_modified = false;
-
-               while ($line = db_fetch_assoc($result)) {
-               
-                       $id = $line["id"];
-                       $count = $line["count"];
-                       $last_error = htmlspecialchars($line["last_error"]);
-       
-                       $has_img = is_file(ICONS_DIR . "/$id.ico");
-
-                       $tmp_result = db_query($link,
-                               "SELECT 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");
-                       
-                       if (db_num_rows($tmp_result) > 0) {                             
-                               while ($l = db_fetch_assoc($tmp_result)) {
-                                       $count += $l["unread"];
-                               }
-                       }
-
-                       if (!$smart_mode || $old_counters[$id] != $count) {
-                               $old_counters[$id] = $count;
-                               $fctrs_modified = true;
-
-                               if ($last_error) {
-                                       $error_part = "error=\"$last_error\"";
-                               } else {
-                                       $error_part = "";
-                               }
-
-                               if ($has_img) {
-                                       $has_img_part = "hi=\"$has_img\"";
-                               } else {
-                                       $has_img_part = "";
-                               }                               
+       $fetch = $_GET["fetch"];
 
-                               print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part/>";
-                       }
-               }
+//     setcookie("ttrss_icons_url", ICONS_URL);
 
-               if ($smart_mode && $fctrs_modified) {
-                       $_SESSION["fctr_last_value"] = $old_counters;
-               }
-       }
+       if (!sanity_check($link)) { return; }
 
        function outputFeedList($link, $tags = false) {
 
                                        type=\"text/css\" href=\"tt-rss_compact.css\"/>";
                }
 
-               print "<script type=\"text/javascript\" src=\"functions.js\"></script>
-                       <script type=\"text/javascript\" src=\"feedlist.js\"></script>
+               print "<script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
+                       <script type=\"text/javascript\" src=\"feedlist.js?$script_dt_add\"></script>
                        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
                        <!--[if gte IE 5.5000]>
                        <script type=\"text/javascript\" src=\"pngfix.js\"></script>
                        <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
                        <![endif]-->
-                       </head><body onload=\"init()\">";
+                       </head><body>
+                       <script type=\"text/javascript\">
+                               if (document.addEventListener) {
+                                       document.addEventListener(\"DOMContentLoaded\", init, null);
+                               }
+                               window.onload = init;
+                       </script>";
 
-               print "<ul class=\"feedList\" id=\"feedList\">";
+               print "<ul class=\"feedList\" id=\"feedList\">\n";
 
                $owner_uid = $_SESSION["uid"];
 
                                "images/mark_set.png", $link);
 
                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                               print "</li></ul>";
+                               print "</ul>\n";
                        }
 
                        if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
 
                                if (db_num_rows($result) > 0) {
                                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                                               print "</li></ul>";
+                                               print "</ul>";
                                        }
                                }
 
                        }
 
                        $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 
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                        $total_unread = 0;
 
                        $category = "";
+
+                       $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
        
                        while ($line = db_fetch_assoc($result)) {
                        
                                $total = $line["total"];
                                $unread = $line["unread"];
 
+                               if (get_pref($link, 'HEADLINES_SMART_DATE')) {
+                                       $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
+                               } else {
+                                       $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
+                               }
+
+                               $rtl_content = sql_bool_to_bool($line["rtl_content"]);
+
+                               if ($rtl_content) {
+                                       $rtl_tag = "dir=\"RTL\"";
+                               } else {
+                                       $rtl_tag = "";
+                               }
+
                                $tmp_result = db_query($link,
                                        "SELECT id,COUNT(unread) AS unread
                                        FROM ttrss_feeds LEFT JOIN ttrss_user_entries 
                                if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
                                
                                        if ($category) {
-                                               print "</li></ul></li>";
+                                               print "</ul></li>";
                                        }
                                
                                        $category = $tmp_category;
                                        $cat_id = sprintf("%d", $cat_id);
                                        
                                        print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
-                                               <a href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category
+                                               <a href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
+                                                       <a href=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
                                                        <span id=\"FCATCTR-$cat_id\" 
-                                                       class=\"$catctr_class\">($cat_unread unread)$ellipsis</span></a></li>";
+                                                       class=\"$catctr_class\">($cat_unread unread)$ellipsis</span>
+                                                       </a></li>";
 
                                        // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
                                        // -> keyboard navigation, etc.
                                }
        
                                printFeedEntry($feed_id, $class, $feed, $unread, 
-                                       "icons/$feed_id.ico", $link);
+                                       "icons/$feed_id.ico", $link, $rtl_content, 
+                                       $last_updated, $line["last_error"]);
        
                                ++$lnum;
                        }
 
                print "</ul>";
 
-       }
-
-
-       if ($op == "rpc") {
-
-               $subop = $_GET["subop"];
-
-               if ($subop == "getLabelCounters") {
-                       $aid = $_GET["aid"];            
-                       print "<rpc-reply>";
-                       getLabelCounters($link);
-                       if ($aid) {
-                               getFeedCounter($link, $aid);
-                       }
-                       print "</rpc-reply>";
-               }
-
-               if ($subop == "getFeedCounters") {
-                       print "<rpc-reply>";
-                       getFeedCounters($link);
-                       print "</rpc-reply>";
-               }
-
-               if ($subop == "getAllCounters") {
-                       print "<rpc-reply>";
-                       getAllCounters($link);
-                       print "</rpc-reply>";
-               }
-
-               if ($subop == "mark") {
-                       $mark = $_GET["mark"];
-                       $id = db_escape_string($_GET["id"]);
-
-                       if ($mark == "1") {
-                               $mark = "true";
-                       } else {
-                               $mark = "false";
-                       }
-
-                       // FIXME this needs collision testing
-
-                       $result = db_query($link, "UPDATE ttrss_user_entries SET marked = $mark
-                               WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-               }
-
-               if ($subop == "updateFeed") {
-                       $feed_id = db_escape_string($_GET["feed"]);
-
-                       $result = db_query($link, 
-                               "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
-                                       AND owner_uid = " . $_SESSION["uid"]);
-
-                       if (db_num_rows($result) > 0) {                 
-                               $feed_url = db_fetch_result($result, 0, "feed_url");
-                               update_rss_feed($link, $feed_url, $feed_id);
-                       }
-
-                       print "<rpc-reply>";
-                       getFeedCounter($link, $feed_id);
-                       print "</rpc-reply>";
-                       
-                       return;
-               }
-
-               if ($subop == "forceUpdateAllFeeds" || $subop == "updateAllFeeds") {
-               
-                       update_all_feeds($link, $subop == "forceUpdateAllFeeds");                       
-
-                       $omode = $_GET["omode"];
-
-                       if (!$omode) $omode = "tfl";
-
-                       print "<rpc-reply>";
-                       if (strchr($omode, "l")) getLabelCounters($link);
-                       if (strchr($omode, "f")) getFeedCounters($link);
-                       if (strchr($omode, "t")) getTagCounters($link);
-                       if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                               getCategoryCounters($link);
-                       }
-                       getGlobalCounters($link);
-                       print "</rpc-reply>";
-               }
-       
-               /* GET["cmode"] = 0 - mark as read, 1 - as unread, 2 - toggle */
-               if ($subop == "catchupSelected") {
-
-                       $ids = split(",", db_escape_string($_GET["ids"]));
-
-                       $cmode = sprintf("%d", $_GET["cmode"]);
-
-                       foreach ($ids as $id) {
-
-                               if ($cmode == 0) {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       unread = false,last_read = NOW()
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-                               } else if ($cmode == 1) {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       unread = true
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-                               } else {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       unread = NOT unread,last_read = NOW()
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+               print '
+                       <script type="text/javascript">
+                               /* for IE */
+                               function statechange() {
+                                       if (document.readyState == "interactive") init();
                                }
-                       }
-                       print "<rpc-reply>";
-                       getAllCounters($link);
-                       print "</rpc-reply>";
-               }
-
-               if ($subop == "markSelected") {
-
-                       $ids = split(",", db_escape_string($_GET["ids"]));
-
-                       $cmode = sprintf("%d", $_GET["cmode"]);
-
-                       foreach ($ids as $id) {
-
-                               if ($cmode == 0) {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       marked = false
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-                               } else if ($cmode == 1) {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       marked = true
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-                               } else {
-                                       db_query($link, "UPDATE ttrss_user_entries SET 
-                                       marked = NOT marked
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+                       
+                               if (document.readyState) {      
+                                       if (document.readyState == "interactive" || document.readyState == "complete") {
+                                               init();
+                                       } else {
+                                               document.onreadystatechange = statechange;
+                                       }
                                }
-                       }
-                       print "<rpc-reply>";
-                       getAllCounters($link);
-                       print "</rpc-reply>";
-               }
-
-               if ($subop == "sanityCheck") {
-                       sanity_check();
-               }
-
-               if ($subop == "globalPurge") {
-
-                       print "<rpc-reply>";
-                       global_purge_old_posts($link, true);
-                       print "</rpc-reply>";
+                       </script></body></html>';
+       }
 
-               }
 
+       if ($op == "rpc") {
+               handle_rpc_request($link);
        }
        
        if ($op == "feeds") {
 
        if ($op == "view") {
 
-               $id = $_GET["id"];
-               $feed_id = $_GET["feed"];
+               $id = db_escape_string($_GET["id"]);
+               $feed_id = db_escape_string($_GET["feed"]);
+
+               $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
+                       WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
+
+               if (db_num_rows($result) == 1) {
+                       $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
+               } else {
+                       $rtl_content = false;
+               }
+
+               if ($rtl_content) {
+                       $rtl_tag = "dir=\"RTL\"";
+                       $rtl_class = "RTL";
+               } else {
+                       $rtl_tag = "";
+                       $rtl_class = "";
+               }
 
                $result = db_query($link, "UPDATE ttrss_user_entries 
                        SET unread = false,last_read = NOW() 
                        WHERE ref_id = '$id' AND feed_id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
 
-               $addheader = $_GET["addheader"];
-
                $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
                        SUBSTRING(updated,1,16) as updated,
                        (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
-                       num_comments
+                       num_comments,
+                       author
                        FROM ttrss_entries,ttrss_user_entries
                        WHERE   id = '$id' AND ref_id = id");
 
-               if ($addheader) {
-                       print "<html><head>
-                               <title>Tiny Tiny RSS : Article $id</title>
-                               <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
-
-                       $user_theme = $_SESSION["theme"];
-                       if ($user_theme) { 
-                               print "<link rel=\"stylesheet\" type=\"text/css\" 
-                                       href=\"themes/$user_theme/theme.css\">";
-                       }
+               print "<html><head>
+                       <title>Tiny Tiny RSS : Article $id</title>
+                       <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
 
-                       if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
-                               print "<link rel=\"stylesheet\" type=\"text/css\" 
-                                       href=\"tt-rss_compact.css\"/>";
-                       } else {
-                               print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
-                                               type=\"text/css\" href=\"tt-rss_compact.css\"/>";
-                       }
+               $user_theme = $_SESSION["theme"];
+               if ($user_theme) { 
+                       print "<link rel=\"stylesheet\" type=\"text/css\" 
+                               href=\"themes/$user_theme/theme.css\">";
+               }
 
-                       print "<script type=\"text/javascript\" src=\"functions.js\"></script>
-                               <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
-                               </head><body>";
+               if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
+                       print "<link rel=\"stylesheet\" type=\"text/css\" 
+                               href=\"tt-rss_compact.css\"/>";
+               } else {
+                       print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
+                                       type=\"text/css\" href=\"tt-rss_compact.css\"/>";
                }
 
+               print "<script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
+                       <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
+                       </head><body $rtl_tag>";
+
                if ($result) {
 
                        $line = db_fetch_assoc($result);
 
                        print "<div class=\"postHeader\"><table width=\"100%\">";
 
-                       print "<tr><td><a href=\"" . $line["link"] . "\">" . $line["title"] . "</a></td>";
+                       $entry_author = $line["author"];
+
+                       if ($entry_author) {
+                               $entry_author = " - by $entry_author";
+                       }
+
+                       print "<tr><td><a href=\"" . $line["link"] . "\">" . $line["title"] . 
+                               "</a>$entry_author</td>";
 
                        $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), 
                                strtotime($line["updated"]));
                
-                       print "<td class=\"postDate\">$parsed_updated</td>";
+                       print "<td class=\"postDate$rtl_class\">$parsed_updated</td>";
                                                
                        print "</tr>";
 
                                print "<div id=\"allEntryTags\">Tags: $f_tags_str</div>";
                        }
 
+                       if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
+                               $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
+                       }
+
                        print $line["content"] . "</div>";
                        
                        print "</div>";
                        </script>";
                }
 
-               if ($addheader) {
-                       print "</body></html>";
-               }
+               print "</body></html>";
        }
 
        if ($op == "viewfeed") {
 
-               $feed = $_GET["feed"];
-               $skip = $_GET["skip"];
-               $subop = $_GET["subop"];
-               $view_mode = $_GET["view"];
-               $addheader = $_GET["addheader"];
-               $limit = $_GET["limit"];
-
-               if (!$feed) {
-                       return;
-               }
+               $feed = db_escape_string($_GET["feed"]);
+               $skip = db_escape_string($_GET["skip"]);
+               $subop = db_escape_string($_GET["subop"]);
+               $view_mode = db_escape_string($_GET["view"]);
+               $limit = db_escape_string($_GET["limit"]);
+               $cat_view = db_escape_string($_GET["cat"]);
 
                if (!$skip) $skip = 0;
 
                if ($subop == "undefined") $subop = "";
 
-               if ($addheader) {
-                       print "<html><head>
-                               <title>Tiny Tiny RSS : Feed $feed</title>
-                               <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
-
-                       $user_theme = $_SESSION["theme"];
-                       if ($user_theme) { 
-                               print "<link rel=\"stylesheet\" type=\"text/css\" 
-                                       href=\"themes/$user_theme/theme.css\">";
-                       }
+               print "<html><head>
+                       <title>Tiny Tiny RSS : Feed $feed</title>
+                       <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">";
 
-                       if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
-                               print "<link rel=\"stylesheet\" 
-                                               type=\"text/css\" href=\"tt-rss_compact.css\"/>";
+               $user_theme = $_SESSION["theme"];
+               if ($user_theme) { 
+                       print "<link rel=\"stylesheet\" type=\"text/css\" 
+                               href=\"themes/$user_theme/theme.css\">";
+               }
 
-                       } else {
-                               print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
-                                               type=\"text/css\" href=\"tt-rss_compact.css\"/>";
-                       }
+               if (get_pref($link, 'USE_COMPACT_STYLESHEET')) {
+                       print "<link rel=\"stylesheet\" 
+                                       type=\"text/css\" href=\"tt-rss_compact.css\"/>";
 
-                       print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
-                               <script type=\"text/javascript\" src=\"functions.js\"></script>
-                               <script type=\"text/javascript\" src=\"viewfeed.js\"></script>
-                               <!--[if gte IE 5.5000]>
-                               <script type=\"text/javascript\" src=\"pngfix.js\"></script>
-                               <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
-                               <![endif]-->
-                               </head><body onload='init()'>";
+               } else {
+                       print "<link title=\"Compact Stylesheet\" rel=\"alternate stylesheet\" 
+                                       type=\"text/css\" href=\"tt-rss_compact.css\"/>";
                }
 
-               if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
-
-                       $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
-                               WHERE id = '$feed'");
+               if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
+       
+                       $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
+                               WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
 
-                       $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
+                       if (db_num_rows($result) == 1) {
+                               $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
+                       } else {
+                               $rtl_content = false;
+                       }
+       
+                       if ($rtl_content) {
+                               $rtl_tag = "dir=\"RTL\"";
+                       } else {
+                               $rtl_tag = "";
+                       }
+               } else {
+                       $rtl_tag = "";
+                       $rtl_content = false;
+               }
 
-                       update_rss_feed($link, $feed_url, $feed);
+               print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">  
+                       <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
+                       <script type=\"text/javascript\" src=\"viewfeed.js?$script_dt_add\"></script>
+                       <!--[if gte IE 5.5000]>
+                       <script type=\"text/javascript\" src=\"pngfix.js\"></script>
+                       <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
+                       <![endif]-->
+                       </head><body $rtl_tag>
+                       <script type=\"text/javascript\">
+                       if (document.addEventListener) {
+                               document.addEventListener(\"DOMContentLoaded\", init, null);
+                       }
+                       window.onload = init;
+                       </script>";
 
+               if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
+                       update_generic_feed($link, $feed, $cat_view);
                }
 
                if ($subop == "MarkAllRead")  {
-
-                       if (sprintf("%d", $feed) != 0) {
-                       
-                               if ($feed > 0) {
-
-                                       $tmp_result = db_query($link, "SELECT id 
-                                               FROM ttrss_feeds WHERE parent_feed = '$feed'
-                                               ORDER BY cat_id,title");
-
-                                       $parent_ids = array();
-
-                                       if (db_num_rows($tmp_result) > 0) {
-                                               while ($p = db_fetch_assoc($tmp_result)) {
-                                                       array_push($parent_ids, "feed_id = " . $p["id"]);
-                                               }
-
-                                               $children_qpart = implode(" OR ", $parent_ids);
-                                               
-                                               db_query($link, "UPDATE ttrss_user_entries 
-                                                       SET unread = false,last_read = NOW() 
-                                                       WHERE (feed_id = '$feed' OR $children_qpart) 
-                                                       AND owner_uid = " . $_SESSION["uid"]);
-
-                                       } else {                                                
-                                               db_query($link, "UPDATE ttrss_user_entries 
-                                                       SET unread = false,last_read = NOW() 
-                                                       WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
-                                       }
-                                               
-                               } else if ($feed < 0 && $feed > -10) { // special, like starred
-
-                                       if ($feed == -1) {
-                                               db_query($link, "UPDATE ttrss_user_entries 
-                                                       SET unread = false,last_read = NOW()
-                                                       WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
-                                       }
-                       
-                               } else if ($feed < -10) { // label
-
-                                       // TODO make this more efficient
-
-                                       $label_id = -$feed - 11;
-
-                                       $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
-                                               WHERE id = '$label_id'");                                       
-
-                                       if ($tmp_result) {
-                                               $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
-
-                                               db_query($link, "BEGIN");
-
-                                               $tmp2_result = db_query($link,
-                                                       "SELECT 
-                                                               int_id 
-                                                       FROM 
-                                                               ttrss_user_entries,ttrss_entries 
-                                                       WHERE
-                                                               ref_id = id AND 
-                                                               $sql_exp AND
-                                                               owner_uid = " . $_SESSION["uid"]);
-
-                                               while ($tmp_line = db_fetch_assoc($tmp2_result)) {
-                                                       db_query($link, "UPDATE 
-                                                               ttrss_user_entries 
-                                                       SET 
-                                                               unread = false, last_read = NOW()
-                                                       WHERE
-                                                               int_id = " . $tmp_line["int_id"]);
-                                               }
-                                                               
-                                               db_query($link, "COMMIT");
-
-/*                                             db_query($link, "UPDATE ttrss_user_entries,ttrss_entries 
-                                                       SET unread = false,last_read = NOW()
-                                                       WHERE $sql_exp
-                                                       AND ref_id = id
-                                                       AND owner_uid = ".$_SESSION["uid"]); */
-                                       }
-                               }
-                       } else { // tag
-                               db_query($link, "BEGIN");
-
-                               $tag_name = db_escape_string($feed);
-
-                               $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
-                                       WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
-
-                               while ($line = db_fetch_assoc($result)) {
-                                       db_query($link, "UPDATE ttrss_user_entries SET
-                                               unread = false, last_read = NOW() 
-                                               WHERE int_id = " . $line["post_int_id"]);
-                               }
-                               db_query($link, "COMMIT");
-                       }
-
+                       catchup_feed($link, $feed, $cat_view);
                }
 
                $search = db_escape_string($_GET["search"]);
                $search_mode = db_escape_string($_GET["smode"]);
 
                if ($search) {
-                       $search_query_part = "(upper(title) LIKE upper('%$search%') 
-                               OR content LIKE '%$search%') AND";
+                       $search_query_part = "(upper(ttrss_entries.title) LIKE upper('%$search%') 
+                               OR ttrss_entries.content LIKE '%$search%') AND";
                } else {
                        $search_query_part = "";
                }
                $view_query_part = "";
 
                if ($view_mode == "Adaptive") {
-                       if ($feed != -1) {
+                       if ($search) {
+                               $view_query_part = " ";
+                       } else if ($feed != -1) {
                                $unread = getFeedUnread($link, $feed);
                                if ($unread > 0) {
                                        $view_query_part = " unread = true AND ";
 
                // override query strategy and enable feed display when searching globally
                if ($search && $search_mode == "All feeds") {
-                       $query_strategy_part = "id > 0";
-                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
-               } else if (sprintf("%d", $feed) == 0) {
+                       $query_strategy_part = "ttrss_entries.id > 0";
+                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
+               } 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) {
-               
+               } else if ($feed >= 0 && $search && $search_mode == "This category") {
+
+                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";         
+
                        $tmp_result = db_query($link, "SELECT id 
-                               FROM ttrss_feeds WHERE parent_feed = '$feed'
-                               ORDER BY cat_id,title");
+                               FROM ttrss_feeds WHERE cat_id = 
+                                       (SELECT cat_id FROM ttrss_feeds WHERE id = '$feed') AND id != '$feed'");
 
-                       $parent_ids = array();
+                       $cat_siblings = array();
 
                        if (db_num_rows($tmp_result) > 0) {
                                while ($p = db_fetch_assoc($tmp_result)) {
-                                       array_push($parent_ids, "feed_id = " . $p["id"]);
+                                       array_push($cat_siblings, "feed_id = " . $p["id"]);
                                }
 
                                $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
-                                       $feed, implode(" OR ", $parent_ids));
-       
-                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                                       $feed, implode(" OR ", $cat_siblings));
+
                        } else {
-                               $query_strategy_part = "feed_id = '$feed'";
+                               $query_strategy_part = "ttrss_entries.id > 0";
+                       }
+                       
+               } else if ($feed >= 0) {
+
+                       if ($cat_view) {
+
+                               if ($feed > 0) {
+                                       $query_strategy_part = "cat_id = '$feed'";
+                               } else {
+                                       $query_strategy_part = "cat_id IS NULL";
+                               }
+
+                               $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+
+                       } else {                
+                               $tmp_result = db_query($link, "SELECT id 
+                                       FROM ttrss_feeds WHERE parent_feed = '$feed'
+                                       ORDER BY cat_id,title");
+       
+                               $parent_ids = array();
+       
+                               if (db_num_rows($tmp_result) > 0) {
+                                       while ($p = db_fetch_assoc($tmp_result)) {
+                                               array_push($parent_ids, "feed_id = " . $p["id"]);
+                                       }
+       
+                                       $query_strategy_part = sprintf("(feed_id = %d OR %s)", 
+                                               $feed, implode(" OR ", $parent_ids));
+       
+                                       $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
+                               } else {
+                                       $query_strategy_part = "feed_id = '$feed'";
+                               }
                        }
                } else if ($feed == -1) { // starred virtual feed
                        $query_strategy_part = "marked = true";
                $feed_title = "";
 
                if ($search && $search_mode == "All feeds") {
-                       $feed_title = "Search results";
-               } else if (sprintf("%d", $feed) == 0) {
+                       $feed_title = "Global search results ($search)";
+               } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
+                       $feed_title = "Feed search results ($search, $feed)";
+               } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
                        $feed_title = $feed;
-               } else if ($feed > 0) {
-                       $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds 
-                               WHERE id = '$feed'");
+               } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
 
-                       $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 ($cat_view) {
+
+                               if ($feed != 0) {                       
+                                       $result = db_query($link, "SELECT title FROM ttrss_feed_categories
+                                               WHERE id = '$feed' AND owner_uid = " . $_SESSION["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"]);
+       
+                               $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 if ($feed == -1) {
                        $feed_title = "Starred articles";
 
                print "<div id=\"headlinesContainer\">";
 
-               if (sprintf("%d", $feed) != 0) {
+               if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
 
-                       if ($feed > 0) {                        
+                       if ($feed >= 0) {
                                $feed_kind = "Feeds";
                        } else {
                                $feed_kind = "Labels";
 //                             $content_query_part = "";
 //                     }
 
-                       $result = db_query($link, "SELECT 
+                       $query = "SELECT 
                                        ttrss_entries.id,ttrss_entries.title,
                                        SUBSTRING(updated,1,16) as updated,
                                        unread,feed_id,marked,link,last_read,
                                $search_query_part
                                $view_query_part
                                $query_strategy_part ORDER BY $order_by
-                               $limit_query_part");
+                               $limit_query_part";
+                               
+                       $result = db_query($link, $query);
+
+                       if ($_GET["debug"]) print $query;
 
                } else {
                        // browsing by tag
                                Could not display feed (query failed). Please check label match syntax or local configuration.</div>";
                        return;
                }
-       
-               if (db_num_rows($result) > 0) {
 
-                       print "<table class=\"headlinesSubToolbar\" 
+               function print_headline_subtoolbar($link, $feed_site_url, $feed_title, 
+                                               $bottom = false, $rtl_content = false) {
+
+                       if (!$bottom) {
+                               $class = "headlinesSubToolbar";
+                               $tid = "headlineActionsTop";
+                       } else {
+                               $class = "invisible";
+                               $tid = "headlineActionsBottom";
+                       }
+
+                       print "<table class=\"$class\" id=\"$tid\"
                                width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
 
+                       if ($rtl_content) {
+                               $rtl_cpart = "RTL";
+                       } else {
+                               $rtl_cpart = "";
+                       }
+
                        if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
 
-                               print "<td class=\"headlineActions\">
+                               print "<td class=\"headlineActions$rtl_cpart\">
                                        Select: 
-                                                               <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
-                                                                       'RROW-', 'RCHK-', true, '', true)\">All</a>,
-                                                               <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
-                                                                       'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
-                                                               <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 
-                                                                       'RROW-', 'RCHK-', false)\">None</a>
+                                                               <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
+                                                               <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
+                                                               <a href='#' onclick=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
                                                &nbsp;&nbsp;
-                                               Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>,
-                                                               <a href=\"javascript:selectionToggleMarked()\">Starred</a>";
+                                               Toggle: <a href='#' onclick=\"javascript:selectionToggleUnread()\">Unread</a>,
+                                                               <a href='#' onclick=\"javascript:selectionToggleMarked()\">Starred</a>";
                                print "</td>";
 
                        } else {
 
-                               print "<td class=\"headlineActions\">
+                               print "<td class=\"headlineActions$rtl_cpart\">
                                        Select: 
-                                                               <a href=\"javascript:cdmSelectArticles('all')\">All</a>,
-                                                               <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
-                                                               <a href=\"javascript:cdmSelectArticles('none')\">None</a>
+                                                               <a href=\"#\" onclick=\"javascript:cdmSelectArticles('all')\">All</a>,
+                                                               <a href=\"#\" onclick=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
+                                                               <a href=\"#\" onclick=\"javascript:cdmSelectArticles('none')\">None</a>
                                                &nbsp;&nbsp;
-                                               Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>,
-                                                               <a href=\"javascript:selectionToggleMarked(true)\">Starred</a>";
+                                               Toggle: <a href=\"#\" onclick=\"javascript:selectionToggleUnread(true)\">Unread</a>,
+                                                               <a href=\"#\" onclick=\"javascript:selectionToggleMarked(true)\">Starred</a>";
                        
                                print "</td>";
 
-
                        }
 
-                       if ($last_error) {
-                                       print "<td align='center' class='small'>
-                                               <a class=\"warning\" href=\"javascript:alert('TT-RSS encountered an error while trying to update this feed.\\n\\n$last_error')\">Could not update this feed.</a></td>";
-                               }       
-
-                       print "<td class=\"headlineTitle\">";
+                       print "<td class=\"headlineTitle$rtl_cpart\">";
                
                        if ($feed_site_url) {
-                               print "<a target=\"_blank\" href=\"$feed_site_url\">$feed_title</a>";
+                               if (!$bottom) {
+                                       $target = "target=\"_blank\"";
+                               }
+                               print "<a $target href=\"$feed_site_url\">$feed_title</a>";
                        } else {
                                print $feed_title;
                        }
                        print "</td>";
                        print "</tr></table>";
 
+               }
+       
+               if (db_num_rows($result) > 0) {
+
+                       print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, 
+                               $rtl_content);
+
                        if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
                                print "<table class=\"headlinesList\" id=\"headlinesList\" 
                                        cellspacing=\"0\" width=\"100%\">";
 
                                if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
                                        $content_preview = truncate_string(strip_tags($line["content_preview"]), 
-                                               200);
+                                               100);
                                }
 
                                if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
                                        if ($line["feed_title"]) {                      
                                                print "<td class='hlContent'>$content_link</td>";
                                                print "<td class='hlFeed'>
-                                                       <a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>&nbsp;</td>";
+                                                       <a href='javascript:viewfeed($feed_id)'>".
+                                                               $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 (get_pref($link, 'SHOW_CONTENT_PREVIEW') && !$rtl_tag) {
                                                        if ($content_preview) {
                                                                print "<span class=\"contentPreview\"> - $content_preview</span>";
                                                        }
 
                                        print "<div style=\"float : right\">$updated_fmt</div>";
                                        
-                                       print "<a href=\"".$line["link"]."\">".$line["title"]."</a>";
+                                       print "<a target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
 
                                        if ($line["feed_title"]) {      
                                                print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
                                print "</table>";
                        }
 
+                       print_headline_subtoolbar($link, 
+                               "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
+
+
                } else {
                        print "<div width='100%' align='center'>No articles found.</div>";
                }
 
                print "<script type=\"text/javascript\">
                        document.onkeydown = hotkey_handler;
+                       // if (parent.daemon_enabled) parent.updateTitle('$feed_title');
                        update_all_counters('$feed');
                </script>";
        
-               if ($addheader) {
-                       print "</body></html>";
-               }
+               print '
+                       <script type="text/javascript">
+                               /* for IE */
+                               function statechange() {
+                                       if (document.readyState == "interactive") init();
+                               }
+                       
+                               if (document.readyState) {      
+                                       if (document.readyState == "interactive" || document.readyState == "complete") {
+                                               init();
+                                       } else {
+                                               document.onreadystatechange = statechange;
+                                       }
+                               }
+                       </script>';
+
+               print "</body></html>";
        }
 
        if ($op == "pref-rpc") {
                }               
 
                if ($subop == "browse") {
+
+                       if (!ENABLE_FEED_BROWSER) {
+                               print "Feed browser is administratively disabled.";
+                               return;
+                       }
                        
                        print "<div class=\"infoBoxContents\">";
 
 
                        print "<tr class='$row_class'><td>Feed URL:</td>";
                        print "<td><input id=\"iedit_link\" value=\"$feed_url\"></td></tr>";
-       
+
                        if (get_pref($link, 'ENABLE_FEED_CATS')) {
 
                                $cat_id = db_fetch_result($result, 0, "cat_id");
                        $row_class = toggleEvenOdd($row_class);
 
                        print "<tr class='$row_class'><td>Update Interval:</td>";
-                       print "<td><input id=\"iedit_updintl\" 
-                               value=\"$update_interval\"></td></tr>";
+//                     print "<td><input id=\"iedit_updintl\" 
+//                             value=\"$update_interval\"></td></tr>";
+
+                       print "<td>";
+
+                       print "<select id=\"iedit_updintl\">";
+                       
+                       foreach (array_keys($update_intervals) as $i) {
+                       
+                               if ($i == $update_interval) {
+                                       $selected = "selected";
+                               } else {
+                                       $selected = "";
+                               }                                       
+                               print "<option $selected id=\"$i\">" . $update_intervals[$i] . "</option>";
+                       }
+                               
+                       print "</select>";
+
+                       print "</td>";
 
                        $row_class = toggleEvenOdd($row_class);
                        print "<tr class='$row_class'><td>Link to:</td>";
 
                        $linked_count = db_fetch_result($tmp_result, 0, "count");
 
-
                        $parent_feed = db_fetch_result($result, 0, "parent_feed");
 
                        if ($linked_count > 0) {
                        }
 
                        $tmp_result = db_query($link, "SELECT id,title FROM ttrss_feeds
-                               WHERE owner_uid = ".$_SESSION["uid"]." $cat_qpart ORDER BY title");
+                               WHERE id != '$feed_id' AND owner_uid = ".$_SESSION["uid"]." 
+                                       $cat_qpart ORDER BY title");
 
                                if (db_num_rows($tmp_result) > 0) {
                                        print "<option disabled>--------</option>";
                        $row_class = toggleEvenOdd($row_class);
 
                        print "<tr class='$row_class'><td>Purge Days:</td>";
-                       print "<td><input id=\"iedit_purgintl\" 
-                               value=\"$purge_interval\"></td></tr>";
+//                     print "<td><input id=\"iedit_purgintl\" 
+//                             value=\"$purge_interval\"></td></tr>";
+
+                       print "<td>";
+
+                       print "<select id=\"iedit_purgintl\">";
+                       
+                       foreach (array_keys($purge_intervals) as $i) {
+                       
+                               if ($i == $purge_interval) {
+                                       $selected = "selected";
+                               } else {
+                                       $selected = "";
+                               }                                       
+                               print "<option $selected id=\"$i\">" . $purge_intervals[$i] . "</option>";
+                       }
+                               
+                       print "</select>";
+
+                       print "</td>";
 
 //                     print "<tr><td colspan=\"2\"><b>Authentication</b></td></tr>";
 
                                $checked = "";
                        }
 
-                       print "<tr class='$row_class'><td>Options:</td>";
+                       print "<tr class='$row_class'><td valign='top'>Options:</td>";
                        print "<td><input type=\"checkbox\" id=\"iedit_private\" 
-                               $checked><label for=\"iedit_private\">Hide from feed browser</label>
-                               </td></tr>";
+                               $checked><label for=\"iedit_private\">Hide from feed browser</label>";
+
+                       $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
+
+                       if ($rtl_content) {
+                               $checked = "checked";
+                       } else {
+                               $checked = "";
+                       }
+
+                       print "<br><input type=\"checkbox\" id=\"iedit_rtl\" 
+                               $checked><label for=\"iedit_rtl\">Right-to-left content</label>";
+                       
+                       print "</td></tr>";
 
                        print "</table>";
                        print "</div>";
                        $auth_login = db_escape_string($_POST["login"]);
                        $auth_pass = db_escape_string($_POST["pass"]);
                        $parent_feed = db_escape_string($_POST["pfeed"]);
-                       $private = db_escape_string($_POST["private"]);
+                       $private = db_escape_string($_POST["is_pvt"]);
+                       $rtl_content = db_escape_string($_POST["is_rtl"]);
 
                        if (strtoupper($upd_intl) == "DEFAULT")
                                $upd_intl = 0;
                                purge_interval = '$purge_intl',
                                auth_login = '$auth_login',
                                auth_pass = '$auth_pass',
-                               private = '$private'                            
-                               WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);                    
+                               private = $private,
+                               rtl_content = $rtl_content
+                               WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
                }
 
                if ($subop == "saveCat") {
                                onchange=\"javascript:addFeed()\"
                                size=\"40\">
                                <input type=\"submit\" class=\"button\"
-                               onclick=\"javascript:addFeed()\" value=\"Add feed\">
-                               &nbsp;
-                               (<a href='javascript:browseFeeds()'>Top 50</a>)
-                       </td><td align='right'>
+                               onclick=\"javascript:addFeed()\" value=\"Add feed\">";
+
+               if (ENABLE_FEED_BROWSER) {
+                       print "&nbsp;(<a href='javascript:browseFeeds()'>Top 50</a>)";
+               }
+               
+               print "</td><td align='right'>
                                <input id=\"feed_search\" size=\"20\"  
                                onchange=\"javascript:updateFeedList()\"
                                value=\"$feed_search\">
                        if (!get_pref($link, 'ENABLE_FEED_CATS')) {
                                print "<tr class=\"title\">
                                        <td width='5%' align='center'>&nbsp;</td>
-                                       <td width='30%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
-                                       <td width='30%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
-                                       <td width='15%'><a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a></td>
-                                       <td width='15%'><a href=\"javascript:updateFeedList('purge_interval')\">Purge Days</a></td></tr>";
+                                       <td width='40%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
+                                       <td width='45%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
+                                       <td width='15%' align='right'><a href=\"javascript:updateFeedList('last_updated')\">Updated</a></td>";
                        }
                        
                        $lnum = 0;
                                $edit_title = htmlspecialchars(db_unescape_string($line["title"]));
                                $edit_link = htmlspecialchars(db_unescape_string($line["feed_url"]));
                                $edit_cat = htmlspecialchars(db_unescape_string($line["category"]));
-       
-                               if ($line["update_interval"] == "0") $line["update_interval"] = "Default";
-                               if ($line["update_interval"] == "-1") $line["update_interval"] = "Disabled";
-                               if ($line["purge_interval"] == "0") $line["purge_interval"] = "Default";
-                               if ($line["purge_interval"] < 0)        $line["purge_interval"] = "Disabled";
 
                                if (!$edit_cat) $edit_cat = "Uncategorized";
 
+                               $last_updated = $line["last_updated"];
+
+                               if (get_pref($link, 'HEADLINES_SMART_DATE')) {
+                                       $last_updated = smart_date_time(strtotime($last_updated));
+                               } else {
+                                       $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
+                                       $last_updated = date($short_date, strtotime($last_updated));
+                               }
 
                                if (get_pref($link, 'ENABLE_FEED_CATS') && $cur_cat_id != $cat_id) {
                                        $lnum = 0;
 
                                        print "<tr class=\"title\">
                                                <td width='5%' align='center'>&nbsp;</td>
-                                               <td width='30%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
-                                               <td width='30%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
-                                               <td width='15%'><a href=\"javascript:updateFeedList('update_interval')\">Update Interval</a></td>
-                                               <td width='15%'><a href=\"javascript:updateFeedList('purge_interval')\">Purge Days</a></td></tr>";
+                                               <td width='40%'><a href=\"javascript:updateFeedList('title')\">Title</a></td>
+                                               <td width='45%'><a href=\"javascript:updateFeedList('feed_url')\">Feed</a></td>
+                                               <td width='15%' align='right'><a href=\"javascript:updateFeedList('last_updated')\">Updated</a></td>";
 
                                        $cur_cat_id = $cat_id;
                                }
                                                $edit_cat . "</a></td>";                
                                } */
 
-                               print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
-                                       $line["update_interval"] . "</a></td>";
+/*                             print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
+                                       $update_intervals[$line["update_interval"]] . "</a></td>";
 
                                print "<td><a href=\"javascript:editFeed($feed_id);\">" . 
-                                       $line["purge_interval"] . "</a></td>";
-       
+                                       $purge_intervals[$line["purge_interval"]] . "</a></td>"; */
+
+                               print "<td align='right'><a href=\"javascript:editFeed($feed_id);\">" . 
+                                       "$last_updated</a></td>";
+
                                print "</tr>";
        
                                ++$lnum;
                                        onclick=\"javascript:categorizeSelectedFeeds()\" value=\"Set category\">";
 
                                }
-
-                               if (get_pref($link, 'ENABLE_PREFS_CATCHUP_UNCATCHUP')) {
-                                       print "
-                                       <input type=\"submit\" class=\"button\" 
-                                               onclick=\"javascript:readSelectedFeeds(true)\" value=\"Mark as read\">
-                                       <input type=\"submit\" class=\"button\" 
-                                               onclick=\"javascript:readSelectedFeeds(false)\" 
-                                               value=\"Mark as unread\">&nbsp;";
-                               }
                                
                                print "
                                        &nbsp;All feeds: <input type=\"submit\" 
                if ($subop == "editSave") {
 
                        $regexp = db_escape_string($_GET["r"]);
-                       $descr = db_escape_string($_GET["d"]);
                        $match = db_escape_string($_GET["m"]);
                        $filter_id = db_escape_string($_GET["id"]);
                        $feed_id = db_escape_string($_GET["fid"]);
                        
                        $result = db_query($link, "UPDATE ttrss_filters SET 
                                reg_exp = '$regexp', 
-                               description = '$descr',
                                feed_id = $feed_id,
                                action_id = '$action_id',
                                filter_type = (SELECT id FROM ttrss_filter_types WHERE
 
                print "</select>&nbsp;";
 
+/*             print "<input type=\"submit\" 
+                       class=\"button\" onclick=\"javascript:testFilter()\" 
+                       value=\"Test filter\"> "; */
+
                print "<input type=\"submit\" 
                        class=\"button\" onclick=\"javascript:addFilter()\" 
                        value=\"Add filter\">";
 
                $result = db_query($link, "SELECT 
                                ttrss_filters.id AS id,reg_exp,
-                               ttrss_filters.description AS description,
                                ttrss_filter_types.name AS filter_type_name,
                                ttrss_filter_types.description AS filter_type_descr,
                                feed_id,
                                                <td width=\"20%\">Filter expression</td>
                                                <td width=\"20%\">Feed</td>
                                                <td width=\"15%\">Match</td>
-                                               <td width=\"15%\">Action</td>
-                                               <td width=\"30%\">Description</td></tr>";
-               
+                                               <td width=\"15%\">Action</td>";
+
                        $lnum = 0;
                        
                        while ($line = db_fetch_assoc($result)) {
                                print "<tr class=\"$class\" $this_row_id>";
        
                                $line["regexp"] = htmlspecialchars($line["reg_exp"]);
-                               $line["description"] = htmlspecialchars($line["description"]);
        
                                if (!$line["feed_title"]) $line["feed_title"] = "All feeds";
        
                                if (!$edit_filter_id || $subop != "edit") {
        
-                                       if (!$line["description"]) $line["description"] = "[No description]";
-       
                                        print "<td align='center'><input onclick='toggleSelectRow(this);' 
                                        type=\"checkbox\" id=\"FICHK-".$line["id"]."\"></td>";
        
                
                                        print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
                                                $line["action_description"] . "</td>";                  
-
-                                       print "<td><a href=\"javascript:editFilter($filter_id);\">" . 
-                                               $line["description"] . "</td>";                 
        
                                } else if ($filter_id != $edit_filter_id) {
        
                                        print "<td>".$line["feed_title"]."</td>";
                                        print "<td>".$line["filter_type_descr"]."</td>";
                                        print "<td>".$line["action_description"]."</td>";               
-                                       print "<td>".$line["description"]."</td>";              
 
                                } else {
        
        
                                        print "</select></td>";
 
-
-                                       print "<td><input id=\"iedit_descr\" value=\"".$line["description"].
-                                               "\"></td>";
-       
                                        print "</td>";
                                }
                                
                        print "<html><head>
                                <title>Tiny Tiny RSS : Help</title>
                                <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
-                               <script type=\"text/javascript\" src=\"functions.js\"></script>
+                               <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
                                </head><body>";
                }
 
                if ($id == "search") {
 
+                       $active_feed_id = db_escape_string($_GET["param"]);
+
                        print "<input id=\"searchbox\" class=\"extSearch\"                      
                        onblur=\"javascript:enableHotkeys()\" onfocus=\"javascript:disableHotkeys()\"
                        onchange=\"javascript:search()\">
                        <select id=\"searchmodebox\">
-                               <option selected>All feeds</option>
-                               <option>This feed</option>
-                       </select>               
+                               <option selected>All feeds</option>";
+                               
+                       if ($active_feed_id) {                          
+                               print "<option>This feed</option>";
+                       } else {
+                               print "<option disabled>This feed</option>";
+                       }
+
+                       if (get_pref($link, 'ENABLE_FEED_CATS')) {
+                               print "<option>This category</option>";
+                       }
+
+                       print "</select>                
                        <input type=\"submit\" 
                                class=\"button\" onclick=\"javascript:search()\" value=\"Search\">
                        <input class=\"button\"
                                print "<p><input class=\"button\" type=\"submit\" 
                                        value=\"Change e-mail\" name=\"subop\">";
 
+                               print "</form>";
+
                                print "<form action=\"backend.php\" method=\"POST\">";
        
                                print "<table width=\"100%\" class=\"prefPrefsList\">";
                                $tmp_user_pwd = make_password(8);
                                $pwd_hash = 'SHA1:' . sha1($tmp_user_pwd);
 
-                               db_query($link, "INSERT INTO ttrss_users (login,pwd_hash,access_level)
-                                       VALUES ('$login', '$pwd_hash', 0)");
+                               db_query($link, "INSERT INTO ttrss_users 
+                                       (login,pwd_hash,access_level,last_login)
+                                       VALUES ('$login', '$pwd_hash', 0, NOW())");
 
 
                                $result = db_query($link, "SELECT id FROM ttrss_users WHERE 
                        $line["last_login"] = date(get_pref($link, 'SHORT_DATE_FORMAT'),
                                strtotime($line["last_login"]));
 
+                       $access_level_names = array(0 => "User", 10 => "Administrator");
+
 /*                     if ($uid == $_SESSION["uid"]) {
 
                                print "<td align='center'><input disabled=\"true\" type=\"checkbox\" 
                                        $line["email"] . "</td>";                       
 
                                print "<td><a href=\"javascript:editUser($uid);\">" . 
-                                       $line["access_level"] . "</td>";                        
+                                       $access_level_names[$line["access_level"]] . "</td>";                   
 
                        } else if ($uid != $edit_uid) {
 
 
                                print "<td>".$line["login"]."</td>";            
                                print "<td>".$line["email"]."</td>";            
-                               print "<td>".$line["access_level"]."</td>";             
+                               print "<td>".$access_level_names[$line["access_level"]]."</td>";
 
                        } else {
 
                                print "<td><input id=\"iedit_email\" value=\"".$line["email"].
                                        "\"></td>";
 
-                               print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
-                                       "\"></td>";
-                                               
+//                             print "<td><input id=\"iedit_ulevel\" value=\"".$line["access_level"].
+//                                     "\"></td>";
+
+                               print "<td>";
+                               print "<select id=\"iedit_ulevel\">";
+                               foreach (array_keys($access_level_names) as $al) {
+                                       if ($al == $line["access_level"]) {
+                                               $selected = "selected";
+                                       } else {
+                                               $selected = "";
+                                       }                                       
+                                       print "<option $selected id=\"$al\">" . 
+                                               $access_level_names[$al] . "</option>";
+                               }
+                               print "</select>";
+                               print "</td>";
+
                        }
                                
                        print "<td>".$line["last_login"]."</td>";               
 
        if ($op == "pref-feed-browser") {
 
+               if (!ENABLE_FEED_BROWSER) {
+                       print "Feed browser is administratively disabled.";
+                       return;
+               }
+
                $subop = $_REQUEST["subop"];
 
                if ($subop == "details") {
                        return;
                }
 
-               print "<div class=\"warning\">Under construction</div>";
-
                $result = db_query($link, "SELECT feed_url,count(id) AS subscribers 
                        FROM ttrss_feeds 
                        WHERE auth_login = '' AND auth_pass = '' AND private = false
 
        }
 
+       function check_configuration_variables() {
+               if (!defined('SESSION_EXPIRE_TIME')) {
+                       return "config: SESSION_EXPIRE_TIME is undefined";
+               }
+
+               if (SESSION_EXPIRE_TIME < 60) {
+                       return "config: SESSION_EXPIRE_TIME is too low (less than 60)";
+               }
+
+               if (SESSION_EXPIRE_TIME < SESSION_COOKIE_LIFETIME_REMEMBER) {
+                       return "config: SESSION_EXPIRE_TIME should be greater or equal to" .
+                               "SESSION_COOKIE_LIFETIME_REMEMBER";
+               }
+
+               if (defined('DISABLE_SESSIONS') && DISABLE_SESSIONS) {
+                       return "config: you have enabled DISABLE_SESSIONS. Please disable this option.";
+               }
+
+               if (DATABASE_BACKED_SESSIONS && SINGLE_USER_MODE) {
+                       return "config: DATABASE_BACKED_SESSIONS is incompatible with SINGLE_USER_MODE";
+               }
+
+               return false;
+       }
+
        db_close($link);
 ?>