summaryrefslogtreecommitdiff
path: root/classes/rssutils.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/rssutils.php')
-rwxr-xr-xclasses/rssutils.php260
1 files changed, 196 insertions, 64 deletions
diff --git a/classes/rssutils.php b/classes/rssutils.php
index 68e0255ed..fe4c0a8a3 100755
--- a/classes/rssutils.php
+++ b/classes/rssutils.php
@@ -507,7 +507,7 @@ class RSSUtils {
Debug::log("loading filters & labels...", Debug::$LOG_VERBOSE);
- $filters = load_filters($feed, $owner_uid);
+ $filters = RSSUtils::load_filters($feed, $owner_uid);
if (Debug::get_loglevel() >= Debug::$LOG_EXTENDED) {
print_r($filters);
@@ -808,7 +808,7 @@ class RSSUtils {
Debug::log("force catchup: $entry_force_catchup", Debug::$LOG_VERBOSE);
- if ($cache_images && is_writable(CACHE_DIR . '/images'))
+ if ($cache_images)
RSSUtils::cache_media($entry_content, $site_url);
$csth = $pdo->prepare("SELECT id FROM ttrss_entries
@@ -871,7 +871,7 @@ class RSSUtils {
$entry_ref_id = $ref_id;
if (RSSUtils::find_article_filter($article_filters, "filter")) {
- Debug::log("article is filtered out, nothing to do.");
+ Debug::log("article is filtered out, nothing to do.", Debug::$LOG_VERBOSE);
$pdo->commit();
continue;
}
@@ -1033,7 +1033,7 @@ class RSSUtils {
}
}
- if ($cache_images && is_writable(CACHE_DIR . '/images'))
+ if ($cache_images)
RSSUtils::cache_enclosures($enclosures, $site_url);
if (Debug::get_loglevel() >= Debug::$LOG_EXTENDED) {
@@ -1071,7 +1071,7 @@ class RSSUtils {
$manual_tags = trim_array(explode(",", $f["param"]));
foreach ($manual_tags as $tag) {
- if (tag_is_valid($tag)) {
+ if (Article::tag_is_valid($tag)) {
array_push($entry_tags, $tag);
}
}
@@ -1115,9 +1115,9 @@ class RSSUtils {
foreach ($filtered_tags as $tag) {
- $tag = sanitize_tag($tag);
+ $tag = Article::sanitize_tag($tag);
- if (!tag_is_valid($tag)) continue;
+ if (!Article::tag_is_valid($tag)) continue;
$tsth->execute([$tag, $entry_int_id, $owner_uid]);
@@ -1147,7 +1147,7 @@ class RSSUtils {
Debug::log("purging feed...", Debug::$LOG_VERBOSE);
- purge_feed($feed, 0);
+ Feeds::purge_feed($feed, 0);
$sth = $pdo->prepare("UPDATE ttrss_feeds
SET last_updated = NOW(), last_unconditional = NOW(), last_error = '' WHERE id = ?");
@@ -1181,56 +1181,62 @@ class RSSUtils {
}
static function cache_enclosures($enclosures, $site_url) {
- foreach ($enclosures as $enc) {
+ $cache = new DiskCache("images");
- if (preg_match("/(image|audio|video)/", $enc[1])) {
+ if ($cache->isWritable()) {
+ foreach ($enclosures as $enc) {
- $src = rewrite_relative_url($site_url, $enc[0]);
+ if (preg_match("/(image|audio|video)/", $enc[1])) {
+ $src = rewrite_relative_url($site_url, $enc[0]);
- $local_filename = CACHE_DIR . "/images/" . sha1($src);
+ $local_filename = sha1($src);
- Debug::log("cache_enclosures: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ Debug::log("cache_enclosures: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
- if (!file_exists($local_filename)) {
- $file_content = fetch_file_contents($src);
+ if (!$cache->exists($local_filename)) {
+ $file_content = fetch_file_contents(array("url" => $src, "max_size" => MAX_CACHE_FILE_SIZE));
- if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
- file_put_contents($local_filename, $file_content);
+ if ($file_content) {
+ $cache->put($local_filename, $file_content);
+ }
+ } else if (is_writable($local_filename)) {
+ $cache->touch($local_filename);
}
- } else if (is_writable($local_filename)) {
- touch($local_filename);
}
}
}
}
static function cache_media($html, $site_url) {
- libxml_use_internal_errors(true);
+ $cache = new DiskCache("images");
- $doc = new DOMDocument();
- $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
- $xpath = new DOMXPath($doc);
+ if ($cache->isWritable()) {
+ $doc = new DOMDocument();
+ if ($doc->loadHTML($html)) {
+ $xpath = new DOMXPath($doc);
- $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])');
+ $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])');
- foreach ($entries as $entry) {
- if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
- $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
+ foreach ($entries as $entry) {
+ if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
+ $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
- $local_filename = CACHE_DIR . "/images/" . sha1($src);
+ $local_filename = sha1($src);
- Debug::log("cache_media: checking $src", Debug::$LOG_VERBOSE);
+ Debug::log("cache_media: checking $src", Debug::$LOG_VERBOSE);
- if (!file_exists($local_filename)) {
- Debug::log("cache_media: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ if (!$cache->exists($local_filename)) {
+ Debug::log("cache_media: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
- $file_content = fetch_file_contents($src);
+ $file_content = fetch_file_contents(array("url" => $src, "max_size" => MAX_CACHE_FILE_SIZE));
- if ($file_content && strlen($file_content) > MIN_CACHE_FILE_SIZE) {
- file_put_contents($local_filename, $file_content);
+ if ($file_content) {
+ $cache->put($local_filename, $file_content);
+ }
+ } else if ($cache->isWritable($local_filename)) {
+ $cache->touch($local_filename);
+ }
}
- } else if (is_writable($local_filename)) {
- touch($local_filename);
}
}
}
@@ -1285,32 +1291,6 @@ class RSSUtils {
Debug::log("Removed $num_deleted old lock files.");
}
- static function expire_cached_files() {
- foreach (array("feeds", "images", "export", "upload") as $dir) {
- $cache_dir = CACHE_DIR . "/$dir";
-
- Debug::log("Expiring $cache_dir", Debug::$LOG_VERBOSE);
-
- $num_deleted = 0;
-
- if (is_writable($cache_dir)) {
- $files = glob("$cache_dir/*");
-
- if ($files) {
- foreach ($files as $file) {
- if (time() - filemtime($file) > 86400*CACHE_MAX_DAYS) {
- unlink($file);
-
- ++$num_deleted;
- }
- }
- }
- }
-
- Debug::log("$cache_dir: removed $num_deleted files.");
- }
- }
-
/**
* Source: http://www.php.net/manual/en/function.parse-url.php#104527
* Returns the url query as associative array
@@ -1499,7 +1479,8 @@ class RSSUtils {
}
static function housekeeping_common() {
- RSSUtils::expire_cached_files();
+ DiskCache::expire();
+
RSSUtils::expire_lock_files();
RSSUtils::expire_error_log();
RSSUtils::expire_feed_archive();
@@ -1517,7 +1498,7 @@ class RSSUtils {
$icon_file = ICONS_DIR . "/$feed.ico";
if (!file_exists($icon_file)) {
- $favicon_url = get_favicon_url($site_url);
+ $favicon_url = RSSUtils::get_favicon_url($site_url);
if ($favicon_url) {
// Limiting to "image" type misses those served with text/plain
@@ -1570,4 +1551,155 @@ class RSSUtils {
return mb_strpos($feed_data, "\x1f" . "\x8b" . "\x08", 0, "US-ASCII") === 0;
}
+ static function load_filters($feed_id, $owner_uid) {
+ $filters = array();
+
+ $feed_id = (int) $feed_id;
+ $cat_id = (int)Feeds::getFeedCategory($feed_id);
+
+ if ($cat_id == 0)
+ $null_cat_qpart = "cat_id IS NULL OR";
+ else
+ $null_cat_qpart = "";
+
+ $pdo = Db::pdo();
+
+ $sth = $pdo->prepare("SELECT * FROM ttrss_filters2 WHERE
+ owner_uid = ? AND enabled = true ORDER BY order_id, title");
+ $sth->execute([$owner_uid]);
+
+ $check_cats = array_merge(
+ Feeds::getParentCategories($cat_id, $owner_uid),
+ [$cat_id]);
+
+ $check_cats_str = join(",", $check_cats);
+ $check_cats_fullids = array_map(function($a) { return "CAT:$a"; }, $check_cats);
+
+ while ($line = $sth->fetch()) {
+ $filter_id = $line["id"];
+
+ $match_any_rule = sql_bool_to_bool($line["match_any_rule"]);
+
+ $sth2 = $pdo->prepare("SELECT
+ r.reg_exp, r.inverse, r.feed_id, r.cat_id, r.cat_filter, r.match_on, t.name AS type_name
+ FROM ttrss_filters2_rules AS r,
+ ttrss_filter_types AS t
+ WHERE
+ (match_on IS NOT NULL OR
+ (($null_cat_qpart (cat_id IS NULL AND cat_filter = false) OR cat_id IN ($check_cats_str)) AND
+ (feed_id IS NULL OR feed_id = ?))) AND
+ filter_type = t.id AND filter_id = ?");
+ $sth2->execute([$feed_id, $filter_id]);
+
+ $rules = array();
+ $actions = array();
+
+ while ($rule_line = $sth2->fetch()) {
+ # print_r($rule_line);
+
+ if ($rule_line["match_on"]) {
+ $match_on = json_decode($rule_line["match_on"], true);
+
+ if (in_array("0", $match_on) || in_array($feed_id, $match_on) || count(array_intersect($check_cats_fullids, $match_on)) > 0) {
+
+ $rule = array();
+ $rule["reg_exp"] = $rule_line["reg_exp"];
+ $rule["type"] = $rule_line["type_name"];
+ $rule["inverse"] = sql_bool_to_bool($rule_line["inverse"]);
+
+ array_push($rules, $rule);
+ } else if (!$match_any_rule) {
+ // this filter contains a rule that doesn't match to this feed/category combination
+ // thus filter has to be rejected
+
+ $rules = [];
+ break;
+ }
+
+ } else {
+
+ $rule = array();
+ $rule["reg_exp"] = $rule_line["reg_exp"];
+ $rule["type"] = $rule_line["type_name"];
+ $rule["inverse"] = sql_bool_to_bool($rule_line["inverse"]);
+
+ array_push($rules, $rule);
+ }
+ }
+
+ if (count($rules) > 0) {
+ $sth2 = $pdo->prepare("SELECT a.action_param,t.name AS type_name
+ FROM ttrss_filters2_actions AS a,
+ ttrss_filter_actions AS t
+ WHERE
+ action_id = t.id AND filter_id = ?");
+ $sth2->execute([$filter_id]);
+
+ while ($action_line = $sth2->fetch()) {
+ # print_r($action_line);
+
+ $action = array();
+ $action["type"] = $action_line["type_name"];
+ $action["param"] = $action_line["action_param"];
+
+ array_push($actions, $action);
+ }
+ }
+
+ $filter = [];
+ $filter["id"] = $filter_id;
+ $filter["match_any_rule"] = sql_bool_to_bool($line["match_any_rule"]);
+ $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
+ $filter["rules"] = $rules;
+ $filter["actions"] = $actions;
+
+ if (count($rules) > 0 && count($actions) > 0) {
+ array_push($filters, $filter);
+ }
+ }
+
+ return $filters;
+ }
+
+ /**
+ * Try to determine the favicon URL for a feed.
+ * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
+ * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
+ *
+ * @param string $url A feed or page URL
+ * @access public
+ * @return mixed The favicon URL, or false if none was found.
+ */
+ static function get_favicon_url($url) {
+
+ $favicon_url = false;
+
+ if ($html = @fetch_file_contents($url)) {
+
+ $doc = new DOMDocument();
+ if ($doc->loadHTML($html)) {
+ $xpath = new DOMXPath($doc);
+
+ $base = $xpath->query('/html/head/base[@href]');
+ foreach ($base as $b) {
+ $url = rewrite_relative_url($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"));
+ break;
+ }
+ }
+ }
+ }
+
+ if (!$favicon_url)
+ $favicon_url = rewrite_relative_url($url, "/favicon.ico");
+
+ return $favicon_url;
+ }
+
}