]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
compat theme: properly reflow container height
[tt-rss.git] / functions.php
index 829650b782e1cd8b6dc137acbaa5917aeb840510..1b92f94409ef62065c8d5e2c361f35a4b6437457 100644 (file)
@@ -8,6 +8,12 @@
 
        require_once 'config.php';
 
+       if (DB_TYPE == "pgsql") {
+               define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
+       } else {
+               define('SUBSTRING_FOR_DATE', 'SUBSTRING');
+       }
+
        /**
         * Return available translations names.
         * 
@@ -19,7 +25,8 @@
                                        "auto"  => "Detect automatically",
                                        "en_US" => "English",
                                        "fr_FR" => "Français",
-                                       "nb_NO" => "Norsk Bokmål",
+                                       "hu_HU" => "Magyar (Hungarian)",
+                                       "nb_NO" => "Norwegian bokmål",
                                        "ru_RU" => "Русский",
                                        "pt_BR" => "Portuguese/Brazil",
                                        "zh_CN" => "Simplified Chinese");
                        }
 
                        if ($lang) {
-                               _setlocale(LC_MESSAGES, $lang);
+                               if (defined('LC_MESSAGES')) {
+                                       _setlocale(LC_MESSAGES, $lang);
+                               } else if (defined('LC_ALL')) {
+                                       _setlocale(LC_ALL, $lang);
+                               } else {
+                                       die("can't setlocale(): please set ENABLE_TRANSLATIONS to false in config.php");
+                               }
                                _bindtextdomain("messages", "locale");
                                _textdomain("messages");
                                _bind_textdomain_codeset("messages", "UTF-8");
@@ -86,7 +99,9 @@
         */
        function _debug($msg) {
                $ts = strftime("%H:%M:%S", time());
-               $ts = "$ts/" . posix_getpid();
+               if (function_exists('posix_getpid')) {
+                       $ts = "$ts/" . posix_getpid();
+               }
                print "[$ts] $msg\n";
        } // function _debug
 
                }
 
                $result = db_query($link, "SELECT feed_url,id,
-                       SUBSTRING(last_updated,1,19) AS last_updated,
+                       ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
                        update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
                        ORDER BY $q_order");
 
                                _debug("update_rss_feed: loading filters...");
                        }
 
-                       $filters = array();
-
-                       $result = db_query($link, "SELECT reg_exp,
-                               ttrss_filter_types.name AS name,
-                               ttrss_filter_actions.name AS action,
-                               inverse,
-                               action_param
-                               FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE                                        
-                                       enabled = true AND
-                                       owner_uid = $owner_uid AND
-                                       ttrss_filter_types.id = filter_type AND
-                                       ttrss_filter_actions.id = action_id AND
-                               (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
-
-                       while ($line = db_fetch_assoc($result)) {
-                               if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
-
-                               $filter["reg_exp"] = $line["reg_exp"];
-                               $filter["action"] = $line["action"];
-                               $filter["action_param"] = $line["action_param"];
-                               $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
-                       
-                               array_push($filters[$line["name"]], $filter);
-                       }
+                       $filters = load_filters($link, $feed, $owner_uid);
 
                        if ($use_simplepie) {
                                $iterator = $rss->get_items();
                                $entry_link = strip_tags($entry_link);
 
                                if ($use_simplepie) {
-                                       $entry_content = $item->get_description();
+                                       $entry_content = $item->get_content();
+                                       if (!$entry_content) $entry_content = $item->get_description();
                                } else {
                                        $entry_content = $item["content:escaped"];
 
                                        $entry_comments = strip_tags($item->data["comments"]);
                                        if ($item->get_author()) {
                                                $entry_author_item = $item->get_author();
-                                               $entry_author = $entry_author_item->get_name();                                                 
+                                               $entry_author = $entry_author_item->get_name();
+                                               if (!$entry_author) $entry_author = $entry_author_item->get_email();
+
+                                               $entry_author = db_escape_string($entry_author);
                                        }
                                } else {
                                        $entry_comments = strip_tags($item["comments"]);
 
                                # sanitize content
                                
-//                             $entry_content = sanitize_rss($entry_content);
+                               $entry_content = sanitize_article_content($entry_content);
+                               $entry_title = sanitize_article_content($entry_title);
 
                                if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
 
                                $result = db_query($link, "SELECT 
                                                id,content_hash,no_orig_date,title,
-                                               substring(date_entered,1,19) as date_entered,
-                                               substring(updated,1,19) as updated,
+                                               ".SUBSTRING_FOR_DATE."(date_entered,1,19) as date_entered,
+                                               ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
                                                num_comments
                                        FROM 
                                                ttrss_entries 
 
 //                                     error_reporting (DEFAULT_ERROR_LEVEL);
 
+                                       $score = calculate_article_score($article_filters);
+
+                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
+                                               _debug("update_rss_feed: initial score: $score");
+                                       }
+
                                        $result = db_query($link,
                                                "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
                                                        ref_id = '$ref_id' AND owner_uid = '$owner_uid'
                                                        $last_read_qpart = 'NOW()';
                                                }                                               
 
-                                               if (find_article_filter($article_filters, 'mark')) {
+                                               if (find_article_filter($article_filters, 'mark') || $score > 1000) {
                                                        $marked = 'true';
                                                } else {
                                                        $marked = 'false';
 
                                                $result = db_query($link,
                                                        "INSERT INTO ttrss_user_entries 
-                                                               (ref_id, owner_uid, feed_id, unread, last_read, marked, published) 
+                                                               (ref_id, owner_uid, feed_id, unread, last_read, marked, 
+                                                                       published, score) 
                                                        VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
-                                                               $last_read_qpart, $marked, $published)");
+                                                               $last_read_qpart, $marked, $published, '$score')");
 
                                                $result = db_query($link, 
                                                        "SELECT int_id FROM ttrss_user_entries WHERE
                return false;
        }
 
+       function calculate_article_score($filters) {
+               $score = 0;
+
+               foreach ($filters as $f) {
+                       if ($f[0] == "score") {
+                               $score += $f[1];
+                       };
+               }
+               return $score;
+       }
+
+
        function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
                $rtl_content = false, $last_updated = false, $last_error = false) {
 
                                }
                                fclose($fp);
                                return true;
+                       } else {
+                               return false;
                        }
                }
-               return false;
+               return true; // consider the file always locked and skip the test
        }
 
        function make_lockfile($filename) {
                $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
                error_reporting (DEFAULT_ERROR_LEVEL);
 
-               if (flock($fp, LOCK_EX)) {
-                       $stamp = fgets($fp);
-                       flock($fp, LOCK_UN);
-                       fclose($fp);
-                       return $stamp;
+               if ($fp) {
+                       if (flock($fp, LOCK_EX)) {
+                               $stamp = fgets($fp);
+                               flock($fp, LOCK_UN);
+                               fclose($fp);
+                               return $stamp;
+                       } else {
+                               return false;
+                       }
                } else {
                        return false;
                }
                $old_counters = $_SESSION["fctr_last_value"];
 
 /*             $result = db_query($link, "SELECT id,last_error,parent_feed,
-                       SUBSTRING(last_updated,1,19) AS last_updated,
+                       ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
                        (SELECT count(id) 
                                FROM ttrss_entries,ttrss_user_entries 
                                WHERE feed_id = ttrss_feeds.id AND 
 
                $query = "SELECT ttrss_feeds.id,
                                ttrss_feeds.title,
-                               SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated, 
+                               ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, 
                                last_error, 
                                COUNT(ttrss_entries.id) AS count 
                        FROM ttrss_feeds 
                } else if ($id == -3) {
                        return __("Fresh articles");
                } else if ($id < -10) {
-                       $label_id = -10 - $id;
+                       $label_id = -$id - 11;
                        $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
                        if (db_num_rows($result) == 1) {
                                return db_fetch_result($result, 0, "description");
                print "<param key=\"hide_read_shows_special\" value=\"" . 
                        (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
 
+               print "<param key=\"hide_feedlist\" value=\"" .
+                       (int) get_pref($link, "HIDE_FEEDLIST") . "\"/>";
+
                print "</init-params>";
        }
 
                                $order_by = "updated DESC";
                        }
 
+                       $order_by = "score DESC, $order_by";
+
                        if ($override_order) {
                                $order_by = $override_order;
                        }
        
                        } else if ($feed == -1) {
                                $feed_title = __("Starred articles");
+                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed == -2) {
                                $feed_title = __("Published articles");
+                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed == -3) {
                                $feed_title = __("Fresh articles");
+                               if ($search) {  $feed_title = __("Searched for") . " $search ($feed_title)"; }
                        } else if ($feed < -10) {
                                $label_id = -$feed - 11;
                                $result = db_query($link, "SELECT description FROM ttrss_labels
                                        $offset_query_part = "OFFSET $offset";
                                }
 
+                               if ($vfeed_query_part && defined('_VFEED_GROUP_BY_FEED')) {
+                                       if (!$override_order) {
+                                               $order_by = "ttrss_feeds.title, $order_by";     
+                                       }
+
+                                       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";
+                                       }
+                               }
+
                                $query = "SELECT 
                                                guid,
                                                ttrss_entries.id,ttrss_entries.title,
                                                updated,
                                                unread,feed_id,marked,published,link,last_read,
-                                               SUBSTRING(last_read,1,19) as last_read_noms,
+                                               ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
                                                $vfeed_query_part
                                                $content_query_part
-                                               SUBSTRING(updated,1,19) as updated_noms,
-                                               author
+                                               ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
+                                               author,score
                                        FROM
                                                ttrss_entries,ttrss_user_entries,ttrss_feeds
                                        WHERE
-                                       ttrss_feeds.hidden = false AND
+                                       $group_limit_part
+                                       ttrss_feeds.hidden = false AND 
                                        ttrss_user_entries.feed_id = ttrss_feeds.id AND
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                                        ttrss_user_entries.owner_uid = '$owner_uid' AND
                                        updated,
                                        unread,feed_id,
                                        marked,link,last_read,                          
-                                       SUBSTRING(last_read,1,19) as last_read_noms,
+                                       ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
                                        $vfeed_query_part
                                        $content_query_part
-                                       SUBSTRING(updated,1,19) as updated_noms
+                                       ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms
                                        FROM
                                                ttrss_entries,ttrss_user_entries,ttrss_tags
                                        WHERE
-                                               ref_id = ttrss_entries.id AND
+                                               ref_id = ttrss_entries.id AND 
                                                ttrss_user_entries.owner_uid = '$owner_uid' AND
                                                post_int_id = int_id AND tag_name = '$feed' AND
                                                $view_query_part
                                ttrss_user_entries.ref_id,
                                link,
                                SUBSTRING(content, 1, 120) AS excerpt,
-                               SUBSTRING(last_updated,1,19) AS last_updated
+                               ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
                        FROM 
                                ttrss_user_entries,ttrss_entries,ttrss_feeds 
                        WHERE 
        function print_headline_subtoolbar($link, $feed_site_url, $feed_title, 
                        $bottom = false, $rtl_content = false, $feed_id = 0,
                        $is_cat = false, $search = false, $match_on = false,
-                       $search_mode = false, $offset = 0, $limit = 0) {
+                       $search_mode = false, $offset = 0, $limit = 0, 
+                       $dashboard_menu = 0, $disable_feed = 0, $feed_small_icon = 0) {
 
                        $user_page_offset = $offset + 1;
 
 
                        }
 
-                       if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
+                       if (!$dashboard_menu) {
 
-                               print "<td class=\"headlineActions$rtl_cpart\">
-                                       <ul class=\"headlineDropdownMenu\">
-                                       <li class=\"top2\">
-                                       ".__('Select:')."
-                                               <a href=\"$sel_all_link\">".__('All')."</a>,
-                                               <a href=\"$sel_unread_link\">".__('Unread')."</a>,
-                                               <a href=\"$sel_none_link\">".__('None')."</a></li>
-                                       <li class=\"vsep\">&nbsp;</li>
-                                       <li class=\"top\">".__('Toggle')."<ul>
-                                               <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
-                                               <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
-                                               <li onclick=\"$tog_published_link\">".__('Published')."</li>
-                                               </ul></li>
-                                       <li class=\"vsep\">&nbsp;</li>
-                                       <li class=\"top\">".__('Mark as read')."<ul>
-                                               <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
-                                               <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->";
+                               if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
 
-                               if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
+                                       print "<td class=\"headlineActions$rtl_cpart\">
+                                               <ul class=\"headlineDropdownMenu\">
+                                               <li class=\"top2\">
+                                               ".__('Select:')."
+                                                       <a href=\"$sel_all_link\">".__('All')."</a>,
+                                                       <a href=\"$sel_unread_link\">".__('Unread')."</a>,
+                                                       <a href=\"$sel_none_link\">".__('None')."</a></li>
+                                               <li class=\"vsep\">&nbsp;</li>
+                                               <li class=\"top\">".__('Actions...')."<ul>
+                                                       <li><span class=\"insensitive\">".__('Selection toggle:')."</span></li>
+                                                       <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
+                                                       <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
+                                                       <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
+                                                       <li><span class=\"insensitive\">--------</span></li>
+                                                       <li><span class=\"insensitive\">".__('Mark as read:')."</span></li>
+                                                       <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
+
+/*                             if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
+       
+                                       print "
+                                               <li onclick=\"catchupRelativeToArticle(0)\">&nbsp;&nbsp;".__("Above active article")."</li>
+                                               <li onclick=\"catchupRelativeToArticle(1)\">&nbsp;&nbsp;".__("Below active article")."</li>";
+                               } else {
+                                       print "
+                                               <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Above active article")."</span></li>
+                                               <li><span class=\"insensitive\">&nbsp;&nbsp;".__("Below active article")."</span></li>";
 
-                               print "
-                                               <li><span class=\"insensitive\">--------</span></li>
-                                               <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
-                                               <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
-                                               <li><span class=\"insensitive\">--------</span></li>";
-                               }
+                               } */
 
-                               print "
-                                               <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
-                                       ";
+                               print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed')."</li>";
 
-                                       $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
+                               print "<li><span class=\"insensitive\">--------</span></li>";
+                               print "<li><span class=\"insensitive\">".__('Other actions:')."</span></li>";
+               
 
-                                       if ($limit != 0 && !$search && $enable_pagination) {
-                                               print "
-                                               <li class=\"vsep\">&nbsp;</li>
-                                               <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
-                                                       <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
-                                                       <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
-                                                       </ul>";
-                                               }
+                               if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
+                                       print "
+                                               <li onclick=\"javascript:labelFromSearch('$search', '$search_mode',
+                                                       '$match_on', '$feed_id', '$is_cat');\">&nbsp;&nbsp;
+                                                       ".__('Search to label')."</li>";
+                               } else {
+                                       print "<li><span class=\"insensitive\">&nbsp;&nbsp;".__('Search to label')."</li>";
 
+                               }
+                               
+                               print   "</ul></li></ul>";
+                               print "</td>"; 
+       
+                               } else {
+                                       // old style subtoolbar:
+       
+                                       print "<td class=\"headlineActions$rtl_cpart\">".
+                                               __('Select:')."
+                                                                       <a href=\"$sel_all_link\">".__('All')."</a>,
+                                                                       <a href=\"$sel_unread_link\">".__('Unread')."</a>,
+                                                                       <a href=\"$sel_none_link\">".__('None')."</a>
+                                                       &nbsp;&nbsp;".
+                                                       __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
+                                                               <a href=\"$tog_marked_link\">".__('Starred')."</a>
+                                                       &nbsp;&nbsp;".
+                                                       __('Mark as read:')."
+                                                               <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
+                                                               <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
+       
                                        if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
-                                               print "
-                                                       <li class=\"vsep\">&nbsp;</li>
-                                                       <li class=\"top3\">
-                                                       <a href=\"javascript:labelFromSearch('$search', '$search_mode',
-                                                               '$match_on', '$feed_id', '$is_cat');\">
-                                                               ".__('Convert to label')."</a></td>";
+       
+                                               print "&nbsp;&nbsp;
+                                                               <a href=\"javascript:labelFromSearch('$search', '$search_mode',
+                                                                       '$match_on', '$feed_id', '$is_cat');\">
+                                                               ".__('Convert to label')."</a>";
                                        }
-                                       print " 
-                                       </td>"; 
-
-                       } else {
-                       // old style subtoolbar:
-
-                               print "<td class=\"headlineActions$rtl_cpart\">".
-                                       __('Select:')."
-                                                               <a href=\"$sel_all_link\">".__('All')."</a>,
-                                                               <a href=\"$sel_unread_link\">".__('Unread')."</a>,
-                                                               <a href=\"$sel_none_link\">".__('None')."</a>
-                                               &nbsp;&nbsp;".
-                                               __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
-                                                       <a href=\"$tog_marked_link\">".__('Starred')."</a>
-                                               &nbsp;&nbsp;".
-                                               __('Mark as read:')."
-                                                       <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
-                                                       <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
-
-                               if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
-
-                                       print "&nbsp;&nbsp;
-                                                       <a href=\"javascript:labelFromSearch('$search', '$search_mode',
-                                                               '$match_on', '$feed_id', '$is_cat');\">
-                                                       ".__('Convert to label')."</a>";
+       
+                                       print "</td>";  
+       
                                }
+                       } else { // dashboard menu actions
 
-                               print "</td>";  
-
+                               // not implemented
+                               print "</td>";
                        }
 
-/*                     if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
-                               print "<td class=\"headlineActions$rtl_cpart\">
-                                       <a href=\"javascript:labelFromSearch('$search', '$search_mode',
-                                                       '$match_on', '$feed_id', '$is_cat');\">
-                                               ".__('Convert to Label')."</a></td>";
-} */
-
                        print "<td class=\"headlineTitle$rtl_cpart\">";
 
-                       print "<span class=\"headlineInnerTitle\">";
+                       print "<span id=\"subtoolbar_search\" 
+                               style=\"display : none\">Search: <input 
+                               id=\"subtoolbar_search_box\"
+                               onblur=\"javascript:enableHotkeys();\" 
+                               onfocus=\"javascript:disableHotkeys();\"
+                               onchange=\"subtoolbarSearch()\"
+                               onkeyup=\"subtoolbarSearch()\" type=\"search\"></span>";
+
+                       print "<span id=\"subtoolbar_ftitle\">";
 
                        if ($feed_site_url) {
                                if (!$bottom) {
                                print " [$user_page_offset] ";
                        }
 
-                       print "</span>";
-
-                       if (!$bottom) {
+                       if (!$bottom && !$disable_feed) {
                                print "
                                        <a target=\"_new\" 
                                                href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
                                                <img class=\"noborder\" 
                                                        alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
                                        </a>";
+                       } else if ($feed_small_icon) {
+                               print "<img class=\"noborder\" alt=\"\" src=\"images/$feed_small_icon\">";
                        }
-                               
+
+                       print "</span>";
+
                        print "</td>";
                        print "</tr></table>";
 
                        printCategoryHeader($link, -1, $cat_hidden, false);
                }
 
+               if (defined('_ENABLE_DASHBOARD')) {
+                       printFeedEntry(-4, "virt", __("Dashboard"), 0, 
+                               "images/tag.png", $link);
+               }
+
                $num_starred = getFeedUnread($link, -1);
                $num_published = getFeedUnread($link, -2);
                $num_fresh = getFeedUnread($link, -3);
                        $age_qpart = getMaxAgeSubquery();
 
                        $result = db_query($link, "SELECT ttrss_feeds.*,
-                               SUBSTRING(last_updated,1,19) AS last_updated_noms,
+                               ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
                                (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
                                        WHERE feed_id = ttrss_feeds.id AND unread = true
                                                AND $age_qpart
                }
 
                $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
-                       SUBSTRING(updated,1,16) as updated,
+                       ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
                        (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
                        num_comments,
                        author
                        $entry_author = $line["author"];
 
                        if ($entry_author) {
-                               $entry_author = __(" - by ") . $entry_author;
+                               $entry_author = __(" - ") . $entry_author;
                        }
 
                        $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), 
                        print $line["content"];
 
                        $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
-                               post_id = '$id'");
+                               post_id = '$id' AND content_url != ''");
 
                        if (db_num_rows($result) > 0) {
                                print "<div class=\"postEnclosures\">";
                                while ($line = db_fetch_assoc($result)) {
 
                                        $url = $line["content_url"];
+                                       $ctype = $line["content_type"];
+
+                                       if (!$ctype) $ctype = __("unknown type");
 
                                        $filename = substr($url, strrpos($url, "/")+1);
 
                                        $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
-                                               $filename . " (" . $line["content_type"] . ")" . "</a>";
+                                               $filename . " (" . $ctype . ")" . "</a>";
 
                                        array_push($entries, $entry);
                                }
                        error_reporting (DEFAULT_ERROR_LEVEL);
        
                        $num_unread = 0;
-       
+                       $cur_feed_title = '';
+
                        while ($line = db_fetch_assoc($result)) {
 
                                $class = ($lnum % 2) ? "even" : "odd";
                                                100);
                                }
 
+                               $score = $line["score"];
+
+                               $score_pic = get_score_pic($score);
+
+                               $score_title = __("(Click to change)");
+
+                               $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\" 
+                                       onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">";
+
+                               if ($score > 500) {
+                                       $hlc_suffix = "H";
+                               } else if ($score < -100) {
+                                       $hlc_suffix = "L";
+                               } else {
+                                       $hlc_suffix = "";
+                               }
+
                                $entry_author = $line["author"];
 
                                if ($entry_author) {
-                                       $entry_author = " - by $entry_author";
+                                       $entry_author = " - $entry_author";
                                }
 
                                if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
+
+                                       if (defined('_VFEED_GROUP_BY_FEED')) {
+                                               if ($line["feed_title"] != $cur_feed_title) {
+/*                                                     print "<tr class='feedTitle'><td colspan='7'>".
+                                                               $line["feed_title"].
+                                                               " (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
+                                                               "more</a>)</td></tr>"; */
+
+                                                       print "<tr class='feedTitle'><td colspan='7'>".
+                                                               "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
+                                                               $line["feed_title"]."</a>:</td></tr>";
+
+                                                       $cur_feed_title = $line["feed_title"];
+                                               }
+                                       }
                                        
                                        print "<tr class='$class' id='RROW-$id'>";
                
 #                                                              truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
 #                                      } else {                        
 
-                                       print "<td class='hlContent' valign='middle'>";
+                                       print "<td class='hlContent$hlc_suffix' valign='middle'>";
 
-                                       print "<a href=\"javascript:view($id,$feed_id);\">" .
+                                       print "<a id=\"RTITLE-$id\" href=\"javascript:view($id,$feed_id);\">" .
                                                $line["title"];
 
                                        if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
 #                                                      <a href=\"javascript:viewfeed($feed_id, '', false)\">".
 #                                                      $line["feed_title"]."</a>       
 
-                                       if ($line["feed_title"]) {                      
-                                               print "<span class=\"hlFeed\">
-                                                       (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
-                                                       $line["feed_title"]."</a>)
-                                               </span>";
+                                       if (!defined('_VFEED_GROUP_BY_FEED')) {
+                                               if ($line["feed_title"]) {                      
+                                                       print "<span class=\"hlFeed\">
+                                                               (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
+                                                               $line["feed_title"]."</a>)
+                                                       </span>";
+                                               }
                                        }
 
 
 #                                      }
                                        
                                        print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
-               
+
+                                       print "<td class='hlMarkedPic'>$score_pic</td>";
+       
                                        print "</tr>";
 
                                } else {
-                                       
+
+                                       if (defined('_VFEED_GROUP_BY_FEED')) {
+                                               if ($line["feed_title"] != $cur_feed_title) {
+                                                       print "<div class='cdmFeedTitle'>".
+                                                               "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
+                                                               $line["feed_title"]."</a></div>";
+                                                       $cur_feed_title = $line["feed_title"];
+                                               }
+                                       }
+
                                        if ($is_unread) {
                                                $add_class = "Unread";
                                        } else {
 
                                        $expand_cdm = get_pref($link, 'CDM_EXPANDED');
 
-                                       if ($expand_cdm) {
+                                       if ($expand_cdm && $score >= -100) {
                                                $cdm_cstyle = "";
                                        } else {
                                                $cdm_cstyle = "style=\"display : none\"";
 
                                        print "<div class=\"cdmHeader\">";
 
-                                       print "<div class=\"articleUpdated\">$updated_fmt</div>";
-                                       
-                                       print "<a class=\"title\" 
+                                       print "<div class=\"articleUpdated\">$updated_fmt $score_pic</div>";
+
+                                       print "<span id=\"RTITLE-$id\" class=\"titleWrap$hlc_suffix\"><a class=\"title\" 
                                                onclick=\"javascript:toggleUnread($id, 0)\"
-                                               target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
+                                               target=\"_blank\" href=\"".$line["link"]."\">".$line["title"]."</a>
+                                               ";
 
                                        print $entry_author;
 
-                                       if (!$expand_cdm) {
+                                       if (!$expand_cdm || $score < -100) {
                                                print "&nbsp;<a id=\"CICH-$id\" 
                                                        href=\"javascript:cdmExpandArticle($id)\">
                                                        (".__('Show article').")</a>";
                                        } 
 
 
-                                       if ($line["feed_title"]) {      
-                                               print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
+                                       if (!defined('_VFEED_GROUP_BY_FEED')) {
+                                               if ($line["feed_title"]) {      
+                                                       print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
+                                               }
                                        }
 
-                                       print "</div>";
+                                       print "</span></div>";
 
                                        if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
                                                $line["content_preview"] = preg_replace("/href=/i", 
                                        print "<div class=\"cdmContent\" id=\"CICD-$id\" $cdm_cstyle>";
 
 //                                     print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
+
                                        print $line["content_preview"];
 
                                        $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
-                                               post_id = '$id'");
+                                               post_id = '$id' AND content_url != ''");
 
                                        if (db_num_rows($e_result) > 0) {
                                print "<div class=\"cdmEnclosures\">";
                                while ($e_line = db_fetch_assoc($e_result)) {
 
                                        $url = $e_line["content_url"];
+                                       $ctype = $e_line["content_type"];
+                                       if (!$ctype) $ctype = __("unknown type");
 
                                        $filename = substr($url, strrpos($url, "/")+1);
 
                                        $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
-                                               $filename . " (" . $e_line["content_type"] . ")" . "</a>";
+                                               $filename . " (" . $ctype . ")" . "</a>";
 
                                        array_push($entries, $entry);
                                }
 
 
                } else {
-                       if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
+                       $message = "";
+
+                       switch ($view_mode) {
+                               case "unread":
+                                       $message = __("No unread articles found to display.");
+                                       break;
+                               case "marked":
+                                       $message = __("No starred articles found to display.");
+                                       break;
+                               default:
+                                       $message = __("No articles found to display.");
+                       }
+
+                       if (!$offset) print "<div class='whiteBox'>$message</div>";
                }
 
                if (!$offset) {
 
        function article_publish_url($link) {
 
-               $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+               $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' :  'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
 
                $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
 
         * @return string The Mozilla Firefox feed adding URL.
         */
        function add_feed_url() {
-               $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+               $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' :  'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
                $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
                return $url_path;
        } // function add_feed_url
                $query_limit = "";
                if($limit) $query_limit = sprintf("LIMIT %d", $limit);
 
+               $random_qpart = sql_random_function();
+
                // We search for feed needing update.
                $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
-                               SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
+                               ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
                                ttrss_feeds.update_interval 
                        FROM 
                                ttrss_feeds, ttrss_users, ttrss_user_prefs
                                AND ttrss_users.id = ttrss_user_prefs.owner_uid
                                AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
                                $login_thresh_qpart $update_limit_qpart
-                                $updstart_thresh_qpart
-                       ORDER BY ttrss_feeds.last_updated ASC $query_limit");
+                        $updstart_thresh_qpart
+                       ORDER BY $random_qpart $query_limit");
 
                $user_prefs_cache = array();
 
 
                        // We setup a alarm to alert if the feed take more than 300s to update.
                        // => HANG alarm.
-                       if(!$from_http) pcntl_alarm(300);
+                       if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(300);
                        update_rss_feed($link, $line["feed_url"], $line["id"], true);
                        // Cancel the alarm (the update went well)
-                       if(!$from_http) pcntl_alarm(0);
+                       if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(0);
 
                        sleep(1); // prevent flood (FIXME make this an option?)
                }
 
        } // function update_daemon_common
 
+       function generate_dashboard_feed($link) {
+
+               print "<div id=\"headlinesContainer\">";
+
+               print_headline_subtoolbar($link, "", "Dashboard", 
+                       false, false, -4, false, false, false,
+                       false, 0, 0, true, true, "tag.png");
+
+               print "<div id=\"headlinesInnerContainer\" class=\"dashboard\">";
+               print "<div>There is <b>666</b> unread articles in <b>666</b> feeds.</div>";
+               print "</div>";
+
+               print "</div>";
+
+               print "]]></headlines>";
+               print "<headlines-count value=\"0\"/>";
+               print "<headlines-unread value=\"0\"/>";
+               print "<disable-cache value=\"1\"/>";
+
+               print "<articles>";
+               print "</articles>";
+       }
+
+       function sanitize_article_content($text) {
+               # we don't support CDATA sections in articles, they break our own escaping
+               $text = preg_replace("/\[\[CDATA/", "", $text);
+               $text = preg_replace("/\]\]\>/", "", $text);
+               return $text;
+       }
+
+       function load_filters($link, $feed, $owner_uid, $action_id = false) {
+               $filters = array();
+
+               if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
+
+               $result = db_query($link, "SELECT reg_exp,
+                       ttrss_filter_types.name AS name,
+                       ttrss_filter_actions.name AS action,
+                       inverse,
+                       action_param
+                       FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE                                        
+                               enabled = true AND
+                               $ftype_query_part
+                               owner_uid = $owner_uid AND
+                               ttrss_filter_types.id = filter_type AND
+                               ttrss_filter_actions.id = action_id AND
+                               (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
+
+               while ($line = db_fetch_assoc($result)) {
+                       if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
+                               $filter["reg_exp"] = $line["reg_exp"];
+                               $filter["action"] = $line["action"];
+                               $filter["action_param"] = $line["action_param"];
+                               $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
+                       
+                               array_push($filters[$line["name"]], $filter);
+                       }
+
+               return $filters;
+       }
+
+       function get_score_pic($score) {
+               if ($score > 0) { 
+                       return "score_high.png"; 
+               } else if ($score < 0) {
+                       return "score_low.png"; 
+               } else { 
+                       return "score_neutral.png"; 
+               }
+       }
 ?>