]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
fix 'view as rss' not working correctly for search results
[tt-rss.git] / functions.php
index b0c768ff8b77a14d377f508cd854f4964d82233d..6deeef461b5755ad5f29b4293abfe88bd4d02507 100644 (file)
                return $tr;
        }
 
-       if (ENABLE_TRANSLATIONS == true) { // If translations are enabled.
-               require_once "lib/accept-to-gettext.php";
-               require_once "lib/gettext/gettext.inc";
+       require_once "lib/accept-to-gettext.php";
+       require_once "lib/gettext/gettext.inc";
 
-               function startup_gettext() {
+       function startup_gettext() {
 
-                       # Get locale from Accept-Language header
-                       $lang = al2gt(array_keys(get_translations()), "text/html");
+               # Get locale from Accept-Language header
+               $lang = al2gt(array_keys(get_translations()), "text/html");
 
-                       if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
-                               $lang = _TRANSLATION_OVERRIDE_DEFAULT;
-                       }
-
-                       if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
-                               $lang = $_COOKIE["ttrss_lang"];
-                       }
-
-                       /* In login action of mobile version */
-                       if ($_POST["language"] && defined('MOBILE_VERSION')) {
-                               $lang = $_POST["language"];
-                               $_COOKIE["ttrss_lang"] = $lang;
-                       }
+               if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
+                       $lang = _TRANSLATION_OVERRIDE_DEFAULT;
+               }
 
-                       if ($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");
-                               }
+               if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
+                       $lang = $_COOKIE["ttrss_lang"];
+               }
 
-                               if (defined('MOBILE_VERSION')) {
-                                       _bindtextdomain("messages", "../locale");
-                               } else {
-                                       _bindtextdomain("messages", "locale");
-                               }
+               /* In login action of mobile version */
+               if ($_POST["language"] && defined('MOBILE_VERSION')) {
+                       $lang = $_POST["language"];
+                       $_COOKIE["ttrss_lang"] = $lang;
+               }
 
-                               _textdomain("messages");
-                               _bind_textdomain_codeset("messages", "UTF-8");
+               if ($lang) {
+                       if (defined('LC_MESSAGES')) {
+                               _setlocale(LC_MESSAGES, $lang);
+                       } else if (defined('LC_ALL')) {
+                               _setlocale(LC_ALL, $lang);
                        }
-               }
 
-               startup_gettext();
+                       if (defined('MOBILE_VERSION')) {
+                               _bindtextdomain("messages", "../locale");
+                       } else {
+                               _bindtextdomain("messages", "locale");
+                       }
 
-       } else { // If translations are enabled.
-               function __($msg) {
-                       return $msg;
-               }
-               function startup_gettext() {
-                       // no-op
-                       return true;
+                       _textdomain("messages");
+                       _bind_textdomain_codeset("messages", "UTF-8");
                }
-       } // If translations are enabled.
+       }
+
+       startup_gettext();
 
        if (defined('MEMCACHE_SERVER')) {
                $memcache = new Memcache;
 
        //define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
-       define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes
 
        define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
        define('MAGPIE_USER_AGENT', SELF_USER_AGENT);
        require_once "lib/magpierss/rss_fetch.inc";
        require_once 'lib/magpierss/rss_utils.inc';
        require_once 'lib/htmlpurifier/library/HTMLPurifier.auto.php';
+       require_once 'lib/pubsubhubbub/publisher.php';
+       require_once 'lib/pubsubhubbub/subscriber.php';
 
        $config = HTMLPurifier_Config::createDefault();
 
-       $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title],ul,ol,li,h1,h2,h3,h4,s";
+       $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title],ul,ol,li,h1,h2,h3,h4,s,object[classid|type|id|name|width|height|codebase],param[name|value],table,tr,td";
+
+       $config->set('HTML.SafeObject', true);
+       @$config->set('HTML', 'Allowed', $allowed);
+       $config->set('Output.FlashCompat', true);
+       $config->set('Attr.EnableID', true);
+       @$config->set('Cache', 'SerializerPath', CACHE_DIR . "/htmlpurifier");
 
-       $config->set('HTML', 'Allowed', $allowed);
        $purifier = new HTMLPurifier($config);
 
        /**
                if (!$purge_unread) $query_limit = " unread = false AND ";
 
                if (DB_TYPE == "pgsql") {
-/*                     $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
-                               marked = false AND feed_id = '$feed_id' AND
-                               (SELECT date_updated FROM ttrss_entries WHERE
-                                       id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
-
                        $pg_version = get_pgsql_version($link);
 
                        if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
                }
        }
 
-       function fetch_file_contents($url, $type = false, $login = false, $pass = false) {
+       function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false) {
                $login = urlencode($login);
                $pass = urlencode($pass);
 
-               if (function_exists('curl_init')) {
+               if (function_exists('curl_init') && !ini_get("open_basedir")) {
                        $ch = curl_init($url);
 
                        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
                        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-                       curl_setopt($fp, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
+                       curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
+                       curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
+
+                       if ($post_query) {
+                               curl_setopt($ch, CURLOPT_POST, true);
+                               curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
+                       }
 
                        if ($login && $pass)
                                curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
                        $doc = new DOMDocument();
                        $doc->loadHTML($html);
                        $xpath = new DOMXPath($doc);
-                       $entries = $xpath->query('/html/head/link[@rel="shortcut icon"]');
 
+                       $base = $xpath->query('/html/head/base');
+                       foreach ($base as $b) {
+                               $url = $b->getAttribute("href");
+                               break;
+                       }
+
+                       $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]');
                        if (count($entries) > 0) {
                                foreach ($entries as $entry) {
                                        $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
 
                if ($favicon_url && !file_exists($icon_file)) {
                        $contents = fetch_file_contents($favicon_url, "image");
-
                        if ($contents) {
                                $fp = fopen($icon_file, "w");
 
                }
        }
 
-       function update_rss_feed($link, $feed, $ignore_daemon = false) {
+       function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false) {
 
                global $memcache;
 
                                WHERE   f2.feed_url = f1.feed_url AND f2.id = '$feed'");
 
                        while ($line = db_fetch_assoc($result)) {
-                               update_rss_feed_real($link, $line["id"], $ignore_daemon);
+                               update_rss_feed_real($link, $line["id"], $ignore_daemon, $no_cache);
                        }
                } else {
-                       update_rss_feed_real($link, $feed, $ignore_daemon);
+                       update_rss_feed_real($link, $feed, $ignore_daemon, $no_cache);
                }
        }
 
-       function update_rss_feed_real($link, $feed, $ignore_daemon = false) {
+       function update_rss_feed_real($link, $feed, $ignore_daemon = false, $no_cache = false) {
 
                global $memcache;
 
+               $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
+
                if (!$_REQUEST["daemon"] && !$ignore_daemon) {
                        return false;
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: start");
                }
 
 
                        $result = db_query($link, "SELECT id,update_interval,auth_login,
                                feed_url,auth_pass,cache_images,update_method,last_updated,
-                               owner_uid
+                               mark_unread_on_update, owner_uid, update_on_checksum_change,
+                               pubsub_state
                                FROM ttrss_feeds WHERE id = '$feed'");
 
                }
 
                if (db_num_rows($result) == 0) {
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED");
                        }
                        return false;
                $update_method = db_fetch_result($result, 0, "update_method");
                $last_updated = db_fetch_result($result, 0, "last_updated");
                $owner_uid = db_fetch_result($result, 0, "owner_uid");
+               $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result,
+                       0, "mark_unread_on_update"));
+               $update_on_checksum_change = sql_bool_to_bool(db_fetch_result($result,
+                       0, "update_on_checksum_change"));
+               $pubsub_state = db_fetch_result($result, 0, "pubsub_state");
 
                db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
                        WHERE id = '$feed'");
                else
                        $use_simplepie = false;
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update method: $update_method (feed setting: $update_method) (use simplepie: $use_simplepie)\n");
                }
 
 
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: fetching [$fetch_url]...");
                }
 
 
                if ($memcache && $obj = $memcache->get($obj_id)) {
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: data found in memcache.");
                        }
 
 
                        if ($update_method == 3) {
                                $rss = fetch_twitter_rss($link, $fetch_url, $owner_uid);
-                                       } else if ($update_method == 1) {
+                       } else if ($update_method == 1) {
+
+                               define('MAGPIE_CACHE_AGE', get_feed_update_interval($link, $feed) * 60);
+                               define('MAGPIE_CACHE_ON', !$no_cache);
+                               define('MAGPIE_FETCH_TIME_OUT', 60);
+                               define('MAGPIE_CACHE_DIR', CACHE_DIR . "/magpie");
+
                                $rss = @fetch_rss($fetch_url);
                        } else {
-                               if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
-                                       mkdir(SIMPLEPIE_CACHE_DIR);
+                               $simplepie_cache_dir = CACHE_DIR . "/simplepie";
+
+                               if (!is_dir($simplepie_cache_dir)) {
+                                       mkdir($simplepie_cache_dir);
                                }
 
                                $rss = new SimplePie();
 
                                if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("enabling image cache");
                                        }
 
                                        $rss->set_image_handler("image.php", 'i');
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("feed update interval (sec): " .
                                                get_feed_update_interval($link, $feed)*60);
                                }
 
-                               if (is_dir(SIMPLEPIE_CACHE_DIR)) {
-                                       $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
+                               $rss->enable_cache(!$no_cache);
+
+                               if (!$no_cache) {
+                                       $rss->set_cache_location($simplepie_cache_dir);
                                        $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
                                }
 
 
 //             print_r($rss);
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: fetch done, parsing...");
                }
 
 
                if ($fetch_ok) {
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: processing feed data...");
                        }
 
                                $site_url = $rss->channel["link"];
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: checking favicon...");
                        }
 
                                        $feed_title = db_escape_string($rss->channel["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: registering title: $feed_title");
                                }
 
                                db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: loading filters...");
                        }
 
                        $filters = load_filters($link, $feed, $owner_uid);
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
-                               print_r($filters);
-                       }
+//                     if ($debug_enabled) {
+//                             print_r($filters);
+//                     }
 
                        if ($use_simplepie) {
                                $iterator = $rss->get_items();
                                // clear any errors and mark feed as updated if fetched okay
                                // even if it's blank
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: entry iterator is not an array, no articles?");
                                }
 
                                return; // no articles
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) {
+
+                               if ($debug_enabled) _debug("update_rss_feed: checking for PUSH hub...");
+
+                               $feed_hub_url = false;
+                               if ($use_simplepie) {
+                                       $links = $rss->get_links('hub');
+
+                                       if ($links && is_array($links)) {
+                                               foreach ($links as $l) {
+                                                       $feed_hub_url = $l;
+                                                       break;
+                                               }
+                                       }
+
+                               } else {
+                                       $atom = $rss->channel['atom'];
+
+                                       if ($atom) {
+                                               if ($atom['link@rel'] == 'hub') {
+                                                       $feed_hub_url = $atom['link@href'];
+                                               }
+
+                                               if (!$feed_hub_url && $atom['link#'] > 1) {
+                                                       for ($i = 2; $i <= $atom['link#']; $i++) {
+                                                               if ($atom["link#$i@rel"] == 'hub') {
+                                                                       $feed_hub_url = $atom["link#$i@href"];
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
+                                       } else {
+                                               $feed_hub_url = $rss->channel['link_hub'];
+                                       }
+                               }
+
+                               if ($debug_enabled) _debug("update_rss_feed: feed hub url: $feed_hub_url");
+
+                               if ($feed_hub_url && function_exists('curl_init') &&
+                                       !ini_get("open_basedir")) {
+
+                                       $callback_url = get_self_url_prefix() .
+                                               "/backend.php?op=pubsub&id=$feed";
+
+                                       $s = new Subscriber($feed_hub_url, $callback_url);
+
+                                       $rc = $s->subscribe($fetch_url);
+
+                                       if ($debug_enabled)
+                                               _debug("update_rss_feed: feed hub url found, subscribe request sent.");
+
+                                       db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1
+                                               WHERE id = '$feed'");
+                               }
+                       }
+
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: processing articles...");
                        }
 
                                        if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: guid $entry_guid");
                                }
 
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
                                }
 
                                        if (!$entry_link) $entry_link = $item["link"];
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: title $entry_title");
                                }
 
 
                                if (!$num_comments) $num_comments = 0;
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for tags [1]...");
                                }
 
                                                }
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: category tags:");
                                                print_r($additional_tags);
                                        }
                                        }
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for tags [2]...");
                                }
 
                                for ($i = 0; $i < count($entry_tags); $i++)
                                        $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: unfiltered tags found:");
                                        print_r($entry_tags);
                                }
                                $entry_content = sanitize_article_content($entry_content);
                                $entry_title = sanitize_article_content($entry_title);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
                                }
 
 
                                if (db_num_rows($result) == 0) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: base guid not found");
                                        }
 
 
                                if (db_num_rows($result) == 1) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: base guid found, checking for user record");
                                        }
 
                                                $entry_content, $entry_link, $entry_timestamp, $entry_author,
                                                $entry_tags);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: article filters: ");
                                                if (count($article_filters) != 0) {
                                                        print_r($article_filters);
 
                                        $score = calculate_article_score($article_filters);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: initial score: $score");
                                        }
 
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                               if ($debug_enabled) {
                                                        _debug("update_rss_feed: user record not found, creating...");
                                                }
 
                                                        VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
                                                                $last_read_qpart, $marked, $published, '$score', '', '')");
 
+                                               if (PUBSUBHUBBUB_HUB && $published == 'true') {
+                                                       $rss_link = get_self_url_prefix() .
+                                                               "/backend.php?op=rss&id=-2&key=" .
+                                                               get_feed_access_key($link, -2, false, $owner_uid);
+
+                                                       $p = new Publisher(PUBSUBHUBBUB_HUB);
+
+                                                       $pubsub_result = $p->publish_update($rss_link);
+                                               }
+
                                                $result = db_query($link,
                                                        "SELECT int_id FROM ttrss_user_entries WHERE
                                                                ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
                                                        $entry_int_id = db_fetch_result($result, 0, "int_id");
                                                }
                                        } else {
-                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                               if ($debug_enabled) {
                                                        _debug("update_rss_feed: user record FOUND");
                                                }
 
                                                $entry_int_id = db_fetch_result($result, 0, "int_id");
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
                                        }
 
                                        $post_needs_update = false;
+                                       $update_insignificant = false;
 
-                                       if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
-                                               ($content_hash != $orig_content_hash)) {
-//                                             print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
+                                       if ($orig_num_comments != $num_comments) {
                                                $post_needs_update = true;
+                                               $update_insignificant = true;
                                        }
 
-                                       if (db_escape_string($orig_title) != $entry_title) {
+                                       if ($content_hash != $orig_content_hash) {
                                                $post_needs_update = true;
+                                               $update_insignificant = false;
                                        }
 
-                                       if ($orig_num_comments != $num_comments) {
+                                       if (db_escape_string($orig_title) != $entry_title) {
                                                $post_needs_update = true;
+                                               $update_insignificant = false;
                                        }
 
-//                                     this doesn't seem to be very reliable
-//
-//                                     if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
-//                                             $post_needs_update = true;
-//                                     }
-
                                        // if post needs update, update it and mark all user entries
                                        // linking to this post as updated
                                        if ($post_needs_update) {
                                                db_query($link, "UPDATE ttrss_entries
                                                        SET title = '$entry_title', content = '$entry_content',
                                                                content_hash = '$content_hash',
+                                                               updated = '$entry_timestamp_fmt',
                                                                num_comments = '$num_comments'
                                                        WHERE id = '$ref_id'");
 
-                                               if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
-                                                       db_query($link, "UPDATE ttrss_user_entries
-                                                               SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
-                                               } else {
-                                                       db_query($link, "UPDATE ttrss_user_entries
-                                                               SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
+                                               if (!$update_insignificant) {
+                                                       if ($mark_unread_on_update) {
+                                                               db_query($link, "UPDATE ttrss_user_entries
+                                                                       SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
+                                                       } else if ($update_on_checksum_change) {
+                                                               db_query($link, "UPDATE ttrss_user_entries
+                                                                       SET last_read = null WHERE ref_id = '$ref_id'
+                                                                               AND unread = false");
+                                                       }
                                                }
-
                                        }
                                }
 
                                db_query($link, "COMMIT");
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: assigning labels...");
                                }
 
                                assign_article_to_labels($link, $entry_ref_id, $article_filters,
                                        $owner_uid);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for enclosures...");
                                }
 
                                }
 
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: article enclosures:");
                                        print_r($enclosures);
                                }
 
                                $filtered_tags = array_unique($filtered_tags);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: filtered article tags:");
                                        print_r($filtered_tags);
                                }
                                        db_query($link, "COMMIT");
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: article processed");
                                }
                        }
 
                        if (!$last_updated) {
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: new feed, catching it up...");
                                }
                                catchup_feed($link, $feed, false, $owner_uid);
                        }
 
-                       purge_feed($link, $feed, 0);
+                       if ($debug_enabled) {
+                               _debug("purging feed...");
+                       }
+
+                       purge_feed($link, $feed, 0, $debug_enabled);
 
                        db_query($link, "UPDATE ttrss_feeds
                                SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
                                $error_msg = mb_substr(magpie_error(), 0, 250);
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: error fetching feed: $error_msg");
                        }
 
                        unset($rss);
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: done");
                }
 
                }
        }
 
-       function http_authenticate_user($link) {
-
-//             error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
-
+/*     function http_authenticate_user($link) {
                if (!$_SERVER["PHP_AUTH_USER"]) {
 
                        header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
                }
 
                return true;
+       } */
+
+       function get_ssl_certificate_id() {
+               if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) {
+                       return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
+                               $_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
+                               $_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
+                               $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
+               }
+               return "";
+       }
+
+       function get_login_by_ssl_certificate($link) {
+
+               $cert_serial = db_escape_string(get_ssl_certificate_id());
+
+               if ($cert_serial) {
+                       $result = db_query($link, "SELECT login FROM ttrss_user_prefs, ttrss_users
+                               WHERE pref_name = 'SSL_CERT_SERIAL' AND value = '$cert_serial' AND
+                               owner_uid = ttrss_users.id");
+
+                       if (db_num_rows($result) != 0) {
+                               return db_escape_string(db_fetch_result($result, 0, "login"));
+                       }
+               }
+
+               return "";
+       }
+
+       function get_remote_user($link) {
+
+               if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH) {
+                       return db_escape_string($_SERVER["REMOTE_USER"]);
+               }
+
+               return db_escape_string(get_login_by_ssl_certificate($link));
+       }
+
+       function get_remote_fakepass($link) {
+               if (get_remote_user($link))
+                       return "******";
+               else
+                       return "";
        }
 
        function authenticate_user($link, $login, $password, $force_auth = false) {
                        $pwd_hash2 = encrypt_password($password, $login);
                        $login = db_escape_string($login);
 
-                       if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
-                                       && $_SERVER["REMOTE_USER"] && $login != "admin") {
+                       $remote_user = get_remote_user($link);
+
+                       if ($remote_user && $remote_user == $login && $login != "admin") {
 
-                               $login = db_escape_string($_SERVER["REMOTE_USER"]);
+                               $login = $remote_user;
 
                                $query = "SELECT id,login,access_level,pwd_hash
                    FROM ttrss_users WHERE
                                        login = '$login'";
 
+                               if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER
+                                               && $_SERVER["REMOTE_USER"]) {
+                                       $result = db_query($link, $query);
+
+                                       // First login ?
+                                       if (db_num_rows($result) == 0) {
+                                               $query = "INSERT INTO ttrss_users
+                                                               (login,access_level,last_login,created)
+                                                               VALUES ('$login', 0, null, NOW())";
+                                               db_query($link, $query);
+                                       }
+                               }
+
                        } else {
                                $query = "SELECT id,login,access_level,pwd_hash
                    FROM ttrss_users WHERE
                                db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
                                        $_SESSION["uid"]);
 
+
+                               // LemonLDAP can send user informations via HTTP HEADER
+                               if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER){
+                                       // update user name
+                                       if ($_SERVER['HTTP_USER_NAME']){
+                                               $fullname = db_escape_string($_SERVER['HTTP_USER_NAME']);
+                                               db_query($link, "UPDATE ttrss_users SET full_name = '$fullname' WHERE id = " .
+                                                       $_SESSION["uid"]);
+                                       }
+                                       // update user mail
+                                       if ($_SERVER['HTTP_USER_MAIL']){
+                                               $email = db_escape_string($_SERVER['HTTP_USER_MAIL']);
+                                               db_query($link, "UPDATE ttrss_users SET email = '$email' WHERE id = " .
+                                                       $_SESSION["uid"]);
+                                       }
+                               }
+
                                $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
                                $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
 
                        }
 
                        if (!$_SESSION["uid"] || !validate_session($link)) {
-                               if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
-                                       && $_SERVER["REMOTE_USER"] && defined('AUTO_LOGIN') && AUTO_LOGIN) {
-                                   authenticate_user($link,$_SERVER['REMOTE_USER'],null);
+
+                               if (get_remote_user($link) && AUTO_LOGIN) {
+                                   authenticate_user($link, get_remote_user($link), null);
                                    $_SESSION["ref_schema_version"] = get_schema_version($link, true);
                                } else {
                                    render_login_form($link, $mobile);
                $dt = new DateTime($timestamp, new DateTimeZone('UTC'));
                $user_timestamp = $dt->format('U') + $user_tz->getOffset($dt);
 
-               if (!$no_smart_dt && get_pref($link, 'HEADLINES_SMART_DATE', $owner_uid)) {
+               if (!$no_smart_dt) {
                        return smart_date_time($link, $user_timestamp,
                                $user_tz->getOffset($dt), $owner_uid);
                } else {
        }
 
        function sql_bool_to_bool($s) {
-               if ($s == "t" || $s == "1") {
+               if ($s == "t" || $s == "1" || $s == "true") {
                        return true;
                } else {
                        return false;
                        return "even";
        }
 
+       // Session caching removed due to causing wrong redirects to upgrade
+       // script when get_schema_version() is called on an obsolete session
+       // created on a previous schema version.
        function get_schema_version($link, $nocache = false) {
-               if (!$_SESSION["schema_version"] || $nocache) {
+//             if (!$_SESSION["schema_version"] || $nocache) {
                        $result = db_query($link, "SELECT schema_version FROM ttrss_version");
                        $version = db_fetch_result($result, 0, "schema_version");
                        $_SESSION["schema_version"] = $version;
                        return $version;
-               } else {
-                       return $_SESSION["schema_version"];
-               }
+//             } else {
+//                     return $_SESSION["schema_version"];
+//             }
        }
 
        function sanity_check($link) {
 
+               global $ERRORS;
+
                $error_code = 0;
                $schema_version = get_schema_version($link);
 
                        $error_code = 12;
                }
 
-               if ($error_code != 0) {
-                       print_error_xml($error_code);
-                       return false;
-               } else {
-                       return true;
-               }
+               return array("code" => $error_code, "message" => $ERRORS[$error_code]);
        }
 
        function file_is_locked($filename) {
                if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link));
                if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed));
                if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link));
-               if (strchr($omode, "c")) {
-                       if (get_pref($link, 'ENABLE_FEED_CATS')) {
-                               $data = array_merge($data, getCategoryCounters($link));
-                       }
-               }
+               if (strchr($omode, "c")) $data = array_merge($data, getCategoryCounters($link));
 
                return $data;
        }
                return $version[1];
        }
 
-       function print_error_xml($code, $add_msg = "") {
-               global $ERRORS;
-
-               $error_msg = $ERRORS[$code];
-
-               if ($add_msg) {
-                       $error_msg = "$error_msg; $add_msg";
-               }
-
-               print "<rpc-reply>";
-               print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
-               print "</rpc-reply>";
-       }
-
        /**
         * Subscribes the user to the given feed
         *
 
                $params["theme"] = get_user_theme($link);
                $params["theme_options"] = get_user_theme_options($link);
-               $params["daemon_enabled"] = ENABLE_UPDATE_DAEMON;
 
                $params["sign_progress"] = theme_image($link, "images/indicator_white.gif");
                $params["sign_progress_tiny"] = theme_image($link, "images/indicator_tiny.gif");
                $data['last_article_id'] = getLastArticleId($link);
                $data['cdm_expanded'] = get_pref($link, 'CDM_EXPANDED');
 
-               if (ENABLE_UPDATE_DAEMON) {
+               if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
 
                        $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock");
 
                return $data;
        }
 
-       function getSearchSql($link, $search, $match_on) {
+       function search_to_sql($link, $search, $match_on) {
 
                $search_query_part = "";
 
                                $not = "";
                        }
 
-                       if (strpos($k, "@") === 0) {
+                       $commandpair = split(":", mb_strtolower($k), 2);
+
+                       if ($commandpair[0] == "note" && $commandpair[1]) {
+
+                               if ($commandpair[1] == "true")
+                                       array_push($query_keywords, "($not (note IS NOT NULL AND note != ''))");
+                               else
+                                       array_push($query_keywords, "($not (note IS NULL OR note = ''))");
+
+                       } else if ($commandpair[0] == "star" && $commandpair[1]) {
+
+                               if ($commandpair[1] == "true")
+                                       array_push($query_keywords, "($not (marked = true))");
+                               else
+                                       array_push($query_keywords, "($not (marked = false))");
+
+                       } else if ($commandpair[0] == "pub" && $commandpair[1]) {
+
+                               if ($commandpair[1] == "true")
+                                       array_push($query_keywords, "($not (published = true))");
+                               else
+                                       array_push($query_keywords, "($not (published = false))");
+
+                       } else if (strpos($k, "@") === 0) {
 
                                $user_tz_string = get_pref($link, 'USER_TIMEZONE', $_SESSION['uid']);
                                $orig_ts = strtotime(substr($k, 1));
-
                                $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC'));
 
+                               //$k = date("Y-m-d", strtotime(substr($k, 1)));
+
                                array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')");
                        } else if ($match_on == "both") {
                                array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                return $search_query_part;
        }
 
-       function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
+       function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false) {
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                                                $search_query_part = "ref_id = -1 AND ";
 
                                } else {
-                                       $search_query_part = getSearchSql($link, $search, $match_on);
+                                       $search_query_part = search_to_sql($link, $search, $match_on);
                                        $search_query_part .= " AND ";
                                }
 
                                $search_query_part = "";
                        }
 
+                       if ($filter) {
+                               $filter_query_part = filter_to_sql($filter);
+                       } else {
+                               $filter_query_part = "";
+                       }
+
                        $view_query_part = "";
 
                        if ($view_mode == "adaptive" || $view_query_part == "noscores") {
                                        ttrss_user_entries.ref_id = ttrss_entries.id AND
                                        ttrss_user_entries.owner_uid = '$owner_uid' AND
                                        $search_query_part
+                                       $filter_query_part
                                        $view_query_part
                                        $query_strategy_part ORDER BY $order_by
                                        $limit_query_part $offset_query_part";
        function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
                $limit, $search, $search_mode, $match_on, $view_mode = false) {
 
-               $note_style =   "float : right; background-color : #fff7d5; border-width : 1px; ".
+               require_once "lib/MiniTemplator.class.php";
+
+               $note_style =   "background-color : #fff7d5;
+                       border-width : 1px; ".
                        "padding : 5px; border-style : dashed; border-color : #e7d796;".
                        "margin-bottom : 1em; color : #9a8c59;";
 
                $feed_site_url = $qfh_ret[2];
                $last_error = $qfh_ret[3];
 
-//             if (!$feed_site_url) $feed_site_url = "http://localhost/";
+               $feed_self_url = get_self_url_prefix() .
+                       "/backend.php?op=rss&id=-2&key=" .
+                       get_feed_access_key($link, -2, false);
 
-               print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
-                       <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
-                       <rss version=\"2.0\">
-                       <channel>
-                       <title>$feed_title</title>
-                       <link>$feed_site_url</link>
-                       <description>Feed generated by Tiny Tiny RSS</description>";
+               if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
 
-               while ($line = db_fetch_assoc($result)) {
-                       print "<item>";
-                       print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
-                       print "<link>" . htmlspecialchars($line["link"]) . "</link>";
+               $tpl = new MiniTemplator;
 
-                       $tags = get_article_tags($link, $line["id"], $owner_uid);
+               $tpl->readTemplateFromFile("templates/generated_feed.txt");
 
-                       foreach ($tags as $tag) {
-                               print "<category>" . htmlspecialchars($tag) . "</category>";
-                       }
+               $tpl->setVariable('FEED_TITLE', $feed_title);
+               $tpl->setVariable('VERSION', VERSION);
+               $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url));
 
-                       $rfc822_date = date('r', strtotime($line["updated"]));
+               if (PUBSUBHUBBUB_HUB && $feed == -2) {
+                       $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB));
+                       $tpl->addBlock('feed_hub');
+               }
+
+               $tpl->setVariable('SELF_URL', htmlspecialchars(get_self_url_prefix()));
 
-                       print "<pubDate>$rfc822_date</pubDate>";
+               while ($line = db_fetch_assoc($result)) {
+                       $tpl->setVariable('ARTICLE_ID', htmlspecialchars($line['link']));
+                       $tpl->setVariable('ARTICLE_LINK', htmlspecialchars($line['link']));
+                       $tpl->setVariable('ARTICLE_TITLE', htmlspecialchars($line['title']));
+                       $tpl->setVariable('ARTICLE_EXCERPT',
+                               truncate_string(strip_tags($line["content_preview"]), 100, '...'));
 
-                       if ($line["author"]) {
-                               print "<author>" . htmlspecialchars($line["author"]) . "</author>";
+                       $content = sanitize_rss($link, $line["content_preview"], false, $owner_uid);
+
+                       if ($line['note']) {
+                               $content = "<div style=\"$note_style\">Article note: " . $line['note'] . "</div>" .
+                                       $content;
                        }
 
-                       print "<title><![CDATA[" .
-                               htmlspecialchars($line["title"]) . "]]></title>";
+                       $tpl->setVariable('ARTICLE_CONTENT', $content);
 
-                       print "<description><![CDATA[";
+                       $tpl->setVariable('ARTICLE_UPDATED', date('c', strtotime($line["updated"])));
+                       $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']));
 
-                       if ($line["note"]) {
-                               print "<div style='$note_style'>";
-                               print $line["note"];
-                               print "</div>";
-                       }
+                       $tags = get_article_tags($link, $line["id"], $owner_uid);
 
-                       print sanitize_rss($link, $line["content_preview"], false, $owner_uid);
-                       print "]]></description>";
+                       foreach ($tags as $tag) {
+                               $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag));
+                               $tpl->addBlock('category');
+                       }
 
                        $enclosures = get_article_enclosures($link, $line["id"]);
 
                                $type = htmlspecialchars($e['content_type']);
                                $url = htmlspecialchars($e['content_url']);
                                $length = $e['duration'];
-                               print "<enclosure url=\"$url\" type=\"$type\" length=\"$length\"/>";
+
+                               $tpl->setVariable('ARTICLE_ENCLOSURE_URL', $url);
+                               $tpl->setVariable('ARTICLE_ENCLOSURE_TYPE', $type);
+                               $tpl->setVariable('ARTICLE_ENCLOSURE_LENGTH', $length);
+
+                               $tpl->addBlock('enclosure');
                        }
 
-                       print "</item>";
-               }
+                       $tpl->addBlock('entry');
+               }
 
-               print "</channel></rss>";
+               $tmp = "";
 
+               $tpl->addBlock('feed');
+               $tpl->generateOutputToString($tmp);
+
+               print $tmp;
        }
 
        function getCategoryTitle($link, $cat_id) {
 
                $res = trim($str); if (!$res) return '';
 
-               if (get_pref($link, "STRIP_UNSAFE_TAGS", $owner) || $force_strip_tags) {
-                       $res = $purifier->purify($res);
-               }
+//             if (get_pref($link, "STRIP_UNSAFE_TAGS", $owner) || $force_strip_tags) {
+               $res = $purifier->purify($res);
+//             }
 
                if (get_pref($link, "STRIP_IMAGES", $owner)) {
                        $res = preg_replace('/<img[^>]+>/is', '', $res);
                        published = NOT published,last_read = NOW()
                        WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
                }
+
+               if (PUBSUBHUBBUB_HUB) {
+                       $rss_link = get_self_url_prefix() .
+                               "/backend.php?op=rss&id=-2&key=" .
+                               get_feed_access_key($link, -2, false);
+
+                       $p = new Publisher(PUBSUBHUBBUB_HUB);
+
+                       $pubsub_result = $p->publish_update($rss_link);
+               }
        }
 
        function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
                $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
                        "</option>";
 
+               if ($is_cat) $cat_q = "&is_cat=$is_cat";
+
+               if ($search) {
+                       $search_q = "&q=$search&m=$match_on&smode=$search_mode";
+               } else {
+                       $search_q = "";
+               }
+
                $rss_link = htmlspecialchars(get_self_url_prefix() .
-                       "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat&view_mode=$view_mode$search_q");
+                       "/backend.php?op=rss&id=$feed_id$cat_q$search_q");
 
                $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
 
                        }
                }
 
-               if ($search) {
-                       $search_q = "&q=$search&m=$match_on&smode=$search_mode";
-               } else {
-                       $search_q = "";
-               }
-
-               // Adaptive doesn't really make any sense for generated feeds
-               // All Articles is the default, so no need to insert it either
-               if ($view_mode == "adaptive" || $view_mode == "all_articles")
-                       $view_mode = "";
-               else
-                       $view_mode = "&view-mode=$view_mode";
-
                $reply .= "
                        <a href=\"#\"
                                title=\"".__("View as RSS feed")."\"
                        } else {
 
                                $entry .= "<object type=\"application/x-shockwave-flash\"
-                                       data=\"extras/button/musicplayer.swf?song_url=$url\"
+                                       data=\"lib/button/musicplayer.swf?song_url=$url\"
                                        width=\"17\" height=\"17\" style='float : left; margin-right : 5px;'>
                                        <param name=\"movie\"
-                                               value=\"extras/button/musicplayer.swf?song_url=$url\" />
+                                               value=\"lib/button/musicplayer.swf?song_url=$url\" />
                                        </object>";
                        }
                }
                                $rv['content'] .= "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
-                       $tags_str = format_tags_string(get_article_tags($link, $id), $id);
+                       $tags = get_article_tags($link, $id);
+                       $tags_str = format_tags_string($tags, $id);
+                       $tags_str_full = join(", ", $tags);
+
+                       if (!$tags_str_full) $tags_str_full = __("no tags");
 
                        if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
 
                                        <a title=\"".__('Edit tags for this article')."\"
                                        href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
 
+                               $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
+                                       id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
+                                       position=\"below\">$tags_str_full</div>";
+
                                $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
                                                class='tagsPic' style=\"cursor : pointer\"
                                                onclick=\"postOpenInNewTab(event, $id)\"
 
                        $rv['content'] .= "</div>";
 
-                       $rv['content'] .= "<div class=\"postIcon\">" .
-                               "<a target=\"_blank\" title=\"".__("Visit the website")."\"$
-                               href=\"".htmlspecialchars($feed_site_url)."\">".
-                               $feed_icon . "</a></div>";
-
                        $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
                                if ($line['note']) {
                                        $rv['content'] .= format_article_note($id, $line['note']);
                                }
                        $rv['content'] .= "</div>";
 
+                       $rv['content'] .= "<div class=\"postIcon\">" .
+                               "<a target=\"_blank\" title=\"".__("Visit the website")."\"$
+                               href=\"".htmlspecialchars($feed_site_url)."\">".
+                               $feed_icon . "</a></div>";
+
                        $rv['content'] .= "<div class=\"postContent\">";
 
                        $article_content = sanitize_rss($link, $line["content"], false, false,
                                                class=\"hlTitle\"><span class='hlContent$hlc_suffix'>";
                                        $reply['content'] .= "<a id=\"RTITLE-$id\"
                                                href=\"" . htmlspecialchars($line["link"]) . "\"
-                                               onclick=\"return false;\">" .
+                                               onclick=\"\">" .
                                                $line["title"];
 
                                        if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
 
                                        $reply['content'] .= $labels_str;
 
-                                       /* if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
+                                       if (!get_pref($link, 'VFEED_GROUP_BY_FEED') &&
+                                               defined('_SHOW_FEED_TITLE_IN_VFEEDS')) {
                                                if (@$line["feed_title"]) {
-                                                       print "<span class=\"hlFeed\">
+                                                       $reply['content'] .= "<span class=\"hlFeed\">
                                                                (<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
                                                                $line["feed_title"]."</a>)
                                                        </span>";
                                                }
-                                       } */
+                                       }
 
                                        $reply['content'] .= "</div>";
 
-                                       $reply['content'] .= "<div class=\"hlRight\">";
                                        $reply['content'] .= "<span class=\"hlUpdated\">$updated_fmt</span>";
+                                       $reply['content'] .= "<div class=\"hlRight\">";
+
                                        $reply['content'] .= $score_pic;
 
                                        if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
 
                                                $reply['content'] .= "<span onclick=\"viewfeed($feed_id)\"
+                                                       style=\"cursor : pointer\"
                                                        title=\"".htmlspecialchars($line['feed_title'])."\">
                                                        $feed_icon_img<span>";
                                        }
 
                                        $reply['content'] .= $labels_str;
 
+                                       if (!get_pref($link, 'VFEED_GROUP_BY_FEED') &&
+                                               defined('_SHOW_FEED_TITLE_IN_VFEEDS')) {
+                                               if (@$line["feed_title"]) {
+                                                       $reply['content'] .= "<span class=\"hlFeed\">
+                                                               (<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
+                                                               $line["feed_title"]."</a>)
+                                                       </span>";
+                                               }
+                                       }
+
                                        if (!$expand_cdm)
                                                $content_hidden = "style=\"display : none\"";
                                        else
 
                $tag = mb_strtolower($tag, 'utf-8');
 
-               $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
+               $tag = preg_replace('/[\'\"\+\>\<]/', "", $tag);
 
 //             $tag = str_replace('"', "", $tag);
 //             $tag = str_replace("+", " ", $tag);
 
        function get_self_url_prefix() {
 
-               $url_path = "";
+               /* $url_path = "";
 
                if ($_SERVER['HTTPS'] != "on") {
                        $url_path = "http://";
 
                $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
 
-               return $url_path;
+               return $url_path; */
+
+               return SELF_URL_PATH;
 
        }
        function opml_publish_url($link){
         * @return string The Mozilla Firefox feed adding URL.
         */
        function add_feed_url() {
-               $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";
+               //$url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' :  'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+
+               $url_path = get_self_url_prefix() .
+                       "/backend.php?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
                return $url_path;
        } // function add_feed_url
 
 
        function update_feedbrowser_cache($link) {
 
-               $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers
+               $result = db_query($link, "SELECT feed_url, site_url, title, COUNT(id) AS subscribers
                        FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
                                WHERE tf.feed_url = ttrss_feeds.feed_url
-                               AND (private IS true OR feed_url LIKE '%:%@%/%'))
-                               GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000");
+                               AND (private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%'))
+                               GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000");
 
                db_query($link, "BEGIN");
 
                        $subscribers = db_escape_string($line["subscribers"]);
                        $feed_url = db_escape_string($line["feed_url"]);
                        $title = db_escape_string($line["title"]);
+                       $site_url = db_escape_string($line["site_url"]);
 
                        $tmp_result = db_query($link, "SELECT subscribers FROM
                                ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
                        if (db_num_rows($tmp_result) == 0) {
 
                                db_query($link, "INSERT INTO ttrss_feedbrowser_cache
-                                       (feed_url, title, subscribers) VALUES ('$feed_url',
-                                               '$title', '$subscribers')");
+                                       (feed_url, site_url, title, subscribers) VALUES ('$feed_url',
+                                               '$site_url', '$title', '$subscribers')");
 
                                ++$count;
 
 
        function format_article_note($id, $note) {
 
-               $str = "<div class='articleNote' title=\"".__('edit note')."\"
-                       onclick=\"editArticleNote($id)\">$note</div>";
+               $str = "<div class='articleNote'        onclick=\"editArticleNote($id)\">
+                       <div class='noteEdit' onclick=\"editArticleNote($id)\">".
+                       __('(edit note)')."</div>$note</div>";
 
                return $str;
        }
        }
 
        function api_get_headlines($link, $feed_id, $limit, $offset,
-                                       $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order) {
+                               $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
+                               $include_attachments) {
 
                        /* do not rely on params below */
 
                                                "tags" => get_article_tags($link, $line["id"]),
                                        );
 
+                                       if ($include_attachments)
+                                               $headline_row['attachments'] = get_article_enclosures($link,
+                                                       $line['id']);
+
                                if ($show_excerpt) {
                                        $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
                                        $headline_row["excerpt"] = $excerpt;
 
                                $content = $tmhOAuth->response['response'];
 
+                               define('MAGPIE_CACHE_ON', false);
+
                                $rss = new MagpieRSS($content, MAGPIE_OUTPUT_ENCODING,
                                        MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
 
                return $result;
        }
 
+       function filter_to_sql($filter) {
+               $query = "";
+
+               if (DB_TYPE == "pgsql")
+                       $reg_qpart = "~";
+               else
+                       $reg_qpart = "REGEXP";
+
+               switch ($filter["type"]) {
+                       case "title":
+                               $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "')";
+                               break;
+                       case "content":
+                               $query = "LOWER(ttrss_entries.content) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "')";
+                               break;
+                       case "both":
+                               $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "') OR LOWER(" .
+                                       "ttrss_entries.content) $reg_qpart LOWER('" . $filter['reg_exp'] . "')";
+                               break;
+                       case "tag":
+                               $query = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "')";
+                               break;
+                       case "link":
+                               $query = "LOWER(ttrss_entries.link) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "')";
+                               break;
+                       case "date":
+
+                               if ($filter["filter_param"] == "before")
+                                       $cmp_qpart = "<";
+                               else
+                                       $cmp_qpart = ">=";
+
+                               $timestamp = date("Y-m-d H:N:s", strtotime($filter["reg_exp"]));
+                               $query = "ttrss_entries.date_entered $cmp_qpart '$timestamp'";
+                               break;
+                       case "author":
+                               $query = "LOWER(ttrss_entries.author) $reg_qpart LOWER('".
+                                       $filter['reg_exp'] . "')";
+                               break;
+               }
+
+               if ($filter["inverse"])
+                       $query = "NOT ($query)";
+
+               if ($query) {
+                       if (DB_TYPE == "pgsql") {
+                               $query = " ($query) AND ttrss_entries.date_entered > NOW() - INTERVAL '14 days'";
+                       } else {
+                               $query = " ($query) AND ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL 14 DAY)";
+                       }
+                       $query .= " AND ";
+               }
+
+
+               return $query;
+       }
+
+       // Status codes:
+       // -1  - never connected
+       // 0   - no data received
+       // 1   - data received successfully
+       // 2   - did not receive valid data
+       // >10 - server error, code + 10 (e.g. 16 means server error 6)
+
+       function get_linked_feeds($link, $instance_id = false) {
+               if ($instance_id)
+                       $instance_qpart = "id = '$instance_id' AND ";
+               else
+                       $instance_qpart = "";
+
+               if (DB_TYPE == "pgsql") {
+                       $date_qpart = "last_connected < NOW() - INTERVAL '6 hours'";
+               } else {
+                       $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR)";
+               }
+
+               $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances
+                       WHERE $instance_qpart $date_qpart ORDER BY last_connected");
+
+               while ($line = db_fetch_assoc($result)) {
+                       $id = $line['id'];
+
+                       _debug("Updating: " . $line['access_url'] . " ($id)");
+
+                       $fetch_url = $line['access_url'] . '/backend.php?op=fbexport';
+                       $post_query = 'key=' . $line['access_key'];
+
+                       $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query);
+
+                       if ($feeds) {
+                               $feeds = json_decode($feeds, true);
+
+                               if ($feeds) {
+                                       if ($feeds['error']) {
+                                               $status = $feeds['error']['code'] + 10;
+                                       } else {
+                                               $status = 1;
+
+                                               if (count($feeds['feeds']) > 0) {
+
+                                                       db_query($link, "DELETE FROM ttrss_linked_feeds
+                                                               WHERE instance_id = '$id'");
+
+                                                       foreach ($feeds['feeds'] as $feed) {
+                                                               $feed_url = db_escape_string($feed['feed_url']);
+                                                               $title = db_escape_string($feed['title']);
+                                                               $subscribers = db_escape_string($feed['subscribers']);
+                                                               $site_url = db_escape_string($feed['site_url']);
+
+                                                               db_query($link, "INSERT INTO ttrss_linked_feeds
+                                                                       (feed_url, site_url, title, subscribers, instance_id, created, updated)
+                                                               VALUES
+                                                                       ('$feed_url', '$site_url', '$title', '$subscribers', '$id', NOW(), NOW())");
+                                                       }
+                                               } else {
+                                                       // received 0 feeds, this might indicate that
+                                                       // the instance on the other hand is rebuilding feedbrowser cache
+                                                       // we will try again later
+
+                                                       // TODO: maybe perform expiration based on updated here?
+                                               }
+
+                                               _debug("Processed " . count($feeds['feeds']) . " feeds.");
+                                       }
+                               } else {
+                                       $status = 2;
+                               }
+
+                       } else {
+                               $status = 0;
+                       }
+
+                       _debug("Status: $status");
+
+                       db_query($link, "UPDATE ttrss_linked_instances SET
+                               last_status_out = '$status', last_connected = NOW() WHERE id = '$id'");
+
+               }
+
+       }
 ?>