]> git.wh0rd.org - tt-rss.git/blobdiff - functions.php
move HTMLPurifier cache inside CACHE_DIR/htmlpurifier
[tt-rss.git] / functions.php
index 069c85359acbf00545853ec30f4ac71718149597..4abca26c069345f24164fdd390137aead2f06004 100644 (file)
        $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('HTML', 'Allowed', $allowed);
        $config->set('Output.FlashCompat', true);
        $config->set('Attr.EnableID', true);
+       $config->set('Cache', 'SerializerPath', CACHE_DIR . "/htmlpurifier");
 
        $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?");
                                }
 
                                }
                        }
 
-                       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...");
                                                }
 
                                                        $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");
                                        }
 
 
                                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");
                }
 
 
                $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 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;