]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
config: remove option ENABLE_UPDATE_DAEMON
[tt-rss.git] / functions.php
index c9a8c86868224032e552cf384e56ff2ad61f0c61..141d7bc8c71e266768e987c18bb20401d3d38e5d 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;
+                       _textdomain("messages");
+                       _bind_textdomain_codeset("messages", "UTF-8");
                }
-               function startup_gettext() {
-                       // no-op
-                       return true;
-               }
-       } // 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]";
 
+       $config->set('HTML.SafeObject', true);
        $config->set('HTML', 'Allowed', $allowed);
+       $config->set('Output.FlashCompat', true);
+       $config->set('Attr.EnableID', true);
+
        $purifier = new HTMLPurifier($config);
 
        /**
                        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);
 
                        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;
 
 
                        $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'");
 
                }
                $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'");
 
                        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);
+
                                $rss = @fetch_rss($fetch_url);
                        } else {
                                if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
                                                get_feed_update_interval($link, $feed)*60);
                                }
 
-                               if (is_dir(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);
                                }
 
                        $filters = load_filters($link, $feed, $owner_uid);
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'] == 2) {
                                print_r($filters);
                        }
 
                                return; // no articles
                        }
 
+                       if ($pubsub_state != 2) {
+
+                               $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 ($feed_hub_url && function_exists('curl_init')) {
+                                       $callback_url = get_self_url_prefix() .
+                                               "/backend.php?op=pubsub&id=$feed";
+
+                                       $s = new Subscriber($feed_hub_url, $callback_url);
+
+                                       $s->subscribe($fetch_url);
+
+                                       db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1
+                                               WHERE id = '$feed'");
+                               }
+                       }
+
                        if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
                                _debug("update_rss_feed: processing articles...");
                        }
                                                        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
 
                                        $post_needs_update = false;
 
-                                       if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
-                                               ($content_hash != $orig_content_hash)) {
+                                       if ($content_hash != $orig_content_hash) {
 //                                             print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
                                                $post_needs_update = true;
                                        }
                                                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)) {
+                                               if ($mark_unread_on_update) {
                                                        db_query($link, "UPDATE ttrss_user_entries
                                                                SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
-                                               } else {
+                                               } 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");
                                                }
                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) {
 
                if (!SINGLE_USER_MODE) {
                        $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 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) {
                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");
 
        function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
                $limit, $search, $search_mode, $match_on, $view_mode = false) {
 
+               require_once "lib/MiniTemplator.class.php";
+
                $note_style =   "float : right; 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));
+
+               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()));
 
-                       $rfc822_date = date('r', strtotime($line["updated"]));
+               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, '...'));
 
-                       print "<pubDate>$rfc822_date</pubDate>";
+                       $content = sanitize_rss($link, $line["content_preview"], false, $owner_uid);
 
-                       if ($line["author"]) {
-                               print "<author>" . htmlspecialchars($line["author"]) . "</author>";
+                       if ($line['note']) {
+                               $content = "<div style=\"$note_style\">" . $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');
+               }
+
+               $tmp = "";
 
-               print "</channel></rss>";
+               $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) {
                        mb_strtolower(strip_tags($title), 'utf-8'));
        }
 
-       function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
+       function format_headline_subtoolbar($link, $feed_site_url, $feed_title,
                        $feed_id, $is_cat, $search, $match_on,
                        $search_mode, $view_mode, $error) {
 
-                       $page_prev_link = "viewFeedGoPage(-1)";
-                       $page_next_link = "viewFeedGoPage(1)";
-                       $page_first_link = "viewFeedGoPage(0)";
+               $page_prev_link = "viewFeedGoPage(-1)";
+               $page_next_link = "viewFeedGoPage(1)";
+               $page_first_link = "viewFeedGoPage(0)";
 
-                       $catchup_page_link = "catchupPage()";
-                       $catchup_feed_link = "catchupCurrentFeed()";
-                       $catchup_sel_link = "catchupSelection()";
+               $catchup_page_link = "catchupPage()";
+               $catchup_feed_link = "catchupCurrentFeed()";
+               $catchup_sel_link = "catchupSelection()";
 
-                       $archive_sel_link = "archiveSelection()";
-                       $delete_sel_link = "deleteSelection()";
+               $archive_sel_link = "archiveSelection()";
+               $delete_sel_link = "deleteSelection()";
 
-                       $sel_all_link = "selectArticles('all')";
-                       $sel_unread_link = "selectArticles('unread')";
-                       $sel_none_link = "selectArticles('none')";
-                       $sel_inv_link = "selectArticles('invert')";
+               $sel_all_link = "selectArticles('all')";
+               $sel_unread_link = "selectArticles('unread')";
+               $sel_none_link = "selectArticles('none')";
+               $sel_inv_link = "selectArticles('invert')";
 
-                       $tog_unread_link = "selectionToggleUnread()";
-                       $tog_marked_link = "selectionToggleMarked()";
-                       $tog_published_link = "selectionTogglePublished()";
+               $tog_unread_link = "selectionToggleUnread()";
+               $tog_marked_link = "selectionToggleMarked()";
+               $tog_published_link = "selectionTogglePublished()";
 
-                       print "<div id=\"subtoolbar_main\">";
+               $reply = "<div id=\"subtoolbar_main\">";
 
-                       print __('Select:')."
-                               <a href=\"#\" onclick=\"$sel_all_link\">".__('All')."</a>,
-                               <a href=\"#\" onclick=\"$sel_unread_link\">".__('Unread')."</a>,
-                               <a href=\"#\" onclick=\"$sel_inv_link\">".__('Invert')."</a>,
-                               <a href=\"#\" onclick=\"$sel_none_link\">".__('None')."</a></li>";
+               $reply .= __('Select:')."
+                       <a href=\"#\" onclick=\"$sel_all_link\">".__('All')."</a>,
+                       <a href=\"#\" onclick=\"$sel_unread_link\">".__('Unread')."</a>,
+                       <a href=\"#\" onclick=\"$sel_inv_link\">".__('Invert')."</a>,
+                       <a href=\"#\" onclick=\"$sel_none_link\">".__('None')."</a></li>";
 
-                       print " ";
+               $reply .= " ";
 
-                       print "<select dojoType=\"dijit.form.Select\"
-                               onchange=\"headlineActionsChange(this)\">";
-                       print "<option value=\"false\">".__('Actions...')."</option>";
+               $reply .= "<select dojoType=\"dijit.form.Select\"
+                       onchange=\"headlineActionsChange(this)\">";
+               $reply .= "<option value=\"false\">".__('Actions...')."</option>";
 
-                       print "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
+               $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
 
-                       print "<option value=\"$tog_unread_link\">".__('Unread')."</option>
-                               <option value=\"$tog_marked_link\">".__('Starred')."</option>
-                               <option value=\"$tog_published_link\">".__('Published')."</option>";
+               $reply .= "<option value=\"$tog_unread_link\">".__('Unread')."</option>
+                       <option value=\"$tog_marked_link\">".__('Starred')."</option>
+                       <option value=\"$tog_published_link\">".__('Published')."</option>";
 
-                       print "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
+               $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
 
-                       print "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
+               $reply .= "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
 
-                       if ($feed_id != "0") {
-                               print "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
-                       } else {
-                               print "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
-                               print "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
+               if ($feed_id != "0") {
+                       $reply .= "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
+               } else {
+                       $reply .= "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
+                       $reply .= "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
 
-                       }
+               }
 
-                       print "<option value=\"emailArticle(false)\">".__('Forward by email').
-                               "</option>";
+               $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
+                       "</option>";
 
-                       $rss_link = htmlspecialchars(get_self_url_prefix() .
-                               "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat&view_mode=$view_mode$search_q");
+               if ($is_cat) $cat_q = "&is_cat=$is_cat";
 
-                       print "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
+               $rss_link = htmlspecialchars(get_self_url_prefix() .
+                       "/backend.php?op=rss&id=$feed_id$cat_q$search_q");
 
-                       print "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
+               $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
 
-                       print "<option value=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
+               $reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
 
-                       print "</select>";
+               $reply .= "<option value=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
 
-                       print "</div>";
+               $reply .= "</select>";
 
-                       print "<div id=\"subtoolbar_ftitle\">";
+               $reply .= "</div>";
 
-                       if ($feed_site_url) {
-                               $target = "target=\"_blank\"";
-                               print "<a title=\"".__("Visit the website")."\" $target href=\"$feed_site_url\">".
-                                       truncate_string($feed_title,30)."</a>";
+               $reply .= "<div id=\"subtoolbar_ftitle\">";
 
-                               if ($error) {
-                                       print " (<span class=\"error\" title=\"$error\">Error</span>)";
-                               }
+               if ($feed_site_url) {
+                       $target = "target=\"_blank\"";
+                       $reply .= "<a title=\"".__("Visit the website")."\" $target href=\"$feed_site_url\">".
+                               truncate_string($feed_title,30)."</a>";
 
-                       } else {
-                               if ($feed_id < -10) {
-                                       $label_id = -11-$feed_id;
+                       if ($error) {
+                               $reply .= " (<span class=\"error\" title=\"$error\">Error</span>)";
+                       }
 
-                                       $result = db_query($link, "SELECT fg_color, bg_color
-                                               FROM ttrss_labels2 WHERE id = '$label_id' AND owner_uid = " .
-                                               $_SESSION["uid"]);
+               } else {
+                       if ($feed_id < -10) {
+                               $label_id = -11-$feed_id;
 
-                                       if (db_num_rows($result) != 0) {
-                                               $fg_color = db_fetch_result($result, 0, "fg_color");
-                                               $bg_color = db_fetch_result($result, 0, "bg_color");
+                               $result = db_query($link, "SELECT fg_color, bg_color
+                                       FROM ttrss_labels2 WHERE id = '$label_id' AND owner_uid = " .
+                                       $_SESSION["uid"]);
 
-                                               print "<span style='background : $bg_color; color : $fg_color'>";
-                                               print $feed_title;
-                                               print "</span>";
-                                       } else {
-                                               print $feed_title;
-                                       }
+                               if (db_num_rows($result) != 0) {
+                                       $fg_color = db_fetch_result($result, 0, "fg_color");
+                                       $bg_color = db_fetch_result($result, 0, "bg_color");
 
+                                       $reply .= "<span style='background : $bg_color; color : $fg_color'>";
+                                       $reply .= $feed_title;
+                                       $reply .= "</span>";
                                } else {
-                                       print $feed_title;
+                                       $reply .= $feed_title;
                                }
-                       }
 
-                       if ($search) {
-                               $search_q = "&q=$search&m=$match_on&smode=$search_mode";
                        } else {
-                               $search_q = "";
+                               $reply .= $feed_title;
                        }
+               }
 
-                       // 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";
+               if ($search) {
+                       $search_q = "&q=$search&m=$match_on&smode=$search_mode";
+               } else {
+                       $search_q = "";
+               }
 
-                       print "
-                               <a href=\"#\"
-                                       title=\"".__("View as RSS feed")."\"
-                                       onclick=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">
-                                       <img class=\"noborder\" style=\"vertical-align : middle\" src=\"images/feed-icon-12x12.png\"></a>";
+               $reply .= "
+                       <a href=\"#\"
+                               title=\"".__("View as RSS feed")."\"
+                               onclick=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">
+                               <img class=\"noborder\" style=\"vertical-align : middle\" src=\"images/feed-icon-12x12.png\"></a>";
 
-                       print "</div>";
+               $reply .= "</div>";
 
-               }
+               return $reply;
+       }
 
        function outputFeedList($link, $special = true) {
 
                return $entry;
        }
 
-       function outputArticleXML($link, $id, $feed_id, $mark_as_read = true,
+       function format_article($link, $id, $feed_id, $mark_as_read = true,
                $zoom_mode = false) {
 
+               $rv = array();
+
+               $rv['id'] = $id;
+
                /* we can figure out feed_id from article id anyway, why do we
                 * pass feed_id here? let's ignore the argument :( */
 
 
                $feed_id = (int) db_fetch_result($result, 0, "feed_id");
 
-               if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
+               $rv['feed_id'] = $feed_id;
+
+               //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"]);
 
                        if ($zoom_mode) {
                                header("Content-Type: text/html");
-                               print "<html><head>
+                               $rv['content'] .= "<html><head>
                                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
                                                <title>Tiny Tiny RSS - ".$line["title"]."</title>
                                                <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
                                        </head><body>";
                        }
 
-                       print "<div id=\"PTITLE-$id\" style=\"display : none\">" .
+                       $rv['content'] .= "<div id=\"PTITLE-$id\" style=\"display : none\">" .
                                truncate_string(strip_tags($line['title']), 15) . "</div>";
 
-                       print "<div class=\"postReply\" id=\"POST-$id\">";
-
-                       /* print "<div dojoType=\"dijit.Menu\" style=\"display: none;\"
-                               targetNodeIds=\"POSTHDR-$id\">";
-                       print "<div onclick=\"postOpenInNewTab(event, $id)\"
-                               dojoType=\"dijit.MenuItem\">".__('View in a new tab')."</div>";
-                       print "<div dojoType=\"dijit.MenuSeparator\"></div>";
-                       print "<div onclick=\"openArticleInNewWindow($id)\"
-                               dojoType=\"dijit.MenuItem\">".__('Open original article')."</div>";
-                       print "</div>"; */
+                       $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
 
-                       print "<div onclick=\"return postClicked(event, $id)\"
+                       $rv['content'] .= "<div onclick=\"return postClicked(event, $id)\"
                                class=\"postHeader\" id=\"POSTHDR-$id\">";
 
                        $entry_author = $line["author"];
                        $parsed_updated = make_local_datetime($link, $line["updated"], true,
                                false, true);
 
-                       print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
+                       $rv['content'] .= "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
 
                        if ($line["link"]) {
-                               print "<div clear='both'><a target='_blank'
+                               $rv['content'] .= "<div 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 {
-                               print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
+                               $rv['content'] .= "<div clear='both'>" . $line["title"] . "$entry_author</div>";
                        }
 
                        $tags_str = format_tags_string(get_article_tags($link, $id), $id);
 
                        if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
 
-                       print "<div style='float : right'>
+                       $rv['content'] .= "<div style='float : right'>
                                <img src='".theme_image($link, 'images/tag.png')."'
                                class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
 
                        if (!$zoom_mode) {
-                               print "<span id=\"ATSTR-$id\">$tags_str</span>
+                               $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
                                        <a title=\"".__('Edit tags for this article')."\"
                                        href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
 
-                               print "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
+                               $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
                                                class='tagsPic' style=\"cursor : pointer\"
                                                onclick=\"postOpenInNewTab(event, $id)\"
                                                alt='Zoom' title='".__('Open article in new tab')."'>";
 
                                //$note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
 
-                               print "<img src=\"".theme_image($link, 'images/art-pub-note.png')."\"
+                               $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-pub-note.png')."\"
                                                class='tagsPic' style=\"cursor : pointer\"
                                                onclick=\"editArticleNote($id)\"
                                                alt='PubNote' title='".__('Edit article note')."'>";
 
                                if (DIGEST_ENABLE) {
-                                       print "<img src=\"".theme_image($link, 'images/art-email.png')."\"
+                                       $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-email.png')."\"
                                                class='tagsPic' style=\"cursor : pointer\"
                                                onclick=\"emailArticle($id)\"
                                                alt='Zoom' title='".__('Forward by email')."'>";
                                }
 
                                if (ENABLE_TWEET_BUTTON) {
-                                       print "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
+                                       $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
                                                        class='tagsPic' style=\"cursor : pointer\"
                                                        onclick=\"tweetArticle($id)\"
                                                        alt='Zoom' title='".__('Share on Twitter')."'>";
                                }
 
-                               print "<img src=\"".theme_image($link, 'images/digest_checkbox.png')."\"
+                               $rv['content'] .= "<img src=\"".theme_image($link, 'images/digest_checkbox.png')."\"
                                                class='tagsPic' style=\"cursor : pointer\"
                                                onclick=\"closeArticlePanel($id)\"
                                                alt='Zoom' title='".__('Close this panel')."'>";
 
                        } else {
                                $tags_str = strip_tags($tags_str);
-                               print "<span id=\"ATSTR-$id\">$tags_str</span>";
+                               $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
                        }
-                       print "</div>";
-                       print "<div clear='both'>$entry_comments</div>";
+                       $rv['content'] .= "</div>";
+                       $rv['content'] .= "<div clear='both'>$entry_comments</div>";
 
                        if ($line["orig_feed_id"]) {
 
 
                                if (db_num_rows($tmp_result) != 0) {
 
-                                       print "<div clear='both'>";
-                                       print __("Originally from:");
+                                       $rv['content'] .= "<div clear='both'>";
+                                       $rv['content'] .= __("Originally from:");
 
-                                       print "&nbsp;";
+                                       $rv['content'] .= "&nbsp;";
 
                                        $tmp_line = db_fetch_assoc($tmp_result);
 
-                                       print "<a target='_blank'
+                                       $rv['content'] .= "<a target='_blank'
                                                href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
                                                $tmp_line['title'] . "</a>";
 
-                                       print "&nbsp;";
+                                       $rv['content'] .= "&nbsp;";
 
-                                       print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
-                                       print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
+                                       $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                       $rv['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
 
-                                       print "</div>";
+                                       $rv['content'] .= "</div>";
                                }
                        }
 
-                       print "</div>";
+                       $rv['content'] .= "</div>";
 
-                       print "<div class=\"postIcon\">" .
+                       $rv['content'] .= "<div class=\"postIcon\">" .
                                "<a target=\"_blank\" title=\"".__("Visit the website")."\"$
                                href=\"".htmlspecialchars($feed_site_url)."\">".
                                $feed_icon . "</a></div>";
 
-                       print "<div id=\"POSTNOTE-$id\">";
+                       $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
                                if ($line['note']) {
-                                       print format_article_note($id, $line['note']);
+                                       $rv['content'] .= format_article_note($id, $line['note']);
                                }
-                       print "</div>";
+                       $rv['content'] .= "</div>";
 
-                       print "<div class=\"postContent\">";
+                       $rv['content'] .= "<div class=\"postContent\">";
 
                        $article_content = sanitize_rss($link, $line["content"], false, false,
                                $feed_site_url);
 
-                       print $article_content;
+                       $rv['content'] .= $article_content;
 
-                       print_article_enclosures($link, $id, $always_display_enclosures,
-                               $article_content);
+                       $rv['content'] .= format_article_enclosures($link, $id,
+                               $always_display_enclosures, $article_content);
 
-                       print "</div>";
+                       $rv['content'] .= "</div>";
 
-                       print "</div>";
+                       $rv['content'] .= "</div>";
 
                }
 
-               if (!$zoom_mode) {
-                       print "]]></article>";
-               } else {
-                       print "
+               if ($zoom_mode) {
+                       $rv['content'] .= "
                                <div style=\"text-align : center\">
                                <button onclick=\"return window.close()\">".
                                        __("Close this window")."</button></div>";
-                       print "</body></html>";
-
+                       $rv['content'] .= "</body></html>";
                }
 
+               return $rv;
+
        }
 
-       function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
+       function format_headlines_list($link, $feed, $subop, $view_mode, $limit, $cat_view,
                                        $next_unread_feed, $offset, $vgr_last_feed = false,
                                        $override_order = false) {
 
                $disable_cache = false;
 
+               $reply = array();
+
                $timing_info = getmicrotime();
 
                $topmost_article_ids = array();
                                "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
 
                        if (db_num_rows($result) == 0) {
-                               print "<div align='center'>".__('Feed not found.')."</div>";
-                               return;
+                               $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
                        }
                }
 
 
                $vgroup_last_feed = $vgr_last_feed;
 
-/*             if ($feed == -2) {
-                       $feed_site_url = article_publish_url($link);
-               } */
-
                /// STOP //////////////////////////////////////////////////////////////////////////////////
 
-               print "<toolbar><![CDATA[";
-
                if (!$offset) {
-//                     print "<div id=\"headlinesContainer\" $rtl_tag>";
-
-                       if (!$result) {
-                               print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
-                               return;
-                       }
 
                        if (db_num_rows($result) > 0) {
-                               print_headline_subtoolbar($link, $feed_site_url, $feed_title,
+                               $reply['toolbar'] = format_headline_subtoolbar($link, $feed_site_url, $feed_title,
                                        $feed, $cat_view, $search, $match_on, $search_mode, $view_mode,
                                        $last_error);
-
-//                             print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
-
                        }
                }
 
-               print "]]></toolbar><content><![CDATA[";
-
                $headlines_count = db_num_rows($result);
 
                if (db_num_rows($result) > 0) {
 
                                                        $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
 
-                                                       print "<div class='cdmFeedTitle'>".
+                                                       $reply['content'] .= "<div class='cdmFeedTitle'>".
                                                                "<div style=\"float : right\">$feed_icon_img</div>".
                                                                "<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
                                                                $line["feed_title"]."</a> $vf_catchup_link</div>";
                                        $mouseover_attrs = "onmouseover='postMouseIn($id)'
                                                onmouseout='postMouseOut($id)'";
 
-                                       print "<div class='$class' id='RROW-$id' $mouseover_attrs>";
+                                       $reply['content'] .= "<div class='$class' id='RROW-$id' $mouseover_attrs>";
 
-                                       print "<div class='hlUpdPic'>$update_pic</div>";
+                                       $reply['content'] .= "<div class='hlUpdPic'>$update_pic</div>";
 
-                                       print "<div class='hlLeft'>";
+                                       $reply['content'] .= "<div class='hlLeft'>";
 
-                                       print "<input type=\"checkbox\" onclick=\"tSR(this)\"
+                                       $reply['content'] .= "<input type=\"checkbox\" onclick=\"tSR(this)\"
                                                        id=\"RCHK-$id\">";
 
-                                       print "$marked_pic";
-                                       print "$published_pic";
+                                       $reply['content'] .= "$marked_pic";
+                                       $reply['content'] .= "$published_pic";
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "<div onclick='return hlClicked(event, $id)'
+                                       $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
                                                class=\"hlTitle\"><span class='hlContent$hlc_suffix'>";
-                                       print "<a id=\"RTITLE-$id\"
+                                       $reply['content'] .= "<a id=\"RTITLE-$id\"
                                                href=\"" . htmlspecialchars($line["link"]) . "\"
-                                               onclick=\"return false;\">" .
+                                               onclick=\"\">" .
                                                $line["title"];
 
                                        if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
                                                if ($content_preview) {
-                                                       print "<span class=\"contentPreview\"> - $content_preview</span>";
+                                                       $reply['content'] .= "<span class=\"contentPreview\"> - $content_preview</span>";
                                                }
                                        }
 
-                                       print "</a></span>";
+                                       $reply['content'] .= "</a></span>";
 
-                                       print $labels_str;
+                                       $reply['content'] .= $labels_str;
 
                                        /* if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
                                                if (@$line["feed_title"]) {
                                                }
                                        } */
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-
-
-                                       print "<div class=\"hlRight\">";
-                                       print "<span class=\"hlUpdated\">$updated_fmt</span>";
-                                       print $score_pic;
+                                       $reply['content'] .= "<div class=\"hlRight\">";
+                                       $reply['content'] .= "<span class=\"hlUpdated\">$updated_fmt</span>";
+                                       $reply['content'] .= $score_pic;
 
                                        if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
 
-                                               print "<span onclick=\"viewfeed($feed_id)\"
+                                               $reply['content'] .= "<span onclick=\"viewfeed($feed_id)\"
                                                        title=\"".htmlspecialchars($line['feed_title'])."\">
                                                        $feed_icon_img<span>";
                                        }
 
-                                       print "</div>";
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
+                                       $reply['content'] .= "</div>";
 
                                } else {
 
                                                                //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
                                                        }
 
-                                                       print "<div class='cdmFeedTitle'>".
+                                                       $reply['content'] .= "<div class='cdmFeedTitle'>".
                                                                "<div style=\"float : right\">$feed_icon_img</div>".
                                                                "<a href=\"#\" onclick=\"viewfeed($feed_id)\">".
                                                                $line["feed_title"]."</a> $vf_catchup_link</div>";
                                        $mouseover_attrs = "onmouseover='postMouseIn($id)'
                                                onmouseout='postMouseOut($id)'";
 
-                                       print "<div class=\"$class\"
+                                       $reply['content'] .= "<div class=\"$class\"
                                                id=\"RROW-$id\" $mouseover_attrs'>";
 
-                                       print "<div class=\"cdmHeader\">";
+                                       $reply['content'] .= "<div class=\"cdmHeader\">";
 
-                                       print "<div style='float : right'>";
-                                       print "<span class='updated'>$updated_fmt</span>";
-                                       print "$score_pic";
+                                       $reply['content'] .= "<div style='float : right'>";
+                                       $reply['content'] .= "<span class='updated'>$updated_fmt</span>";
+                                       $reply['content'] .= "$score_pic";
 
                                        if (!get_pref($link, "VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
-                                               print "<span style=\"cursor : pointer\"
+                                               $reply['content'] .= "<span style=\"cursor : pointer\"
                                                        title=\"".htmlspecialchars($line["feed_title"])."\"
                                                        onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
                                        }
-                                       print "<div class=\"updPic\">$update_pic</div>";
+                                       $reply['content'] .= "<div class=\"updPic\">$update_pic</div>";
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
+                                       $reply['content'] .= "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
                                                        'RROW-$id')\" id=\"RCHK-$id\"/>";
 
-                                       print "$marked_pic";
-                                       print "$published_pic";
+                                       $reply['content'] .= "$marked_pic";
+                                       $reply['content'] .= "$published_pic";
 
-                                       print "<span id=\"RTITLE-$id\"
+                                       $reply['content'] .= "<span id=\"RTITLE-$id\"
                                                onclick=\"return cdmClicked(event, $id);\"
                                                class=\"titleWrap$hlc_suffix\">
                                                <a class=\"title\"
                                                truncate_string($line["title"], 100) .
                                                " $entry_author</a>";
 
-                                       print $labels_str;
+                                       $reply['content'] .= $labels_str;
 
                                        if (!$expand_cdm)
                                                $content_hidden = "style=\"display : none\"";
                                        else
                                                $excerpt_hidden = "style=\"display : none\"";
 
-                                       print "<span $excerpt_hidden
+                                       $reply['content'] .= "<span $excerpt_hidden
                                                id=\"CEXC-$id\" class=\"cdmExcerpt\"> - $content_preview</span>";
 
-                                       print "</span>";
+                                       $reply['content'] .= "</span>";
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "<div class=\"cdmContent\" $content_hidden
+                                       $reply['content'] .= "<div class=\"cdmContent\" $content_hidden
                                                onclick=\"return cdmClicked(event, $id);\"
                                                id=\"CICD-$id\">";
 
-                                       print "<div class=\"cdmContentInner\">";
+                                       $reply['content'] .= "<div class=\"cdmContentInner\">";
 
                        if ($line["orig_feed_id"]) {
 
 
                                                if (db_num_rows($tmp_result) != 0) {
 
-                                                       print "<div clear='both'>";
-                                                       print __("Originally from:");
+                                                       $reply['content'] .= "<div clear='both'>";
+                                                       $reply['content'] .= __("Originally from:");
 
-                                                       print "&nbsp;";
+                                                       $reply['content'] .= "&nbsp;";
 
                                                        $tmp_line = db_fetch_assoc($tmp_result);
 
-                                                       print "<a target='_blank'
+                                                       $reply['content'] .= "<a target='_blank'
                                                                href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
                                                                $tmp_line['title'] . "</a>";
 
-                                                       print "&nbsp;";
+                                                       $reply['content'] .= "&nbsp;";
 
-                                                       print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
-                                                       print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
+                                                       $reply['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                                       $reply['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
 
-                                                       print "</div>";
+                                                       $reply['content'] .= "</div>";
                                                }
                                        }
 
                                                $article_content = '';
                                        }
 
-                                       print "<div id=\"POSTNOTE-$id\">";
+                                       $reply['content'] .= "<div id=\"POSTNOTE-$id\">";
                                        if ($line['note']) {
-                                               print format_article_note($id, $line['note']);
+                                               $reply['content'] .= format_article_note($id, $line['note']);
                                        }
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "<span id=\"CWRAP-$id\">$article_content</span>";
+                                       $reply['content'] .= "<span id=\"CWRAP-$id\">$article_content</span>";
 
                                        $tmp_result = db_query($link, "SELECT always_display_enclosures FROM
                                                ttrss_feeds WHERE id = ".
                                        $always_display_enclosures = sql_bool_to_bool(db_fetch_result($tmp_result,
                                                0, "always_display_enclosures"));
 
-                                       print_article_enclosures($link, $id, $always_display_enclosures,
+                                       $reply['content'] .= format_article_enclosures($link, $id, $always_display_enclosures,
                                                $article_content);
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "<div class=\"cdmFooter\">";
+                                       $reply['content'] .= "<div class=\"cdmFooter\">";
 
                                        $tags_str = format_tags_string(get_article_tags($link, $id), $id);
 
-                                       print "<img src='".theme_image($link,
+                                       $reply['content'] .= "<img src='".theme_image($link,
                                                        'images/tag.png')."' alt='Tags' title='Tags'>
                                                <span id=\"ATSTR-$id\">$tags_str</span>
                                                <a title=\"".__('Edit tags for this article')."\"
                                                href=\"#\" onclick=\"editArticleTags($id, $feed_id, true)\">(+)</a>";
 
-                                       print "<div style=\"float : right\">";
+                                       $reply['content'] .= "<div style=\"float : right\">";
 
-                                       print "<img src=\"images/art-zoom.png\"
+                                       $reply['content'] .= "<img src=\"images/art-zoom.png\"
                                                onclick=\"zoomToArticle(event, $id)\"
                                                style=\"cursor : pointer\"
                                                alt='Zoom'
 
                                        //$note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
 
-                                       print "<img src=\"images/art-pub-note.png\"
+                                       $reply['content'] .= "<img src=\"images/art-pub-note.png\"
                                                style=\"cursor : pointer\" style=\"cursor : pointer\"
                                                onclick=\"editArticleNote($id)\"
                                                alt='PubNote' title='".__('Edit article note')."'>";
 
                                        if (DIGEST_ENABLE) {
-                                               print "<img src=\"".theme_image($link, 'images/art-email.png')."\"
+                                               $reply['content'] .= "<img src=\"".theme_image($link, 'images/art-email.png')."\"
                                                        style=\"cursor : pointer\"
                                                        onclick=\"emailArticle($id)\"
                                                        alt='Zoom' title='".__('Forward by email')."'>";
                                        }
 
                                        if (ENABLE_TWEET_BUTTON) {
-                                               print "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
+                                               $reply['content'] .= "<img src=\"".theme_image($link, 'images/art-tweet.png')."\"
                                                        class='tagsPic' style=\"cursor : pointer\"
                                                        onclick=\"tweetArticle($id)\"
                                                        alt='Zoom' title='".__('Share on Twitter')."'>";
                                        }
 
-                                       print "<img src=\"images/digest_checkbox.png\"
+                                       $reply['content'] .= "<img src=\"images/digest_checkbox.png\"
                                                style=\"cursor : pointer\" style=\"cursor : pointer\"
                                                onclick=\"dismissArticle($id)\"
                                                alt='Dismiss' title='".__('Dismiss article')."'>";
 
-                                       print "</div>";
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
-                                       print "</div>";
+                                       $reply['content'] .= "</div>";
 
                                }
 
                        }
 
                        if (!$offset && $message) {
-                               print "<div class='whiteBox'>$message";
+                               $reply['content'] .= "<div class='whiteBox'>$message";
 
-                               print "<p class=\"small\"><span class=\"insensitive\">";
+                               $reply['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
 
                                $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
                                        WHERE owner_uid = " . $_SESSION['uid']);
                                $last_updated = db_fetch_result($result, 0, "last_updated");
                                $last_updated = make_local_datetime($link, $last_updated, false);
 
-                               printf(__("Feeds last updated at %s"), $last_updated);
+                               $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
 
                                $result = db_query($link, "SELECT COUNT(id) AS num_errors
                                        FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
                                $num_errors = db_fetch_result($result, 0, "num_errors");
 
                                if ($num_errors > 0) {
-                                       print "<br/>";
-                                       print "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
+                                       $reply['content'] .= "<br/>";
+                                       $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
                                                __('Some feeds have update errors (click for details)')."</a>";
                                }
-                               print "</span></p></div>";
+                               $reply['content'] .= "</span></p></div>";
                        }
                }
 
 #                      print "</div>";
 #              }
 
-               print "]]></content>";
+               #print "]]></content>";
 
-               return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $vgroup_last_feed);
+               return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
+                       $vgroup_last_feed, $reply['content'], $reply['toolbar']);
        }
 
 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
 
                $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
 
                        return $headlines;
        }
 
-       function generate_dashboard_feed($link) {
-               print "<headlines id=\"-5\" is_cat=\"\">";
+       function generate_error_feed($link, $error) {
+               $reply = array();
 
-               print "<toolbar><![CDATA[]]></toolbar>";
+               $reply['headlines']['id'] = -6;
+               $reply['headlines']['is_cat'] = false;
 
-               print '<content><![CDATA[';
+               $reply['headlines']['toolbar'] = '';
+               $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
 
-               print "<div class='whiteBox'>".__('No feed selected.');
+               $reply['headlines-info'] = array("count" => 0,
+                       "vgroup_last_feed" => '',
+                       "unread" => 0,
+                       "disable_cache" => true);
+
+               return $reply;
+       }
 
-               print "<p class=\"small\"><span class=\"insensitive\">";
+
+       function generate_dashboard_feed($link) {
+               $reply = array();
+
+               $reply['headlines']['id'] = -5;
+               $reply['headlines']['is_cat'] = false;
+
+               $reply['headlines']['toolbar'] = '';
+               $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
+
+               $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
 
                $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
                        WHERE owner_uid = " . $_SESSION['uid']);
                $last_updated = db_fetch_result($result, 0, "last_updated");
                $last_updated = make_local_datetime($link, $last_updated, false);
 
-               printf(__("Feeds last updated at %s"), $last_updated);
+               $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
 
                $result = db_query($link, "SELECT COUNT(id) AS num_errors
                        FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
                $num_errors = db_fetch_result($result, 0, "num_errors");
 
                if ($num_errors > 0) {
-                       print "<br/>";
-                       print "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
+                       $reply['headlines']['content'] .= "<br/>";
+                       $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
                                __('Some feeds have update errors (click for details)')."</a>";
                }
-               print "</span></p>";
-
-               print "]]></content>";
-               print "</headlines>";
-
-               print "<headlines-info><![CDATA[";
+               $reply['headlines']['content'] .= "</span></p>";
 
-               $info = array("count" => 0,
+               $reply['headlines-info'] = array("count" => 0,
                        "vgroup_last_feed" => '',
                        "unread" => 0,
                        "disable_cache" => true);
 
-               print json_encode($info);
-
-               print "]]></headlines-info>";
-
+               return $reply;
        }
 
        function save_email_address($link, $email) {
 
        }
 
-       function print_article_enclosures($link, $id, $always_display_enclosures,
+       function format_article_enclosures($link, $id, $always_display_enclosures,
                                        $article_content) {
 
                $result = get_article_enclosures($link, $id);
+               $rv = '';
 
                if (count($result) > 0) {
 
                                array_push($entries, $entry);
                        }
 
-                       print "<div class=\"postEnclosures\">";
+                       $rv .= "<div class=\"postEnclosures\">";
 
                        if (!get_pref($link, "STRIP_IMAGES")) {
                                if ($always_display_enclosures ||
                                                if (preg_match("/image/", $entry["type"]) ||
                                                                preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
 
-                                                               print "<p><img
+                                                               $rv .= "<p><img
                                                                alt=\"".htmlspecialchars($entry["filename"])."\"
                                                                src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
                                                }
                        }
 
                        if (count($entries) == 1) {
-                               print __("Attachment:") . " ";
+                               $rv .= __("Attachment:") . " ";
                        } else {
-                               print __("Attachments:") . " ";
+                               $rv .= __("Attachments:") . " ";
                        }
 
-                       print join(", ", $entries_html);
+                       $rv .= join(", ", $entries_html);
 
-                       print "</div>";
+                       $rv .= "</div>";
                }
+
+               return $rv;
        }
 
        function getLastArticleId($link) {
 
                                $content = $tmhOAuth->response['response'];
 
+                               define('MAGPIE_CACHE_ON', false);
+
                                $rss = new MagpieRSS($content, MAGPIE_OUTPUT_ENCODING,
                                        MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );