]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
add special indication for categories having unread items in child categories
[tt-rss.git] / include / functions.php
index b2ff209a054fdf5ae9f1afa3f71f68ec19021379..b2319e6e1efabcad01d2897be89443af7306de32 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+       define('EXPECTED_CONFIG_VERSION', 25);
+       define('SCHEMA_VERSION', 93);
+
+       mb_internal_encoding("UTF-8");
        date_default_timezone_set('UTC');
        if (defined('E_DEPRECATED')) {
                error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
 
        startup_gettext();
 
-       if (defined('MEMCACHE_SERVER')) {
-               $memcache = new Memcache;
-               $memcache->connect(MEMCACHE_SERVER, 11211);
-       }
-
        require_once 'db-prefs.php';
        require_once 'version.php';
 
         * @return void
         */
        function _debug($msg) {
+               if (defined('QUIET') && QUIET) {
+                       return;
+               }
                $ts = strftime("%H:%M:%S", time());
                if (function_exists('posix_getpid')) {
                        $ts = "$ts/" . posix_getpid();
                        $favicon_url = get_favicon_url($site_url);
 
                        if ($favicon_url) {
-                               $contents = fetch_file_contents($favicon_url, "image");
+                               // Limiting to "image" type misses those served with text/plain
+                               $contents = fetch_file_contents($favicon_url); // , "image");
+
+                               if ($contents) {
+                                       // Crude image type matching.
+                                       // Patterns gleaned from the file(1) source code.
+                                       if (preg_match('/^\x00\x00\x01\x00/', $contents)) {
+                                               // 0       string  \000\000\001\000        MS Windows icon resource
+                                               //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource");
+                                       }
+                                       elseif (preg_match('/^GIF8/', $contents)) {
+                                               // 0       string          GIF8            GIF image data
+                                               //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image");
+                                       }
+                                       elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) {
+                                               // 0       string          \x89PNG\x0d\x0a\x1a\x0a         PNG image data
+                                               //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image");
+                                       }
+                                       elseif (preg_match('/^\xff\xd8/', $contents)) {
+                                               // 0       beshort         0xffd8          JPEG image data
+                                               //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image");
+                                       }
+                                       else {
+                                               //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type");
+                                               $contents = "";
+                                       }
+                               }
 
                                if ($contents) {
                                        $fp = @fopen($icon_file, "w");
 
                                        // First login ?
                                        if (db_num_rows($result) == 0) {
+                                               $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
+                                               $pwd_hash = encrypt_password($password, $salt, true);
+
                                                $query2 = "INSERT INTO ttrss_users
-                                                               (login,access_level,last_login,created)
-                                                               VALUES ('$login', 0, null, NOW())";
+                                                               (login,access_level,last_login,created,pwd_hash,salt)
+                                                               VALUES ('$login', 0, null, NOW(), '$pwd_hash','$salt')";
                                                db_query($link, $query2);
                                        }
                                }
 
+                       } else if (get_schema_version($link) > 87) {
+                               $result = db_query($link, "SELECT salt FROM ttrss_users WHERE
+                                       login = '$login'");
+
+                               if (db_num_rows($result) != 1) {
+                                       return false;
+                               }
+
+                               $salt = db_fetch_result($result, 0, "salt");
+
+                               if ($salt == "") {
+
+                                       $query = "SELECT id,login,access_level,pwd_hash
+                           FROM ttrss_users WHERE
+                                               login = '$login' AND (pwd_hash = '$pwd_hash1' OR
+                                               pwd_hash = '$pwd_hash2')";
+
+                                       // verify and upgrade password to new salt base
+
+                                       $result = db_query($link, $query);
+
+                                       if (db_num_rows($result) == 1) {
+                                               // upgrade password to MODE2
+
+                                               $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
+                                               $pwd_hash = encrypt_password($password, $salt, true);
+
+                                               db_query($link, "UPDATE ttrss_users SET
+                                                       pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'");
+
+                                               $query = "SELECT id,login,access_level,pwd_hash
+                                   FROM ttrss_users WHERE
+                                                       login = '$login' AND pwd_hash = '$pwd_hash'";
+
+                                       } else {
+                                               return false;
+                                       }
+
+                               } else {
+
+                                       $pwd_hash = encrypt_password($password, $salt, true);
+
+                                       $query = "SELECT id,login,access_level,pwd_hash
+                                FROM ttrss_users WHERE
+                                               login = '$login' AND pwd_hash = '$pwd_hash'";
+
+                               }
                        } else {
                                $query = "SELECT id,login,access_level,pwd_hash
-                   FROM ttrss_users WHERE
+                        FROM ttrss_users WHERE
                                        login = '$login' AND (pwd_hash = '$pwd_hash1' OR
                                                pwd_hash = '$pwd_hash2')";
                        }
 
                        $_SESSION["uid"] = 1;
                        $_SESSION["name"] = "admin";
+                       $_SESSION["access_level"] = 10;
+
+                       if (!$_SESSION["csrf_token"]) {
+                               $_SESSION["csrf_token"] = sha1(uniqid(rand(), true));
+                       }
 
                        $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
 
                }
        }
 
-       function catchup_feed($link, $feed, $cat_view, $owner_uid = false) {
+       function catchup_feed($link, $feed, $cat_view, $owner_uid = false, $max_id = false) {
 
                        if (!$owner_uid) $owner_uid = $_SESSION['uid'];
 
                        //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
 
+                       $ref_check_qpart = ($max_id &&
+                               !get_pref($link, 'REVERSE_HEADLINES')) ? "ref_id <= '$max_id'" : "true";
+
                        if (is_numeric($feed)) {
                                if ($cat_view) {
 
 
                                                        db_query($link, "UPDATE ttrss_user_entries
                                                                SET unread = false,last_read = NOW()
-                                                               WHERE feed_id = '$tmp_feed' AND owner_uid = $owner_uid");
+                                                               WHERE feed_id = '$tmp_feed'
+                                                               AND $ref_check_qpart
+                                                               AND owner_uid = $owner_uid");
                                                }
                                        } else if ($feed == -2) {
 
                                                db_query($link, "UPDATE ttrss_user_entries
                                                        SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
                                                                FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0
-                                                       AND unread = true AND owner_uid = $owner_uid");
+                                                               AND $ref_check_qpart
+                                                               AND unread = true AND owner_uid = $owner_uid");
                                        }
 
                                } else if ($feed > 0) {
 
                                        db_query($link, "UPDATE ttrss_user_entries
                                                        SET unread = false,last_read = NOW()
-                                                       WHERE feed_id = '$feed' AND owner_uid = $owner_uid");
+                                                       WHERE feed_id = '$feed'
+                                                       AND $ref_check_qpart
+                                                       AND owner_uid = $owner_uid");
 
                                } else if ($feed < 0 && $feed > -10) { // special, like starred
 
                                        if ($feed == -1) {
                                                db_query($link, "UPDATE ttrss_user_entries
                                                        SET unread = false,last_read = NOW()
-                                                       WHERE marked = true AND owner_uid = $owner_uid");
+                                                       WHERE marked = true
+                                                       AND $ref_check_qpart
+                                                       AND owner_uid = $owner_uid");
                                        }
 
                                        if ($feed == -2) {
                                                db_query($link, "UPDATE ttrss_user_entries
                                                        SET unread = false,last_read = NOW()
-                                                       WHERE published = true AND owner_uid = $owner_uid");
+                                                       WHERE published = true
+                                                       AND $ref_check_qpart
+                                                       AND owner_uid = $owner_uid");
                                        }
 
                                        if ($feed == -3) {
                                        if ($feed == -4) {
                                                db_query($link, "UPDATE ttrss_user_entries
                                                        SET unread = false,last_read = NOW()
-                                                       WHERE owner_uid = $owner_uid");
+                                                       WHERE $ref_check_qpart AND owner_uid = $owner_uid");
                                        }
 
                                } else if ($feed < -10) { // label
                                        db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2
                                                SET unread = false, last_read = NOW()
                                                        WHERE label_id = '$label_id' AND unread = true
+                                                       AND $ref_check_qpart
                                                        AND owner_uid = '$owner_uid' AND ref_id = article_id");
 
                                }
                                while ($line = db_fetch_assoc($result)) {
                                        db_query($link, "UPDATE ttrss_user_entries SET
                                                unread = false, last_read = NOW()
-                                               WHERE int_id = " . $line["post_int_id"]);
+                                               WHERE $ref_check_qpart AND int_id = " . $line["post_int_id"]);
                                }
                                db_query($link, "COMMIT");
                        }
 
                array_push($ret_arr, $cv);
 
-               $age_qpart = getMaxAgeSubquery();
-
-               $result = db_query($link, "SELECT id AS cat_id, value AS unread
+               $result = db_query($link, "SELECT id AS cat_id, value AS unread,
+                       (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
+                               WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
                        FROM ttrss_feed_categories, ttrss_cat_counters_cache
                        WHERE ttrss_cat_counters_cache.feed_id = id AND
                        ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
                while ($line = db_fetch_assoc($result)) {
                        $line["cat_id"] = (int) $line["cat_id"];
 
+                       if ($line["num_children"] > 0) {
+                               $child_counter = getCategoryUnreadRecursive($link, $line["cat_id"], $_SESSION["uid"]);
+                       } else {
+                               $child_counter = 0;
+                       }
+
                        $cv = array("id" => $line["cat_id"], "kind" => "cat",
+                               "child_counter" => $child_counter,
                                "counter" => $line["unread"]);
 
                        array_push($ret_arr, $cv);
                return $ret_arr;
        }
 
+       // only accepts real cats (>= 0)
+       function getCategoryUnreadRecursive($link, $cat, $owner_uid = false) {
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
+               $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat'
+                               AND owner_uid = $owner_uid");
+
+               $unread = 0;
+
+               while ($line = db_fetch_assoc($result)) {
+                       $unread += getCategoryUnread($link, $line["id"], $owner_uid);
+                       $unread += getCategoryUnreadRecursive($link, $line["id"], $owner_uid);
+               }
+
+               return $unread;
+       }
+
        function getCategoryUnread($link, $cat, $owner_uid = false) {
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
                                $cat_query = "cat_id IS NULL";
                        }
 
-                       $age_qpart = getMaxAgeSubquery();
-
                        $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
                                        AND owner_uid = " . $owner_uid);
 
                        $match_part = implode(" OR ", $cat_feeds);
 
                        $result = db_query($link, "SELECT COUNT(int_id) AS unread
-                               FROM ttrss_user_entries,ttrss_entries
-                               WHERE   unread = true AND ($match_part) AND id = ref_id
-                               AND $age_qpart AND owner_uid = " . $owner_uid);
+                               FROM ttrss_user_entries
+                               WHERE   unread = true AND ($match_part)
+                               AND owner_uid = " . $owner_uid);
 
                        $unread = 0;
 
 
                        $result = db_query($link, "
                                SELECT COUNT(unread) AS unread FROM
-                                       ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
-                               WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
-                                       ttrss_labels2.owner_uid = '$owner_uid'
-                                       AND unread = true AND feed_id = ttrss_feeds.id
+                                       ttrss_user_entries, ttrss_user_labels2
+                               WHERE article_id = ref_id AND unread = true
                                        AND ttrss_user_entries.owner_uid = '$owner_uid'");
 
                        $unread = db_fetch_result($result, 0, "unread");
                }
        }
 
-       function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
-               if (DB_TYPE == "pgsql") {
-                       return "ttrss_entries.date_updated >
-                               NOW() - INTERVAL '$days days'";
-               } else {
-                       return "ttrss_entries.date_updated >
-                               DATE_SUB(NOW(), INTERVAL $days DAY)";
-               }
-       }
-
        function getFeedUnread($link, $feed, $is_cat = false) {
                return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
        }
        function getLabelUnread($link, $label_id, $owner_uid = false) {
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
-               $result = db_query($link, "
-                       SELECT COUNT(unread) AS unread FROM
-                               ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
-                       WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
-                               ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
-                               AND unread = true AND feed_id = ttrss_feeds.id
-                               AND ttrss_user_entries.owner_uid = '$owner_uid'");
+               $result = db_query($link, "SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
+                       WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id");
 
                if (db_num_rows($result) != 0) {
                        return db_fetch_result($result, 0, "unread");
                $owner_uid = false) {
 
                $n_feed = (int) $feed;
+               $need_entries = false;
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                        $unread_qpart = "true";
                }
 
-               $age_qpart = getMaxAgeSubquery();
-
                if ($is_cat) {
                        return getCategoryUnread($link, $n_feed, $owner_uid);
                } if ($feed != "0" && $n_feed == 0) {
 
                        $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
                                FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
-                                       AND ref_id = id AND $age_qpart
-                                       AND $unread_qpart)) AS count FROM ttrss_tags
+                                       AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
                                WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
                        return db_fetch_result($result, 0, "count");
 
                        } else {
                                $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
                        }
+
+                       $need_entries = true;
+
                } else if ($n_feed == -4) {
                        $match_part = "true";
                } else if ($n_feed >= 0) {
 
                if ($match_part) {
 
-                       if ($n_feed != 0) {
-                               $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
-                               $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
-                       } else {
+                       if ($need_entries) {
                                $from_qpart = "ttrss_user_entries,ttrss_entries";
-                               $feeds_qpart = '';
+                               $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
+                       } else {
+                               $from_qpart = "ttrss_user_entries";
                        }
 
                        $query = "SELECT count(int_id) AS unread
                                FROM $from_qpart WHERE
-                               ttrss_user_entries.ref_id = ttrss_entries.id AND
-                               $age_qpart AND
-                               $feeds_qpart
-                               $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
+                               $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
+
+                       //echo "[$feed/$query]\n";
 
                        $result = db_query($link, $query);
 
                        $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
                                FROM ttrss_tags,ttrss_user_entries,ttrss_entries
                                WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
-                               AND $unread_qpart AND $age_qpart AND
-                                       ttrss_tags.owner_uid = " . $owner_uid);
+                               AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid);
                }
 
                $unread = db_fetch_result($result, 0, "unread");
 
                $ret_arr = array();
 
-               $age_qpart = getMaxAgeSubquery();
-
                $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
                        FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
-                               AND ref_id = id AND $age_qpart
-                               AND unread = true)) AS count FROM ttrss_tags
+                               AND ref_id = id AND unread = true)) AS count FROM ttrss_tags
                                WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
                                ORDER BY count DESC LIMIT 55");
 
 
                $ret_arr = array();
 
-               $age_qpart = getMaxAgeSubquery();
-
                $owner_uid = $_SESSION["uid"];
 
                $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
 
                $ret_arr = array();
 
-               $age_qpart = getMaxAgeSubquery();
-
                $query = "SELECT ttrss_feeds.id,
                                ttrss_feeds.title,
                                ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
        }
 
        /**
-        * Subscribes the user to the given feed
-        *
-        * @param resource $link       Database connection
-        * @param string   $url        Feed URL to subscribe to
-        * @param integer  $cat_id     Category ID the feed shall be added to
-        * @param string   $auth_login (optional) Feed username
-        * @param string   $auth_pass  (optional) Feed password
-        *
         * @return integer Status code:
         *                 0 - OK, Feed already exists
         *                 1 - OK, Feed added
         *                 5 - Couldn't download the URL content.
         */
        function subscribe_to_feed($link, $url, $cat_id = 0,
-                       $auth_login = '', $auth_pass = '') {
+                       $auth_login = '', $auth_pass = '', $need_auth = false) {
 
                require_once "include/rssfuncs.php";
 
 
                $has_oauth = db_fetch_result($result, 0, 'twitter_oauth');
 
-               if (!$has_oauth || strpos($url, '://api.twitter.com') === false) {
+               if (!$need_auth || !$has_oauth || strpos($url, '://api.twitter.com') === false) {
                        if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5;
 
                        if (url_is_html($url, $auth_login, $auth_pass)) {
                        } else if ($feed == 0 && !$cat_view) { // archive virtual feed
                                $query_strategy_part = "feed_id IS NULL";
                        } else if ($feed == 0 && $cat_view) { // uncategorized
-                               $query_strategy_part = "cat_id IS NULL";
+                               $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
                                $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
                        } else if ($feed == -1) { // starred virtual feed
                                $query_strategy_part = "marked = true";
 
                        $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,object[classid|type|id|name|width|height|codebase],param[name|value],table,tr,td";
+                       $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title|align|hspace],ul,ol,li,h1,h2,h3,h4,s,object[classid|type|id|name|width|height|codebase],param[name|value],table,tr,td,span[class]";
 
                        $config->set('HTML.SafeObject', true);
                        @$config->set('HTML', 'Allowed', $allowed);
                                @$config->set('Cache', 'SerializerPath', "../" . CACHE_DIR . "/htmlpurifier");
                        }
 
+                       $config->set('Filter.YouTube', true);
+
                        $purifier = new HTMLPurifier($config);
                }
 
 
                $node = $doc->getElementsByTagName('body')->item(0);
 
-               return $doc->saveXML($node);
+               return $doc->saveXML($node, LIBXML_NOEMPTYTAG);
        }
 
        /**
         * @param integer $limit The maximum number of articles by digest.
         * @return boolean Return false if digests are not enabled.
         */
-       function send_headlines_digests($link, $limit = 100, $debug = true) {
+       function send_headlines_digests($link, $debug = false) {
 
                require_once 'lib/phpmailer/class.phpmailer.php';
 
                $user_limit = 15; // amount of users to process (e.g. emails to send out)
-               $days = 1;
+               $limit = 1000; // maximum amount of headlines to include
 
-               if ($debug) _debug("Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit");
+               if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
 
                if (DB_TYPE == "pgsql") {
-                       $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
+                       $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
                } else if (DB_TYPE == "mysql") {
-                       $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
+                       $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
                }
 
                $result = db_query($link, "SELECT id,email FROM ttrss_users
                while ($line = db_fetch_assoc($result)) {
 
                        if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
-                               print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
+                               $preferred_ts = strtotime(get_pref($link, 'DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
 
-                               $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
+                               // try to send digests within 2 hours of preferred time
+                               if ($preferred_ts && time() >= $preferred_ts &&
+                                               time() - $preferred_ts <= 7200) {
 
-                               $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
-                               $digest = $tuple[0];
-                               $headlines_count = $tuple[1];
-                               $affected_ids = $tuple[2];
-                               $digest_text = $tuple[3];
+                                       if ($debug) print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
 
-                               if ($headlines_count > 0) {
+                                       $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
 
-                                       $mail = new PHPMailer();
+                                       global $tz_offset;
 
-                                       $mail->PluginDir = "lib/phpmailer/";
-                                       $mail->SetLanguage("en", "lib/phpmailer/language/");
+                                       // reset tz_offset global to prevent tz cache clash between users
+                                       $tz_offset = -1;
 
-                                       $mail->CharSet = "UTF-8";
+                                       $tuple = prepare_headlines_digest($link, $line["id"], 1, $limit);
+                                       $digest = $tuple[0];
+                                       $headlines_count = $tuple[1];
+                                       $affected_ids = $tuple[2];
+                                       $digest_text = $tuple[3];
 
-                                       $mail->From = SMTP_FROM_ADDRESS;
-                                       $mail->FromName = SMTP_FROM_NAME;
-                                       $mail->AddAddress($line["email"], $line["login"]);
+                                       if ($headlines_count > 0) {
 
-                                       if (SMTP_HOST) {
-                                               $mail->Host = SMTP_HOST;
-                                               $mail->Mailer = "smtp";
-                                               $mail->SMTPAuth = SMTP_LOGIN != '';
-                                               $mail->Username = SMTP_LOGIN;
-                                               $mail->Password = SMTP_PASSWORD;
-                                       }
+                                               $mail = new PHPMailer();
 
-                                       $mail->IsHTML(true);
-                                       $mail->Subject = DIGEST_SUBJECT;
-                                       $mail->Body = $digest;
-                                       $mail->AltBody = $digest_text;
+                                               $mail->PluginDir = "lib/phpmailer/";
+                                               $mail->SetLanguage("en", "lib/phpmailer/language/");
 
-                                       $rc = $mail->Send();
+                                               $mail->CharSet = "UTF-8";
 
-                                       if (!$rc) print "ERROR: " . $mail->ErrorInfo;
+                                               $mail->From = SMTP_FROM_ADDRESS;
+                                               $mail->FromName = SMTP_FROM_NAME;
+                                               $mail->AddAddress($line["email"], $line["login"]);
 
-                                       print "RC=$rc\n";
+                                               if (SMTP_HOST) {
+                                                       $mail->Host = SMTP_HOST;
+                                                       $mail->Mailer = "smtp";
+                                                       $mail->SMTPAuth = SMTP_LOGIN != '';
+                                                       $mail->Username = SMTP_LOGIN;
+                                                       $mail->Password = SMTP_PASSWORD;
+                                               }
 
-                                       if ($rc && $do_catchup) {
-                                               print "Marking affected articles as read...\n";
-                                               catchupArticlesById($link, $affected_ids, 0, $line["id"]);
+                                               $mail->IsHTML(true);
+                                               $mail->Subject = DIGEST_SUBJECT;
+                                               $mail->Body = $digest;
+                                               $mail->AltBody = $digest_text;
+
+                                               $rc = $mail->Send();
+
+                                               if (!$rc && $debug) print "ERROR: " . $mail->ErrorInfo;
+
+                                               if ($debug) print "RC=$rc\n";
+
+                                               if ($rc && $do_catchup) {
+                                                       if ($debug) print "Marking affected articles as read...\n";
+                                                       catchupArticlesById($link, $affected_ids, 0, $line["id"]);
+                                               }
+                                       } else {
+                                               if ($debug) print "No headlines\n";
                                        }
-                               } else {
-                                       print "No headlines\n";
-                               }
 
-                               db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
-                                       WHERE id = " . $line["id"]);
+                                       db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
+                                               WHERE id = " . $line["id"]);
+
+                               }
                        }
                }
 
 
        }
 
-       function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
+       function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 1000) {
 
                require_once "lib/MiniTemplator.class.php";
 
                $tpl->readTemplateFromFile("templates/digest_template_html.txt");
                $tpl_t->readTemplateFromFile("templates/digest_template.txt");
 
-               $tpl->setVariable('CUR_DATE', date('Y/m/d'));
-               $tpl->setVariable('CUR_TIME', date('G:i'));
+               $user_tz_string = get_pref($link, 'USER_TIMEZONE', $user_id);
+               $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
+
+               $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
+               $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
 
-               $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
-               $tpl_t->setVariable('CUR_TIME', date('G:i'));
+               $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
+               $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
 
                $affected_ids = array();
 
 
                $result = db_query($link, "SELECT ttrss_entries.title,
                                ttrss_feeds.title AS feed_title,
+                               COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title,
                                date_updated,
                                ttrss_user_entries.ref_id,
                                link,
-                               SUBSTRING(content, 1, 120) AS excerpt,
+                               score,
+                               content,
                                ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
                        FROM
                                ttrss_user_entries,ttrss_entries,ttrss_feeds
+                       LEFT JOIN
+                               ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
                        WHERE
                                ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
                                AND include_in_digest = true
                                AND $interval_query
                                AND ttrss_user_entries.owner_uid = $user_id
                                AND unread = true
-                       ORDER BY ttrss_feeds.title, date_updated DESC
+                               AND score >= 0
+                       ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
                        LIMIT $limit");
 
                $cur_feed_title = "";
                        $updated = make_local_datetime($link, $line['last_updated'], false,
                                $user_id);
 
+/*                     if ($line["score"] != 0) {
+                               if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
+
+                               $line["title"] .= " (".$line['score'].")";
+                       } */
+
+                       if (get_pref($link, 'ENABLE_FEED_CATS', $user_id)) {
+                               $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
+                       }
+
                        $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
                        $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
                        $tpl->setVariable('ARTICLE_LINK', $line["link"]);
                        $tpl->setVariable('ARTICLE_UPDATED', $updated);
                        $tpl->setVariable('ARTICLE_EXCERPT',
-                               truncate_string(strip_tags($line["excerpt"]), 100));
+                               truncate_string(strip_tags($line["content"]), 300));
+//                     $tpl->setVariable('ARTICLE_CONTENT',
+//                             strip_tags($article_content));
 
                        $tpl->addBlock('article');
 
 
        function get_article_tags($link, $id, $owner_uid = 0, $tag_cache = false) {
 
-               global $memcache;
-
                $a_id = db_escape_string($id);
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
                $obj_id = md5("TAGS:$owner_uid:$id");
                $tags = array();
 
-               if ($memcache && $obj = $memcache->get($obj_id)) {
-                       $tags = $obj;
-               } else {
-                       /* check cache first */
+               /* check cache first */
 
-                       if ($tag_cache === false) {
-                               $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries
-                                       WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
-
-                               $tag_cache = db_fetch_result($result, 0, "tag_cache");
-                       }
+               if ($tag_cache === false) {
+                       $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries
+                               WHERE ref_id = '$id' AND owner_uid = $owner_uid");
 
-                       if ($tag_cache) {
-                               $tags = explode(",", $tag_cache);
-                       } else {
+                       $tag_cache = db_fetch_result($result, 0, "tag_cache");
+               }
 
-                               /* do it the hard way */
+               if ($tag_cache) {
+                       $tags = explode(",", $tag_cache);
+               } else {
 
-                               $tmp_result = db_query($link, $query);
+                       /* do it the hard way */
 
-                               while ($tmp_line = db_fetch_assoc($tmp_result)) {
-                                       array_push($tags, $tmp_line["tag_name"]);
-                               }
+                       $tmp_result = db_query($link, $query);
 
-                               /* update the cache */
+                       while ($tmp_line = db_fetch_assoc($tmp_result)) {
+                               array_push($tags, $tmp_line["tag_name"]);
+                       }
 
-                               $tags_str = db_escape_string(join(",", $tags));
+                       /* update the cache */
 
-                               db_query($link, "UPDATE ttrss_user_entries
-                                       SET tag_cache = '$tags_str' WHERE ref_id = '$id'
-                                       AND owner_uid = " . $_SESSION["uid"]);
-                       }
+                       $tags_str = db_escape_string(join(",", $tags));
 
-                       if ($memcache) $memcache->add($obj_id, $tags, 0, 3600);
+                       db_query($link, "UPDATE ttrss_user_entries
+                               SET tag_cache = '$tags_str' WHERE ref_id = '$id'
+                               AND owner_uid = $owner_uid");
                }
 
                return $tags;
                return $entry;
        }
 
-       function format_article($link, $id, $mark_as_read = true, $zoom_mode = false) {
+       function format_article($link, $id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
+
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                $rv = array();
 
                //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
 
                $result = db_query($link, "SELECT rtl_content, always_display_enclosures FROM ttrss_feeds
-                       WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
+                       WHERE id = '$feed_id' AND owner_uid = $owner_uid");
 
                if (db_num_rows($result) == 1) {
                        $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
                if ($mark_as_read) {
                        $result = db_query($link, "UPDATE ttrss_user_entries
                                SET unread = false,last_read = NOW()
-                               WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
+                               WHERE ref_id = '$id' AND owner_uid = $owner_uid");
 
-                       ccache_update($link, $feed_id, $_SESSION["uid"]);
+                       ccache_update($link, $feed_id, $owner_uid);
                }
 
                $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
                        orig_feed_id,
                        note
                        FROM ttrss_entries,ttrss_user_entries
-                       WHERE   id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
+                       WHERE   id = '$id' AND ref_id = id AND owner_uid = $owner_uid");
 
                if ($result) {
 
                                        </head><body>";
                        }
 
+                       $title_escaped = db_escape_string($line['title']);
+
                        $rv['content'] .= "<div id=\"PTITLE-$id\" style=\"display : none\">" .
                                truncate_string(strip_tags($line['title']), 15) . "</div>";
 
+                       $rv['content'] .= "<div id=\"PTITLE-FULL-$id\" style=\"display : none\">" .
+                               strip_tags($line['title']) . "</div>";
+
                        $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
 
                        $rv['content'] .= "<div onclick=\"return postClicked(event, $id)\"
                        }
 
                        $parsed_updated = make_local_datetime($link, $line["updated"], true,
-                               false, true);
+                               $owner_uid, true);
 
                        $rv['content'] .= "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
 
                        if ($line["link"]) {
-                               $rv['content'] .= "<div clear='both'><a target='_blank'
+                               $rv['content'] .= "<div class='postTitle' clear='both'><a target='_blank'
                                        title=\"".htmlspecialchars($line['title'])."\"
                                        href=\"" .
                                        $line["link"] . "\">" .
                                        truncate_string($line["title"], 100) .
                                        "<span class='author'>$entry_author</span></a></div>";
                        } else {
-                               $rv['content'] .= "<div clear='both'>" . $line["title"] . "$entry_author</div>";
+                               $rv['content'] .= "<div class='postTitle' clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
                        $tag_cache = $line["tag_cache"];
 
                        if (!$tag_cache)
-                               $tags = get_article_tags($link, $id);
+                               $tags = get_article_tags($link, $id, $owner_uid);
                        else
                                $tags = explode(",", $tag_cache);
 
 
                        if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
 
-                       $rv['content'] .= "<div style='float : right'>
+                       $rv['content'] .= "<div class='postTags' style='float : right'>
                                <img src='".theme_image($link, 'images/tag.png')."'
                                class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
 
 
                        $rv['content'] .= "<div class=\"postContent\">";
 
-                       $article_content = sanitize($link, $line["content"], false, false,
+                       // N-grams
+
+                       if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_RELATED_THRESHOLD')) {
+
+                               $ngram_result = db_query($link, "SELECT id,title FROM
+                                               ttrss_entries,ttrss_user_entries
+                                       WHERE ref_id = id AND updated >= NOW() - INTERVAL '7 day'
+                                               AND similarity(title, '$title_escaped') >= "._NGRAM_TITLE_RELATED_THRESHOLD."
+                                               AND title != '$title_escaped'
+                                               AND owner_uid = $owner_uid");
+
+                               if (db_num_rows($ngram_result) > 0) {
+                                       $rv['content'] .= "<div dojoType=\"dijit.form.DropDownButton\">".
+                                               "<span>" . __('Related')."</span>";
+                                       $rv['content'] .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
+
+                                       while ($nline = db_fetch_assoc($ngram_result)) {
+                                               $rv['content'] .= "<div onclick=\"hlOpenInNewTab(null,".$nline['id'].")\"
+                                                       dojoType=\"dijit.MenuItem\">".$nline['title']."</div>";
+
+                                       }
+                                       $rv['content'] .= "</div></div><br/";
+                               }
+                       }
+
+                       $article_content = sanitize($link, $line["content"], false, $owner_uid,
                                $feed_site_url);
 
                        $rv['content'] .= $article_content;
                return $url_path;
        } // function add_feed_url
 
-       /**
-        * Encrypt a password in SHA1.
-        *
-        * @param string $pass The password to encrypt.
-        * @param string $login A optionnal login.
-        * @return string The encrypted password.
-        */
-       function encrypt_password($pass, $login = '') {
-               if ($login) {
-                       return "SHA1X:" . sha1("$login:$pass");
+       function encrypt_password($pass, $salt = '', $mode2 = false) {
+               if ($salt && $mode2) {
+                       return "MODE2:" . hash('sha256', $salt . $pass);
+               } else if ($salt) {
+                       return "SHA1X:" . sha1("$salt:$pass");
                } else {
                        return "SHA1:" . sha1($pass);
                }
        } // function encrypt_password
 
-
        function sanitize_article_content($text) {
                # we don't support CDATA sections in articles, they break our own escaping
                $text = preg_replace("/\[\[CDATA/", "", $text);
        function load_filters($link, $feed, $owner_uid, $action_id = false) {
                $filters = array();
 
-               global $memcache;
-
-               $obj_id = md5("FILTER:$feed:$owner_uid:$action_id");
 
-               if ($memcache && $obj = $memcache->get($obj_id)) {
+               if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
 
-                       return $obj;
-
-               } else {
-
-                       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,
-                               filter_param
-                               FROM ttrss_filters
-                                       LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = '$feed'),
-                                       ttrss_filter_types,ttrss_filter_actions
-                               WHERE
-                                       enabled = true AND
-                                       $ftype_query_part
-                                       ttrss_filters.owner_uid = $owner_uid AND
-                                       ttrss_filter_types.id = filter_type AND
-                                       ttrss_filter_actions.id = action_id AND
-                                       ((cat_filter = true AND ttrss_feeds.cat_id = ttrss_filters.cat_id) OR
-                                       (cat_filter = true AND ttrss_feeds.cat_id IS NULL AND
-                                               ttrss_filters.cat_id IS NULL) OR
-                                       (cat_filter = false AND (feed_id IS NULL OR feed_id = '$feed')))
-                               ORDER BY reg_exp");
+               $result = db_query($link, "SELECT reg_exp,
+                       ttrss_filter_types.name AS name,
+                       ttrss_filter_actions.name AS action,
+                       inverse,
+                       action_param,
+                       filter_param
+                       FROM ttrss_filters
+                               LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = '$feed'),
+                               ttrss_filter_types,ttrss_filter_actions
+                       WHERE
+                               enabled = true AND
+                               $ftype_query_part
+                               ttrss_filters.owner_uid = $owner_uid AND
+                               ttrss_filter_types.id = filter_type AND
+                               ttrss_filter_actions.id = action_id AND
+                               ((cat_filter = true AND ttrss_feeds.cat_id = ttrss_filters.cat_id) OR
+                               (cat_filter = true AND ttrss_feeds.cat_id IS NULL AND
+                                       ttrss_filters.cat_id IS NULL) OR
+                               (cat_filter = false AND (feed_id IS NULL OR feed_id = '$feed')))
+                       ORDER BY reg_exp");
 
-                       while ($line = db_fetch_assoc($result)) {
+               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["filter_param"] = $line["filter_param"];
-                                       $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
+                       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["filter_param"] = $line["filter_param"];
+                               $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
 
-                                       array_push($filters[$line["name"]], $filter);
-                               }
+                               array_push($filters[$line["name"]], $filter);
+                       }
 
-                       if ($memcache) $memcache->add($obj_id, $filters, 0, 3600*8);
 
-                       return $filters;
-               }
+               return $filters;
        }
 
        function get_score_pic($score) {
        }
 
        function get_article_labels($link, $id) {
-               global $memcache;
-
-               $obj_id = md5("LABELS:$id:" . $_SESSION["uid"]);
-
                $rv = array();
 
-               if ($memcache && $obj = $memcache->get($obj_id)) {
-                       return $obj;
-               } else {
-
-                       $result = db_query($link, "SELECT label_cache FROM
-                               ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
-                               $_SESSION["uid"]);
 
-                       $label_cache = db_fetch_result($result, 0, "label_cache");
+               $result = db_query($link, "SELECT label_cache FROM
+                       ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " .
+                       $_SESSION["uid"]);
 
-                       if ($label_cache) {
+               $label_cache = db_fetch_result($result, 0, "label_cache");
 
-                               $label_cache = json_decode($label_cache, true);
+               if ($label_cache) {
 
-                               if ($label_cache["no-labels"] == 1)
-                                       return $rv;
-                               else
-                                       return $label_cache;
-                       }
+                       $label_cache = json_decode($label_cache, true);
 
-                       $result = db_query($link,
-                               "SELECT DISTINCT label_id,caption,fg_color,bg_color
-                                       FROM ttrss_labels2, ttrss_user_labels2
-                               WHERE id = label_id
-                                       AND article_id = '$id'
-                                       AND owner_uid = ".$_SESSION["uid"] . "
-                               ORDER BY caption");
+                       if ($label_cache["no-labels"] == 1)
+                               return $rv;
+                       else
+                               return $label_cache;
+               }
 
-                       while ($line = db_fetch_assoc($result)) {
-                               $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
-                                       $line["bg_color"]);
-                               array_push($rv, $rk);
-                       }
-                       if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
+               $result = db_query($link,
+                       "SELECT DISTINCT label_id,caption,fg_color,bg_color
+                               FROM ttrss_labels2, ttrss_user_labels2
+                       WHERE id = label_id
+                               AND article_id = '$id'
+                               AND owner_uid = ".$_SESSION["uid"] . "
+                       ORDER BY caption");
 
-                       if (count($rv) > 0)
-                               label_update_cache($link, $id, $rv);
-                       else
-                               label_update_cache($link, $id, array("no-labels" => 1));
+               while ($line = db_fetch_assoc($result)) {
+                       $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
+                               $line["bg_color"]);
+                       array_push($rv, $rk);
                }
 
+               if (count($rv) > 0)
+                       label_update_cache($link, $id, $rv);
+               else
+                       label_update_cache($link, $id, array("no-labels" => 1));
+
                return $rv;
        }
 
 
        function label_add_article($link, $id, $label, $owner_uid) {
 
-               global $memcache;
-
-               if ($memcache) {
-                       $obj_id = md5("LABELS:$id:$owner_uid");
-                       $memcache->delete($obj_id);
-               }
-
                $label_id = label_find_id($link, $label, $owner_uid);
 
                if (!$label_id) return;
        }
 
        function label_remove($link, $id, $owner_uid) {
-               global $memcache;
-
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
-               if ($memcache) {
-                       $obj_id = md5("LABELS:$id:$owner_uid");
-                       $memcache->delete($obj_id);
-               }
-
                db_query($link, "BEGIN");
 
                $result = db_query($link, "SELECT caption FROM ttrss_labels2
                db_query($link, "COMMIT");
        }
 
-       function label_create($link, $caption, $fg_color = '', $bg_color = '') {
+       function label_create($link, $caption, $fg_color = '', $bg_color = '', $owner_uid) {
+
+               if (!$owner_uid) $owner_uid = $_SESSION['uid'];
 
                db_query($link, "BEGIN");
 
                $result = false;
 
                $result = db_query($link, "SELECT id FROM ttrss_labels2
-                       WHERE caption = '$caption' AND owner_uid =  ". $_SESSION["uid"]);
+                       WHERE caption = '$caption' AND owner_uid = $owner_uid");
 
                if (db_num_rows($result) == 0) {
                        $result = db_query($link,
                                "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color)
-                                       VALUES ('$caption', '".$_SESSION["uid"]."', '$fg_color', '$bg_color')");
+                                       VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')");
 
                        $result = db_affected_rows($link, $result) != 0;
                }
 
        function get_article_enclosures($link, $id) {
 
-               global $memcache;
-
                $query = "SELECT * FROM ttrss_enclosures
                        WHERE post_id = '$id' AND content_url != ''";
 
-               $obj_id = md5("ENCLOSURES:$id");
-
                $rv = array();
 
-               if ($memcache && $obj = $memcache->get($obj_id)) {
-                       $rv = $obj;
-               } else {
-                       $result = db_query($link, $query);
+               $result = db_query($link, $query);
 
-                       if (db_num_rows($result) > 0) {
-                               while ($line = db_fetch_assoc($result)) {
-                                       array_push($rv, $line);
-                               }
-                               if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
+               if (db_num_rows($result) > 0) {
+                       while ($line = db_fetch_assoc($result)) {
+                               array_push($rv, $line);
                        }
                }
 
 
                        if ($cat_id == -4 || $cat_id == -3) {
                                $result = db_query($link, "SELECT
-                                       id, feed_url, cat_id, title, ".
+                                       id, feed_url, cat_id, title, order_id, ".
                                                SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
                                                FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
                                                " ORDER BY cat_id, title " . $limit_qpart);
                                        $cat_qpart = "cat_id IS NULL";
 
                                $result = db_query($link, "SELECT
-                                       id, feed_url, cat_id, title, ".
+                                       id, feed_url, cat_id, title, order_id, ".
                                                SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
                                                FROM ttrss_feeds WHERE
                                                $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
                                                        "unread" => (int)$unread,
                                                        "has_icon" => $has_icon,
                                                        "cat_id" => (int)$line["cat_id"],
-                                                       "last_updated" => strtotime($line["last_updated"])
+                                                       "last_updated" => strtotime($line["last_updated"]),
+                                                       "order_id" => (int) $line["order_id"],
                                                );
 
                                        array_push($feeds, $row);
 
        function api_get_headlines($link, $feed_id, $limit, $offset,
                                $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
-                               $include_attachments, $since_id) {
-
-                       /* do not rely on params below */
-
-                       $search = db_escape_string($_REQUEST["search"]);
-                       $search_mode = db_escape_string($_REQUEST["search_mode"]);
-                       $match_on = db_escape_string($_REQUEST["match_on"]);
+                               $include_attachments, $since_id,
+                               $search = "", $search_mode = "", $match_on = "") {
 
                        $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
                                $view_mode, $is_cat, $search, $search_mode, $match_on,
 
                                $headline_row["labels"] = $labels;
 
+                               $headline_row["feed_title"] = $line["feed_title"];
+
                                array_push($headlines, $headline_row);
                        }
 
 
                                if (!$ctype) $ctype = __("unknown type");
 
-#                              $filename = substr($url, strrpos($url, "/")+1);
+                               $filename = substr($url, strrpos($url, "/")+1);
 
-                               $entry = format_inline_player($link, $url, $ctype);
+#                              $player = format_inline_player($link, $url, $ctype);
 
 #                              $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
 #                                      $filename . " (" . $ctype . ")" . "</a>";
 
+                               $entry = "<div onclick=\"window.open('".htmlspecialchars($url)."')\"
+                                       dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
+
                                array_push($entries_html, $entry);
 
                                $entry = array();
                                array_push($entries, $entry);
                        }
 
-                       $rv .= "<div class=\"postEnclosures\">";
-
                        if (!get_pref($link, "STRIP_IMAGES")) {
                                if ($always_display_enclosures ||
                                                        !preg_match("/<img/i", $article_content)) {
                                                                $rv .= "<p><img
                                                                alt=\"".htmlspecialchars($entry["filename"])."\"
                                                                src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
+
                                                }
                                        }
                                }
                        }
 
-                       if (count($entries) == 1) {
-                               $rv .= __("Attachment:") . " ";
-                       } else {
-                               $rv .= __("Attachments:") . " ";
-                       }
+                       $rv .= "<div dojoType=\"dijit.form.DropDownButton\">".
+                               "<span>" . __('Attachments')."</span>";
+                       $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
 
-                       $rv .= join(", ", $entries_html);
+                       foreach ($entries_html as $entry) { $rv .= $entry; };
 
-                       $rv .= "</div>";
+                       $rv .= "</div></div>";
                }
 
                return $rv;
         * @return string Absolute URL
         */
        function rewrite_relative_url($url, $rel_url) {
-               if (strpos($rel_url, "://") !== false) {
+               if (strpos($rel_url, "magnet:") === 0) {
+                       return $rel_url;
+               } else if (strpos($rel_url, "://") !== false) {
+                       return $rel_url;
+               } else if (strpos($rel_url, "//") === 0) {
+                       # protocol-relative URL (rare but they exist)
                        return $rel_url;
                } else if (strpos($rel_url, "/") === 0)
                {
 
        }
 
-       function rewrite_urls($line) {
+/*     function rewrite_urls($line) {
                global $url_regex;
 
                $urls = null;
                        "<a target=\"_blank\" href=\"\\1\">\\1</a>", $line);
 
                return $result;
+       } */
+
+       function rewrite_urls($html) {
+               libxml_use_internal_errors(true);
+
+               $charset_hack = '<head>
+                       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+               </head>';
+
+               $doc = new DOMDocument();
+               $doc->loadHTML($charset_hack . $html);
+               $xpath = new DOMXPath($doc);
+
+               $entries = $xpath->query('//*/text()');
+
+               foreach ($entries as $entry) {
+                       if (strstr($entry->wholeText, "://") !== false) {
+                               $text = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i",
+                                       "<a target=\"_blank\" href=\"\\1\">\\1</a>", $entry->wholeText);
+
+                               if ($text != $entry->wholeText) {
+                                       $cdoc = new DOMDocument();
+                                       $cdoc->loadHTML($charset_hack . $text);
+
+
+                                       foreach ($cdoc->childNodes as $cnode) {
+                                               $cnode = $doc->importNode($cnode, true);
+
+                                               if ($cnode) {
+                                                       $entry->parentNode->insertBefore($cnode);
+                                               }
+                                       }
+
+                                       $entry->parentNode->removeChild($entry);
+
+                               }
+                       }
+               }
+
+               $node = $doc->getElementsByTagName('body')->item(0);
+
+               // http://tt-rss.org/forum/viewtopic.php?f=1&t=970
+               if ($node)
+                       return $doc->saveXML($node, LIBXML_NOEMPTYTAG);
+               else
+                       return $html;
        }
 
        function filter_to_sql($filter) {
                $query = "";
 
-               if (DB_TYPE == "pgsql")
-                       $reg_qpart = "~";
-               else
-                       $reg_qpart = "REGEXP";
+               $regexp_valid = preg_match('/' . $filter['reg_exp'] . '/',
+                       $filter['reg_exp']) !== FALSE;
 
-               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 ($regexp_valid) {
 
-                               if ($filter["filter_param"] == "before")
-                                       $cmp_qpart = "<";
-                               else
-                                       $cmp_qpart = ">=";
+                       if (DB_TYPE == "pgsql")
+                               $reg_qpart = "~";
+                       else
+                               $reg_qpart = "REGEXP";
 
-                               $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;
-               }
+                       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["inverse"])
-                       $query = "NOT ($query)";
+                                       if ($filter["filter_param"] == "before")
+                                               $cmp_qpart = "<";
+                                       else
+                                               $cmp_qpart = ">=";
 
-               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)";
+                                       $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;
                        }
-                       $query .= " AND ";
-               }
 
+                       if ($filter["inverse"])
+                               $query = "NOT ($query)";
 
-               return $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;
+               } else {
+                       return false;
+               }
        }
 
        // Status codes:
                return $rv;
        }
 
+       if (!function_exists('gzdecode')) {
+               function gzdecode($string) { // no support for 2nd argument
+                       return file_get_contents('compress.zlib://data:who/cares;base64,'.
+                               base64_encode($string));
+               }
+       }
+
        function perform_data_import($link, $filename, $owner_uid) {
 
                $num_imported = 0;
                $num_processed = 0;
                $num_feeds_created = 0;
 
-               $doc = DOMDocument::load($filename);
+               $doc = @DOMDocument::load($filename);
+
+               if (!$doc) {
+                       $contents = file_get_contents($filename);
+
+                       if ($contents) {
+                               $data = @gzuncompress($contents);
+                       }
+
+                       if (!$data) {
+                               $data = @gzdecode($contents);
+                       }
+
+                       if ($data)
+                               $doc = DOMDocument::loadXML($data);
+               }
 
                if ($doc) {
 
                        $xpath = new DOMXpath($doc);
+
+                       $container = $doc->firstChild;
+
+                       if ($container && $container->hasAttribute('schema-version')) {
+                               $schema_version = $container->getAttribute('schema-version');
+
+                               if ($schema_version != SCHEMA_VERSION) {
+                                       print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
+                                       return;
+                               }
+
+                       } else {
+                               print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
+                               return;
+                       }
+
                        $articles = $xpath->query("//article");
 
                        foreach ($articles as $article_node) {
                                        $article = array();
 
                                        foreach ($article_node->childNodes as $child) {
-                                               $article[$child->nodeName] = db_escape_string($child->nodeValue);
+                                               if ($child->nodeName != 'label_cache')
+                                                       $article[$child->nodeName] = db_escape_string($child->nodeValue);
+                                               else
+                                                       $article[$child->nodeName] = $child->nodeValue;
                                        }
 
                                        //print_r($article);
                                                                $score = (int) $article['score'];
 
                                                                $tag_cache = $article['tag_cache'];
-                                                               $label_cache = $article['label_cache'];
+                                                               $label_cache = db_escape_string($article['label_cache']);
+                                                               $note = $article['note'];
 
                                                                //print "Importing " . $article['title'] . "<br/>";
 
                                                                $result = db_query($link,
                                                                        "INSERT INTO ttrss_user_entries
                                                                        (ref_id, owner_uid, feed_id, unread, last_read, marked,
-                                                                               published, score, tag_cache, label_cache, uuid)
+                                                                               published, score, tag_cache, label_cache, uuid, note)
                                                                        VALUES ($ref_id, $owner_uid, $feed, false,
-                                                                               NULL, $marked, $published, $score, '$tag_cache', '$label_cache', '')");
+                                                                               NULL, $marked, $published, $score, '$tag_cache',
+                                                                                       '$label_cache', '', '$note')");
+
+                                                               $label_cache = json_decode($label_cache, true);
+
+                                                               if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
+                                                                       foreach ($label_cache as $label) {
+
+                                                                               label_create($link, $label[1],
+                                                                                       $label[2], $label[3], $owner_uid);
+
+                                                                               label_add_article($link, $ref_id, $label[1], $owner_uid);
+
+                                                                       }
+                                                               }
 
                                                                //db_query($link, "COMMIT");
                                                        }
                }
        }
 
+       function get_random_bytes($length) {
+               if (function_exists('openssl_random_pseudo_bytes')) {
+                       return openssl_random_pseudo_bytes($length);
+               } else {
+                       $output = "";
+
+                       for ($i = 0; $i < $length; $i++)
+                               $output .= chr(mt_rand(0, 255));
+
+                       return $output;
+               }
+       }
+
+       function read_stdin() {
+               $fp = fopen("php://stdin", "r");
+
+               if ($fp) {
+                       $line = trim(fgets($fp));
+                       fclose($fp);
+                       return $line;
+               }
+
+               return null;
+       }
 ?>