]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
update.php & daemon: chdir to script directory using __FILE__ magic constant
[tt-rss.git] / functions.php
index 5adf46cdf2f868379b5fef7b8c30607ce2fd5c56..0a2345ff6b699315c319231cbc3e4eeac6882b8d 100644 (file)
 
        $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);
 
-       $config->set('HTML', 'Allowed', $allowed);
        $purifier = new HTMLPurifier($config);
 
        /**
                if (!$purge_unread) $query_limit = " unread = false AND ";
 
                if (DB_TYPE == "pgsql") {
-/*                     $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
-                               marked = false AND feed_id = '$feed_id' AND
-                               (SELECT date_updated FROM ttrss_entries WHERE
-                                       id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
-
                        $pg_version = get_pgsql_version($link);
 
                        if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
 
                global $memcache;
 
+               $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
+
                if (!$_REQUEST["daemon"] && !$ignore_daemon) {
                        return false;
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: start");
                }
 
                }
 
                if (db_num_rows($result) == 0) {
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED");
                        }
                        return false;
                else
                        $use_simplepie = false;
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update method: $update_method (feed setting: $update_method) (use simplepie: $use_simplepie)\n");
                }
 
 
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: fetching [$fetch_url]...");
                }
 
 
                if ($memcache && $obj = $memcache->get($obj_id)) {
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: data found in memcache.");
                        }
 
 
                                define('MAGPIE_CACHE_AGE', get_feed_update_interval($link, $feed) * 60);
                                define('MAGPIE_CACHE_ON', !$no_cache);
+                               define('MAGPIE_FETCH_TIME_OUT', 60);
+                               define('MAGPIE_CACHE_DIR', CACHE_DIR . "/magpie");
 
                                $rss = @fetch_rss($fetch_url);
                        } else {
-                               if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
-                                       mkdir(SIMPLEPIE_CACHE_DIR);
+                               $simplepie_cache_dir = CACHE_DIR . "/simplepie";
+
+                               if (!is_dir($simplepie_cache_dir)) {
+                                       mkdir($simplepie_cache_dir);
                                }
 
                                $rss = new SimplePie();
 
                                if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("enabling image cache");
                                        }
 
                                        $rss->set_image_handler("image.php", 'i');
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("feed update interval (sec): " .
                                                get_feed_update_interval($link, $feed)*60);
                                }
                                $rss->enable_cache(!$no_cache);
 
                                if (!$no_cache) {
-                                       $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
+                                       $rss->set_cache_location($simplepie_cache_dir);
                                        $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
                                }
 
 
 //             print_r($rss);
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: fetch done, parsing...");
                }
 
 
                if ($fetch_ok) {
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: processing feed data...");
                        }
 
                                $site_url = $rss->channel["link"];
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: checking favicon...");
                        }
 
                                        $feed_title = db_escape_string($rss->channel["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: registering title: $feed_title");
                                }
 
                                db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: loading filters...");
                        }
 
                        $filters = load_filters($link, $feed, $owner_uid);
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'] == 2) {
-                               print_r($filters);
-                       }
+//                     if ($debug_enabled) {
+//                             print_r($filters);
+//                     }
 
                        if ($use_simplepie) {
                                $iterator = $rss->get_items();
                                // clear any errors and mark feed as updated if fetched okay
                                // even if it's blank
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: entry iterator is not an array, no articles?");
                                }
 
                                return; // no articles
                        }
 
-                       if (PUBSUBHUBBUB_HUB && $pubsub_state != 2) {
+                       if ($pubsub_state != 2) {
 
                                $feed_hub_url = false;
                                if ($use_simplepie) {
                                        $links = $rss->get_links('hub');
 
-                                       foreach ($links as $l) {
-                                               $feed_hub_url = $l;
-                                               break;
+                                       if ($links && is_array($links)) {
+                                               foreach ($links as $l) {
+                                                       $feed_hub_url = $l;
+                                                       break;
+                                               }
                                        }
 
                                } else {
                                                        for ($i = 2; $i <= $atom['link#']; $i++) {
                                                                if ($atom["link#$i@rel"] == 'hub') {
                                                                        $feed_hub_url = $atom["link#$i@href"];
+                                                                       break;
                                                                }
                                                        }
                                                }
                                        }
                                }
 
-                               if ($feed_hub_url) {
+                               if ($feed_hub_url && function_exists('curl_init')) {
                                        $callback_url = get_self_url_prefix() .
                                                "/backend.php?op=pubsub&id=$feed";
 
                                }
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: processing articles...");
                        }
 
                                        if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: guid $entry_guid");
                                }
 
 
                                $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
                                }
 
                                        if (!$entry_link) $entry_link = $item["link"];
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: title $entry_title");
                                }
 
 
                                if (!$num_comments) $num_comments = 0;
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for tags [1]...");
                                }
 
                                                }
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: category tags:");
                                                print_r($additional_tags);
                                        }
                                        }
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for tags [2]...");
                                }
 
                                for ($i = 0; $i < count($entry_tags); $i++)
                                        $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: unfiltered tags found:");
                                        print_r($entry_tags);
                                }
                                $entry_content = sanitize_article_content($entry_content);
                                $entry_title = sanitize_article_content($entry_title);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
                                }
 
 
                                if (db_num_rows($result) == 0) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: base guid not found");
                                        }
 
 
                                if (db_num_rows($result) == 1) {
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: base guid found, checking for user record");
                                        }
 
                                                $entry_content, $entry_link, $entry_timestamp, $entry_author,
                                                $entry_tags);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: article filters: ");
                                                if (count($article_filters) != 0) {
                                                        print_r($article_filters);
 
                                        $score = calculate_article_score($article_filters);
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: initial score: $score");
                                        }
 
                                        // okay it doesn't exist - create user entry
                                        if (db_num_rows($result) == 0) {
 
-                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                               if ($debug_enabled) {
                                                        _debug("update_rss_feed: user record not found, creating...");
                                                }
 
                                                        VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
                                                                $last_read_qpart, $marked, $published, '$score', '', '')");
 
+                                               if (PUBSUBHUBBUB_HUB && $published == 'true') {
+                                                       $rss_link = get_self_url_prefix() .
+                                                               "/backend.php?op=rss&id=-2&key=" .
+                                                               get_feed_access_key($link, -2, false, $owner_uid);
+
+                                                       $p = new Publisher(PUBSUBHUBBUB_HUB);
+
+                                                       $pubsub_result = $p->publish_update($rss_link);
+                                               }
+
                                                $result = db_query($link,
                                                        "SELECT int_id FROM ttrss_user_entries WHERE
                                                                ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
                                                        $entry_int_id = db_fetch_result($result, 0, "int_id");
                                                }
                                        } else {
-                                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                               if ($debug_enabled) {
                                                        _debug("update_rss_feed: user record FOUND");
                                                }
 
                                                $entry_int_id = db_fetch_result($result, 0, "int_id");
                                        }
 
-                                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                                       if ($debug_enabled) {
                                                _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
                                        }
 
                                        $post_needs_update = false;
 
-                                       if ($update_on_checksum_change && $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 ($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");
                                                }
 
                                db_query($link, "COMMIT");
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: assigning labels...");
                                }
 
                                assign_article_to_labels($link, $entry_ref_id, $article_filters,
                                        $owner_uid);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for enclosures...");
                                }
 
                                }
 
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: article enclosures:");
                                        print_r($enclosures);
                                }
 
                                $filtered_tags = array_unique($filtered_tags);
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: filtered article tags:");
                                        print_r($filtered_tags);
                                }
                                        db_query($link, "COMMIT");
                                }
 
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: article processed");
                                }
                        }
 
                        if (!$last_updated) {
-                               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                               if ($debug_enabled) {
                                        _debug("update_rss_feed: new feed, catching it up...");
                                }
                                catchup_feed($link, $feed, false, $owner_uid);
                        }
 
-                       purge_feed($link, $feed, 0);
+                       if ($debug_enabled) {
+                               _debug("purging feed...");
+                       }
+
+                       purge_feed($link, $feed, 0, $debug_enabled);
 
                        db_query($link, "UPDATE ttrss_feeds
                                SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
                                $error_msg = mb_substr(magpie_error(), 0, 250);
                        }
 
-                       if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+                       if ($debug_enabled) {
                                _debug("update_rss_feed: error fetching feed: $error_msg");
                        }
 
                        unset($rss);
                }
 
-               if (defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']) {
+               if ($debug_enabled) {
                        _debug("update_rss_feed: done");
                }
 
                    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");
 
 
                $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];
 
+               $feed_self_url = get_self_url_prefix() .
+                       "/backend.php?op=rss&id=-2&key=" .
+                       get_feed_access_key($link, -2, false);
+
                if (!$feed_site_url) $feed_site_url = get_self_url_prefix();
 
-               print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
-                       <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
-                       <rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
-                                 xmlns:atom=\"http://www.w3.org/2005/Atom\">
-                       <channel>";
+               $tpl = new MiniTemplator;
+
+               $tpl->readTemplateFromFile("templates/generated_feed.txt");
+
+               $tpl->setVariable('FEED_TITLE', $feed_title);
+               $tpl->setVariable('VERSION', VERSION);
+               $tpl->setVariable('FEED_URL', htmlspecialchars($feed_self_url));
 
                if (PUBSUBHUBBUB_HUB && $feed == -2) {
-                       print "<atom:link rel='hub' href='".PUBSUBHUBBUB_HUB."'/>";
+                       $tpl->setVariable('HUB_URL', htmlspecialchars(PUBSUBHUBBUB_HUB));
+                       $tpl->addBlock('feed_hub');
                }
 
-               print "<title>$feed_title</title>
-                       <link>$feed_site_url</link>
-                       <description>Feed generated by Tiny Tiny RSS</description>";
+               $tpl->setVariable('SELF_URL', htmlspecialchars(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->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, '...'));
 
-                       $tags = get_article_tags($link, $line["id"], $owner_uid);
+                       $content = sanitize_rss($link, $line["content_preview"], false, $owner_uid);
 
-                       foreach ($tags as $tag) {
-                               print "<category>" . htmlspecialchars($tag) . "</category>";
+                       if ($line['note']) {
+                               $content = "<div style=\"$note_style\">" . $line['note'] . "</div>" .
+                                       $content;
                        }
 
-                       $rfc822_date = date('r', strtotime($line["updated"]));
+                       $tpl->setVariable('ARTICLE_CONTENT', $content);
 
-                       print "<pubDate>$rfc822_date</pubDate>";
+                       $tpl->setVariable('ARTICLE_UPDATED', date('c', strtotime($line["updated"])));
+                       $tpl->setVariable('ARTICLE_AUTHOR', htmlspecialchars($line['author']));
 
-                       if ($line["author"]) {
-                               print "<author>" . htmlspecialchars($line["author"]) . "</author>";
-                       }
-
-                       print "<title><![CDATA[" .
-                               htmlspecialchars($line["title"]) . "]]></title>";
-
-                       print "<description><![CDATA[";
+                       $tags = get_article_tags($link, $line["id"], $owner_uid);
 
-                       if ($line["note"]) {
-                               print "<div style='$note_style'>";
-                               print $line["note"];
-                               print "</div>";
+                       foreach ($tags as $tag) {
+                               $tpl->setVariable('ARTICLE_CATEGORY', htmlspecialchars($tag));
+                               $tpl->addBlock('category');
                        }
 
-                       print sanitize_rss($link, $line["content_preview"], false, $owner_uid);
-                       print "]]></description>";
-
                        $enclosures = get_article_enclosures($link, $line["id"]);
 
                        foreach ($enclosures as $e) {
                                $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) {
                        $search_q = "";
                }
 
-               // Adaptive doesn't really make any sense for generated feeds
-               // All Articles is the default, so no need to insert it either
-               if ($view_mode == "adaptive" || $view_mode == "all_articles")
-                       $view_mode = "";
-               else
-                       $view_mode = "&view-mode=$view_mode";
-
                $reply .= "
                        <a href=\"#\"
                                title=\"".__("View as RSS feed")."\"
 
                $tag = mb_strtolower($tag, 'utf-8');
 
-               $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
+               $tag = preg_replace('/[\'\"\+\>\<]/', "", $tag);
 
 //             $tag = str_replace('"', "", $tag);
 //             $tag = str_replace("+", " ", $tag);
 
        function get_self_url_prefix() {
 
-               $url_path = "";
+               /* $url_path = "";
 
                if ($_SERVER['HTTPS'] != "on") {
                        $url_path = "http://";
 
                $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
 
-               return $url_path;
+               return $url_path; */
+
+               return SELF_URL_PATH;
 
        }
        function opml_publish_url($link){
         * @return string The Mozilla Firefox feed adding URL.
         */
        function add_feed_url() {
-               $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' :  'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
-               $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
+               //$url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' :  'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+
+               $url_path = get_self_url_prefix() .
+                       "/backend.php?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
                return $url_path;
        } // function add_feed_url
 
        }
 
        function api_get_headlines($link, $feed_id, $limit, $offset,
-                                       $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order) {
+                               $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
+                               $include_attachments) {
 
                        /* do not rely on params below */
 
                                                "tags" => get_article_tags($link, $line["id"]),
                                        );
 
+                                       if ($include_attachments)
+                                               $headline_row['attachments'] = get_article_enclosures($link,
+                                                       $line['id']);
+
                                if ($show_excerpt) {
                                        $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
                                        $headline_row["excerpt"] = $excerpt;