]>
Commit | Line | Data |
---|---|---|
2c08214a | 1 | <?php |
e6c886bf AD |
2 | class RSSUtils { |
3 | static function calculate_article_hash($article, $pluginhost) { | |
af244f92 AD |
4 | $tmp = ""; |
5 | ||
6 | foreach ($article as $k => $v) { | |
7 | if ($k != "feed" && isset($v)) { | |
24e6ff5d AD |
8 | $x = strip_tags(is_array($v) ? implode(",", $v) : $v); |
9 | ||
10 | //_debug("$k:" . sha1($x) . ":" . htmlspecialchars($x), true); | |
11 | ||
12 | $tmp .= sha1("$k:" . sha1($x)); | |
af244f92 AD |
13 | } |
14 | } | |
15 | ||
eb16bd9f | 16 | return sha1(implode(",", $pluginhost->get_plugin_names()) . $tmp); |
b1840673 AD |
17 | } |
18 | ||
e6c886bf | 19 | static function update_feedbrowser_cache() { |
79178062 | 20 | |
a42c55f0 | 21 | $result = db_query("SELECT feed_url, site_url, title, COUNT(id) AS subscribers |
45378752 LD |
22 | FROM ttrss_feeds WHERE feed_url NOT IN (SELECT feed_url FROM ttrss_feeds |
23 | WHERE private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%') | |
79178062 AD |
24 | GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000"); |
25 | ||
a42c55f0 | 26 | db_query("BEGIN"); |
79178062 | 27 | |
a42c55f0 | 28 | db_query("DELETE FROM ttrss_feedbrowser_cache"); |
79178062 AD |
29 | |
30 | $count = 0; | |
31 | ||
32 | while ($line = db_fetch_assoc($result)) { | |
a42c55f0 AD |
33 | $subscribers = db_escape_string($line["subscribers"]); |
34 | $feed_url = db_escape_string($line["feed_url"]); | |
35 | $title = db_escape_string($line["title"]); | |
36 | $site_url = db_escape_string($line["site_url"]); | |
79178062 | 37 | |
a42c55f0 | 38 | $tmp_result = db_query("SELECT subscribers FROM |
79178062 AD |
39 | ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'"); |
40 | ||
41 | if (db_num_rows($tmp_result) == 0) { | |
42 | ||
a42c55f0 | 43 | db_query("INSERT INTO ttrss_feedbrowser_cache |
79178062 AD |
44 | (feed_url, site_url, title, subscribers) VALUES ('$feed_url', |
45 | '$site_url', '$title', '$subscribers')"); | |
46 | ||
47 | ++$count; | |
48 | ||
49 | } | |
50 | ||
51 | } | |
52 | ||
a42c55f0 | 53 | db_query("COMMIT"); |
79178062 AD |
54 | |
55 | return $count; | |
56 | ||
57 | } | |
58 | ||
e6c886bf | 59 | static function update_daemon_common($limit = DAEMON_FEED_LIMIT, $debug = true) { |
2c08214a AD |
60 | // Process all other feeds using last_updated and interval parameters |
61 | ||
6322ac79 | 62 | $schema_version = get_schema_version(); |
857efe49 AD |
63 | |
64 | if ($schema_version != SCHEMA_VERSION) { | |
65 | die("Schema version is wrong, please upgrade the database.\n"); | |
66 | } | |
67 | ||
61c1812f AD |
68 | define('PREFS_NO_CACHE', true); |
69 | ||
2c08214a | 70 | // Test if the user has loggued in recently. If not, it does not update its feeds. |
09e8bdfd | 71 | if (!SINGLE_USER_MODE && DAEMON_UPDATE_LOGIN_LIMIT > 0) { |
2c08214a AD |
72 | if (DB_TYPE == "pgsql") { |
73 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
74 | } else { | |
75 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
76 | } | |
77 | } else { | |
78 | $login_thresh_qpart = ""; | |
79 | } | |
80 | ||
537eca40 | 81 | // Test if the feed need a update (update interval exceeded). |
2c08214a AD |
82 | if (DB_TYPE == "pgsql") { |
83 | $update_limit_qpart = "AND (( | |
84 | ttrss_feeds.update_interval = 0 | |
ee0542ce | 85 | AND ttrss_user_prefs.value != '-1' |
2c08214a AD |
86 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) |
87 | ) OR ( | |
88 | ttrss_feeds.update_interval > 0 | |
89 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
f08426e3 AD |
90 | ) OR (ttrss_feeds.last_updated IS NULL |
91 | AND ttrss_user_prefs.value != '-1') | |
92 | OR (last_updated = '1970-01-01 00:00:00' | |
93 | AND ttrss_user_prefs.value != '-1'))"; | |
2c08214a AD |
94 | } else { |
95 | $update_limit_qpart = "AND (( | |
96 | ttrss_feeds.update_interval = 0 | |
ee0542ce | 97 | AND ttrss_user_prefs.value != '-1' |
2c08214a AD |
98 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) |
99 | ) OR ( | |
100 | ttrss_feeds.update_interval > 0 | |
101 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
f08426e3 AD |
102 | ) OR (ttrss_feeds.last_updated IS NULL |
103 | AND ttrss_user_prefs.value != '-1') | |
104 | OR (last_updated = '1970-01-01 00:00:00' | |
105 | AND ttrss_user_prefs.value != '-1'))"; | |
2c08214a AD |
106 | } |
107 | ||
108 | // Test if feed is currently being updated by another process. | |
109 | if (DB_TYPE == "pgsql") { | |
566417c4 | 110 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '10 minutes')"; |
2c08214a | 111 | } else { |
566417c4 | 112 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 10 MINUTE))"; |
2c08214a AD |
113 | } |
114 | ||
115 | // Test if there is a limit to number of updated feeds | |
116 | $query_limit = ""; | |
117 | if($limit) $query_limit = sprintf("LIMIT %d", $limit); | |
118 | ||
98070db0 TK |
119 | // Update the least recently updated feeds first |
120 | $query_order = "ORDER BY last_updated"; | |
121 | if (DB_TYPE == "pgsql") $query_order .= " NULLS FIRST"; | |
122 | ||
fce451a4 | 123 | $query = "SELECT DISTINCT ttrss_feeds.feed_url, ttrss_feeds.last_updated |
2c08214a AD |
124 | FROM |
125 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
f4ae0f05 | 126 | WHERE |
2c08214a | 127 | ttrss_feeds.owner_uid = ttrss_users.id |
f08426e3 | 128 | AND ttrss_user_prefs.profile IS NULL |
2c08214a AD |
129 | AND ttrss_users.id = ttrss_user_prefs.owner_uid |
130 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
131 | $login_thresh_qpart $update_limit_qpart | |
1c4421fc | 132 | $updstart_thresh_qpart |
98070db0 | 133 | $query_order $query_limit"; |
fce451a4 AD |
134 | |
135 | // We search for feed needing update. | |
136 | $result = db_query($query); | |
2c08214a | 137 | |
ca5ff2d9 | 138 | if($debug) _debug(sprintf("Scheduled %d feeds to update...", db_num_rows($result))); |
2c08214a AD |
139 | |
140 | // Here is a little cache magic in order to minimize risk of double feed updates. | |
141 | $feeds_to_update = array(); | |
142 | while ($line = db_fetch_assoc($result)) { | |
a42c55f0 | 143 | array_push($feeds_to_update, db_escape_string($line['feed_url'])); |
2c08214a AD |
144 | } |
145 | ||
146 | // We update the feed last update started date before anything else. | |
147 | // There is no lag due to feed contents downloads | |
148 | // It prevent an other process to update the same feed. | |
1c4421fc AD |
149 | |
150 | if(count($feeds_to_update) > 0) { | |
151 | $feeds_quoted = array(); | |
152 | ||
153 | foreach ($feeds_to_update as $feed) { | |
a42c55f0 | 154 | array_push($feeds_quoted, "'" . db_escape_string($feed) . "'"); |
1c4421fc AD |
155 | } |
156 | ||
a42c55f0 | 157 | db_query(sprintf("UPDATE ttrss_feeds SET last_update_started = NOW() |
1c4421fc | 158 | WHERE feed_url IN (%s)", implode(',', $feeds_quoted))); |
2c08214a AD |
159 | } |
160 | ||
8292d05b | 161 | $nf = 0; |
2d9c5684 | 162 | $bstarted = microtime(true); |
8292d05b | 163 | |
5cbd1fe8 AD |
164 | $batch_owners = array(); |
165 | ||
2c08214a | 166 | // For each feed, we call the feed update function. |
1c4421fc AD |
167 | foreach ($feeds_to_update as $feed) { |
168 | if($debug) _debug("Base feed: $feed"); | |
2c08214a | 169 | |
a42c55f0 | 170 | //update_rss_feed($line["id"], true); |
2c08214a | 171 | |
1c4421fc AD |
172 | // since we have the data cached, we can deal with other feeds with the same url |
173 | ||
15c762be | 174 | $tmp_result = db_query("SELECT DISTINCT ttrss_feeds.id,last_updated,ttrss_feeds.owner_uid |
ee0542ce AD |
175 | FROM ttrss_feeds, ttrss_users, ttrss_user_prefs WHERE |
176 | ttrss_user_prefs.owner_uid = ttrss_feeds.owner_uid AND | |
177 | ttrss_users.id = ttrss_user_prefs.owner_uid AND | |
178 | ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' AND | |
f08426e3 | 179 | ttrss_user_prefs.profile IS NULL AND |
9e84bab4 AD |
180 | feed_url = '".db_escape_string($feed)."' |
181 | $update_limit_qpart | |
1c4421fc | 182 | $login_thresh_qpart |
5929a0c1 | 183 | ORDER BY ttrss_feeds.id $query_limit"); |
1c4421fc AD |
184 | |
185 | if (db_num_rows($tmp_result) > 0) { | |
186 | while ($tline = db_fetch_assoc($tmp_result)) { | |
15c762be | 187 | if($debug) _debug(" => " . $tline["last_updated"] . ", " . $tline["id"] . " " . $tline["owner_uid"]); |
f08426e3 | 188 | |
5cbd1fe8 AD |
189 | if (array_search($tline["owner_uid"], $batch_owners) === FALSE) |
190 | array_push($batch_owners, $tline["owner_uid"]); | |
191 | ||
2d9c5684 | 192 | $fstarted = microtime(true); |
e6c886bf | 193 | RSSUtils::update_rss_feed($tline["id"], true, false); |
4f71d743 | 194 | _debug_suppress(false); |
2d9c5684 AD |
195 | |
196 | _debug(sprintf(" %.4f (sec)", microtime(true) - $fstarted)); | |
197 | ||
8292d05b | 198 | ++$nf; |
1c4421fc AD |
199 | } |
200 | } | |
2c08214a AD |
201 | } |
202 | ||
2d9c5684 AD |
203 | if ($nf > 0) { |
204 | _debug(sprintf("Processed %d feeds in %.4f (sec), %.4f (sec/feed avg)", $nf, | |
205 | microtime(true) - $bstarted, (microtime(true) - $bstarted) / $nf)); | |
206 | } | |
207 | ||
5cbd1fe8 AD |
208 | foreach ($batch_owners as $owner_uid) { |
209 | _debug("Running housekeeping tasks for user $owner_uid..."); | |
210 | ||
e6c886bf | 211 | RSSUtils::housekeeping_user($owner_uid); |
5cbd1fe8 AD |
212 | } |
213 | ||
2c08214a | 214 | // Send feed digests by email if needed. |
c2f0f24e | 215 | Digest::send_headlines_digests($debug); |
2c08214a | 216 | |
8292d05b AD |
217 | return $nf; |
218 | ||
7b55001e | 219 | } |
2c08214a | 220 | |
6022776d | 221 | // this is used when subscribing |
e6c886bf | 222 | static function set_basic_feed_info($feed) { |
6022776d AD |
223 | |
224 | $feed = db_escape_string($feed); | |
225 | ||
9bd422c2 | 226 | $result = db_query("SELECT feed_url,auth_pass,auth_login,auth_pass_encrypted |
6022776d AD |
227 | FROM ttrss_feeds WHERE id = '$feed'"); |
228 | ||
229 | $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result, | |
230 | 0, "auth_pass_encrypted")); | |
231 | ||
232 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
233 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
234 | ||
17a8e61d | 235 | if ($auth_pass_encrypted && function_exists("mcrypt_decrypt")) { |
6022776d AD |
236 | require_once "crypt.php"; |
237 | $auth_pass = decrypt_string($auth_pass); | |
238 | } | |
239 | ||
240 | $fetch_url = db_fetch_result($result, 0, "feed_url"); | |
241 | ||
242 | $feed_data = fetch_file_contents($fetch_url, false, | |
243 | $auth_login, $auth_pass, false, | |
3d5d2890 | 244 | FEED_FETCH_TIMEOUT, |
6022776d AD |
245 | 0); |
246 | ||
247 | global $fetch_curl_used; | |
248 | ||
249 | if (!$fetch_curl_used) { | |
250 | $tmp = @gzdecode($feed_data); | |
251 | ||
252 | if ($tmp) $feed_data = $tmp; | |
253 | } | |
254 | ||
255 | $feed_data = trim($feed_data); | |
256 | ||
257 | $rss = new FeedParser($feed_data); | |
258 | $rss->init(); | |
259 | ||
260 | if (!$rss->error()) { | |
261 | ||
262 | $result = db_query("SELECT title, site_url FROM ttrss_feeds WHERE id = '$feed'"); | |
263 | ||
264 | $registered_title = db_fetch_result($result, 0, "title"); | |
265 | $orig_site_url = db_fetch_result($result, 0, "site_url"); | |
266 | ||
267 | $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245)); | |
268 | $feed_title = db_escape_string(mb_substr($rss->get_title(), 0, 199)); | |
269 | ||
270 | if ($feed_title && (!$registered_title || $registered_title == "[Unknown]")) { | |
271 | db_query("UPDATE ttrss_feeds SET | |
272 | title = '$feed_title' WHERE id = '$feed'"); | |
273 | } | |
274 | ||
275 | if ($site_url && $orig_site_url != $site_url) { | |
276 | db_query("UPDATE ttrss_feeds SET | |
277 | site_url = '$site_url' WHERE id = '$feed'"); | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
7b55001e | 282 | /** |
e6c886bf AD |
283 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
284 | */ | |
285 | static function update_rss_feed($feed, $no_cache = false) { | |
2c08214a | 286 | |
2c08214a AD |
287 | $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']; |
288 | ||
4f71d743 | 289 | _debug_suppress(!$debug_enabled); |
68cccafc | 290 | _debug("start", $debug_enabled); |
2c08214a | 291 | |
6bb96beb AD |
292 | $result = db_query("SELECT title FROM ttrss_feeds |
293 | WHERE id = '$feed'"); | |
bfe1eb4e AD |
294 | |
295 | if (db_num_rows($result) == 0) { | |
296 | _debug("feed $feed NOT FOUND/SKIPPED", $debug_enabled); | |
297 | user_error("Attempt to update unknown/invalid feed $feed", E_USER_WARNING); | |
298 | return false; | |
299 | } | |
300 | ||
6bb96beb AD |
301 | $title = db_fetch_result($result, 0, "title"); |
302 | ||
303 | // feed was batch-subscribed or something, we need to get basic info | |
304 | // this is not optimal currently as it fetches stuff separately TODO: optimize | |
305 | if ($title == "[Unknown]") { | |
306 | _debug("setting basic feed info for $feed..."); | |
e6c886bf | 307 | RSSUtils::set_basic_feed_info($feed); |
6bb96beb AD |
308 | } |
309 | ||
a42c55f0 | 310 | $result = db_query("SELECT id,update_interval,auth_login, |
5ba1ddd4 | 311 | feed_url,auth_pass,cache_images, |
5321e775 | 312 | mark_unread_on_update, owner_uid, |
90e5f4f1 | 313 | pubsub_state, auth_pass_encrypted, |
0edf1d0d | 314 | feed_language |
87764a50 | 315 | FROM ttrss_feeds WHERE id = '$feed'"); |
2c08214a | 316 | |
2c08214a AD |
317 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
318 | $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result, | |
319 | 0, "mark_unread_on_update")); | |
2c08214a | 320 | $pubsub_state = db_fetch_result($result, 0, "pubsub_state"); |
044cff2d AD |
321 | $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result, |
322 | 0, "auth_pass_encrypted")); | |
2c08214a | 323 | |
a42c55f0 | 324 | db_query("UPDATE ttrss_feeds SET last_update_started = NOW() |
2c08214a AD |
325 | WHERE id = '$feed'"); |
326 | ||
327 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
328 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
329 | ||
17a8e61d | 330 | if ($auth_pass_encrypted && function_exists("mcrypt_decrypt")) { |
044cff2d AD |
331 | require_once "crypt.php"; |
332 | $auth_pass = decrypt_string($auth_pass); | |
333 | } | |
334 | ||
2c08214a AD |
335 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
336 | $fetch_url = db_fetch_result($result, 0, "feed_url"); | |
df659891 AD |
337 | $feed_language = db_escape_string(mb_strtolower(db_fetch_result($result, 0, "feed_language"))); |
338 | if (!$feed_language) $feed_language = 'english'; | |
2c08214a | 339 | |
a42c55f0 | 340 | $feed = db_escape_string($feed); |
2c08214a | 341 | |
f074ffe9 | 342 | $date_feed_processed = date('Y-m-d H:i'); |
2c08214a | 343 | |
865a3ed6 | 344 | $cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".xml"; |
f074ffe9 | 345 | |
ee65bef4 AD |
346 | $pluginhost = new PluginHost(); |
347 | $pluginhost->set_debug($debug_enabled); | |
348 | $user_plugins = get_pref("_ENABLED_PLUGINS", $owner_uid); | |
349 | ||
350 | $pluginhost->load(PLUGINS, PluginHost::KIND_ALL); | |
351 | $pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid); | |
352 | $pluginhost->load_data(); | |
353 | ||
7b55001e | 354 | $rss_hash = false; |
4f9cbdff | 355 | |
7b55001e AD |
356 | $force_refetch = isset($_REQUEST["force_refetch"]); |
357 | $feed_data = ""; | |
687a4f59 | 358 | |
7b55001e AD |
359 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_FETCH_FEED) as $plugin) { |
360 | $feed_data = $plugin->hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed, 0, $auth_login, $auth_pass); | |
361 | } | |
2c08214a | 362 | |
7b55001e AD |
363 | // try cache |
364 | if (!$feed_data && | |
365 | file_exists($cache_filename) && | |
366 | is_readable($cache_filename) && | |
367 | !$auth_login && !$auth_pass && | |
368 | filemtime($cache_filename) > time() - 30) { | |
be574731 | 369 | |
7b55001e | 370 | _debug("using local cache [$cache_filename].", $debug_enabled); |
52637d3b | 371 | |
7b55001e | 372 | @$feed_data = file_get_contents($cache_filename); |
f074ffe9 | 373 | |
7b55001e AD |
374 | if ($feed_data) { |
375 | $rss_hash = sha1($feed_data); | |
88edaa93 | 376 | } |
ee65bef4 | 377 | |
7b55001e AD |
378 | } else { |
379 | _debug("local cache will not be used for this feed", $debug_enabled); | |
380 | } | |
312742db | 381 | |
7b55001e AD |
382 | // fetch feed from source |
383 | if (!$feed_data) { | |
384 | _debug("fetching [$fetch_url]...", $debug_enabled); | |
312742db | 385 | |
7b55001e AD |
386 | if (ini_get("open_basedir") && function_exists("curl_init")) { |
387 | _debug("not using CURL due to open_basedir restrictions"); | |
388 | } | |
3f6f0857 | 389 | |
7b55001e AD |
390 | $feed_data = fetch_file_contents($fetch_url, false, |
391 | $auth_login, $auth_pass, false, | |
392 | $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT, | |
393 | 0); | |
3f6f0857 | 394 | |
7b55001e | 395 | global $fetch_curl_used; |
3f6f0857 | 396 | |
7b55001e AD |
397 | if (!$fetch_curl_used) { |
398 | $tmp = @gzdecode($feed_data); | |
1367bc3f | 399 | |
7b55001e AD |
400 | if ($tmp) $feed_data = $tmp; |
401 | } | |
017401dd | 402 | |
7b55001e | 403 | $feed_data = trim($feed_data); |
fd687300 | 404 | |
7b55001e | 405 | _debug("fetch done.", $debug_enabled); |
95beaa14 | 406 | |
7b55001e AD |
407 | // cache vanilla feed data for re-use |
408 | if ($feed_data && !$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) { | |
409 | $new_rss_hash = sha1($feed_data); | |
410 | ||
411 | if ($new_rss_hash != $rss_hash) { | |
412 | _debug("saving $cache_filename", $debug_enabled); | |
413 | @file_put_contents($cache_filename, $feed_data); | |
95beaa14 | 414 | } |
4f9cbdff | 415 | } |
7b55001e | 416 | } |
017401dd | 417 | |
7b55001e AD |
418 | if (!$feed_data) { |
419 | global $fetch_last_error; | |
420 | global $fetch_last_error_code; | |
f074ffe9 | 421 | |
7b55001e | 422 | _debug("unable to fetch: $fetch_last_error [$fetch_last_error_code]", $debug_enabled); |
f074ffe9 | 423 | |
7b55001e | 424 | $error_escaped = ''; |
7a01dc77 | 425 | |
7b55001e AD |
426 | // If-Modified-Since |
427 | if ($fetch_last_error_code != 304) { | |
428 | $error_escaped = db_escape_string($fetch_last_error); | |
429 | } else { | |
430 | _debug("source claims data not modified, nothing to do.", $debug_enabled); | |
431 | } | |
4f9cbdff | 432 | |
7b55001e AD |
433 | db_query( |
434 | "UPDATE ttrss_feeds SET last_error = '$error_escaped', | |
435 | last_updated = NOW() WHERE id = '$feed'"); | |
4f9cbdff | 436 | |
7b55001e | 437 | return; |
f074ffe9 AD |
438 | } |
439 | ||
1ffe3391 | 440 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_FETCHED) as $plugin) { |
6791af0c | 441 | $feed_data = $plugin->hook_feed_fetched($feed_data, $fetch_url, $owner_uid, $feed); |
017401dd AD |
442 | } |
443 | ||
07d3431e AD |
444 | $rss = new FeedParser($feed_data); |
445 | $rss->init(); | |
2c08214a | 446 | |
a42c55f0 | 447 | $feed = db_escape_string($feed); |
2c08214a | 448 | |
19b3992b | 449 | if (!$rss->error()) { |
2c08214a | 450 | |
d2a421e3 | 451 | // We use local pluginhost here because we need to load different per-user feed plugins |
1ffe3391 | 452 | $pluginhost->run_hooks(PluginHost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); |
4412b877 | 453 | |
df659891 | 454 | _debug("language: $feed_language", $debug_enabled); |
68cccafc | 455 | _debug("processing feed data...", $debug_enabled); |
2c08214a | 456 | |
a42c55f0 | 457 | // db_query("BEGIN"); |
2c08214a | 458 | |
382268c6 AD |
459 | if (DB_TYPE == "pgsql") { |
460 | $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'"; | |
461 | } else { | |
462 | $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)"; | |
463 | } | |
464 | ||
fe4535e6 | 465 | $result = db_query("SELECT owner_uid,favicon_avg_color, |
382268c6 AD |
466 | (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS |
467 | favicon_needs_check | |
2c08214a AD |
468 | FROM ttrss_feeds WHERE id = '$feed'"); |
469 | ||
382268c6 AD |
470 | $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0, |
471 | "favicon_needs_check")); | |
36490f11 | 472 | $favicon_avg_color = db_fetch_result($result, 0, "favicon_avg_color"); |
2c08214a AD |
473 | |
474 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
475 | ||
a42c55f0 | 476 | $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245)); |
2c08214a | 477 | |
cd07592c AD |
478 | _debug("site_url: $site_url", $debug_enabled); |
479 | _debug("feed_title: " . $rss->get_title(), $debug_enabled); | |
480 | ||
687a4f59 | 481 | if ($favicon_needs_check || $force_refetch) { |
36490f11 AD |
482 | |
483 | /* terrible hack: if we crash on floicon shit here, we won't check | |
560cbd8c | 484 | * the icon avgcolor again (unless the icon got updated) */ |
36490f11 | 485 | |
560cbd8c AD |
486 | $favicon_file = ICONS_DIR . "/$feed.ico"; |
487 | $favicon_modified = @filemtime($favicon_file); | |
488 | ||
68cccafc | 489 | _debug("checking favicon...", $debug_enabled); |
687a4f59 | 490 | |
e6c886bf | 491 | RSSUtils::check_feed_favicon($site_url, $feed); |
560cbd8c AD |
492 | $favicon_modified_new = @filemtime($favicon_file); |
493 | ||
494 | if ($favicon_modified_new > $favicon_modified) | |
495 | $favicon_avg_color = ''; | |
687a4f59 | 496 | |
6ee0d4b0 | 497 | if (file_exists($favicon_file) && function_exists("imagecreatefromstring") && $favicon_avg_color == '') { |
e6c886bf | 498 | require_once "colors.php"; |
687a4f59 | 499 | |
e6c886bf | 500 | db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE |
aafd55ba AD |
501 | id = '$feed'"); |
502 | ||
e6c886bf AD |
503 | $favicon_color = db_escape_string( |
504 | calculate_avg_color($favicon_file)); | |
63c323f7 | 505 | |
e6c886bf | 506 | $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'"; |
36490f11 | 507 | } else if ($favicon_avg_color == 'fail') { |
84ceb6bd | 508 | _debug("floicon failed on this file, not trying to recalculate avg color", $debug_enabled); |
6ac722d5 | 509 | } |
687a4f59 | 510 | |
a42c55f0 | 511 | db_query("UPDATE ttrss_feeds SET favicon_last_checked = NOW() |
bc7a144d | 512 | $favicon_colorstring |
f2798eb6 AD |
513 | WHERE id = '$feed'"); |
514 | } | |
2c08214a | 515 | |
68cccafc | 516 | _debug("loading filters & labels...", $debug_enabled); |
2c08214a | 517 | |
a42c55f0 | 518 | $filters = load_filters($feed, $owner_uid); |
2c08214a | 519 | |
68cccafc | 520 | _debug("" . count($filters) . " filters loaded.", $debug_enabled); |
2c08214a | 521 | |
19b3992b | 522 | $items = $rss->get_items(); |
2c08214a | 523 | |
19b3992b | 524 | if (!is_array($items)) { |
68cccafc | 525 | _debug("no articles found.", $debug_enabled); |
2c08214a | 526 | |
a42c55f0 | 527 | db_query("UPDATE ttrss_feeds |
2c08214a AD |
528 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
529 | ||
530 | return; // no articles | |
531 | } | |
532 | ||
533 | if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) { | |
534 | ||
68cccafc | 535 | _debug("checking for PUSH hub...", $debug_enabled); |
2c08214a AD |
536 | |
537 | $feed_hub_url = false; | |
2c08214a | 538 | |
19b3992b | 539 | $links = $rss->get_links('hub'); |
2c08214a | 540 | |
19b3992b AD |
541 | if ($links && is_array($links)) { |
542 | foreach ($links as $l) { | |
543 | $feed_hub_url = $l; | |
544 | break; | |
2c08214a AD |
545 | } |
546 | } | |
547 | ||
68cccafc | 548 | _debug("feed hub url: $feed_hub_url", $debug_enabled); |
2c08214a | 549 | |
d8179cb9 AD |
550 | $feed_self_url = $fetch_url; |
551 | ||
552 | $links = $rss->get_links('self'); | |
553 | ||
554 | if ($links && is_array($links)) { | |
555 | foreach ($links as $l) { | |
556 | $feed_self_url = $l; | |
557 | break; | |
558 | } | |
559 | } | |
560 | ||
561 | _debug("feed self url = $feed_self_url"); | |
562 | ||
563 | if ($feed_hub_url && $feed_self_url && function_exists('curl_init') && | |
2c08214a AD |
564 | !ini_get("open_basedir")) { |
565 | ||
becd215a | 566 | require_once 'lib/pubsubhubbub/Subscriber.php'; |
2c08214a AD |
567 | |
568 | $callback_url = get_self_url_prefix() . | |
569 | "/public.php?op=pubsub&id=$feed"; | |
570 | ||
cb3f8773 | 571 | $s = new Pubsubhubbub\Subscriber\Subscriber($feed_hub_url, $callback_url); |
2c08214a | 572 | |
d8179cb9 | 573 | $rc = $s->subscribe($feed_self_url); |
2c08214a | 574 | |
5ba1ddd4 | 575 | _debug("feed hub url found, subscribe request sent. [rc=$rc]", $debug_enabled); |
2c08214a | 576 | |
a42c55f0 | 577 | db_query("UPDATE ttrss_feeds SET pubsub_state = 1 |
2c08214a AD |
578 | WHERE id = '$feed'"); |
579 | } | |
580 | } | |
581 | ||
68cccafc | 582 | _debug("processing articles...", $debug_enabled); |
2c08214a | 583 | |
6c9f3d4a AD |
584 | $tstart = time(); |
585 | ||
19b3992b | 586 | foreach ($items as $item) { |
5d56d100 | 587 | if ($_REQUEST['xdebug'] == 3) { |
2c08214a AD |
588 | print_r($item); |
589 | } | |
590 | ||
6c9f3d4a AD |
591 | if (ini_get("max_execution_time") > 0 && time() - $tstart >= ini_get("max_execution_time") * 0.7) { |
592 | _debug("looks like there's too many articles to process at once, breaking out", $debug_enabled); | |
593 | break; | |
594 | } | |
595 | ||
19b3992b AD |
596 | $entry_guid = $item->get_id(); |
597 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
e6c886bf | 598 | if (!$entry_guid) $entry_guid = RSSUtils::make_guid_from_title($item->get_title()); |
2c08214a AD |
599 | if (!$entry_guid) continue; |
600 | ||
3a4c8973 AD |
601 | $entry_guid = "$owner_uid,$entry_guid"; |
602 | ||
a42c55f0 | 603 | $entry_guid_hashed = db_escape_string('SHA1:' . sha1($entry_guid)); |
5e3d5480 | 604 | |
68cccafc | 605 | _debug("guid $entry_guid / $entry_guid_hashed", $debug_enabled); |
5e3d5480 | 606 | |
2c08214a AD |
607 | $entry_timestamp = ""; |
608 | ||
04d2f9c8 AD |
609 | $entry_timestamp = $item->get_date(); |
610 | ||
611 | _debug("orig date: " . $item->get_date(), $debug_enabled); | |
2c08214a | 612 | |
30123fe6 | 613 | if ($entry_timestamp == -1 || !$entry_timestamp || $entry_timestamp > time()) { |
2c08214a | 614 | $entry_timestamp = time(); |
2c08214a AD |
615 | } |
616 | ||
617 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
618 | ||
68cccafc | 619 | _debug("date $entry_timestamp [$entry_timestamp_fmt]", $debug_enabled); |
2c08214a | 620 | |
99429e57 AD |
621 | // $entry_title = html_entity_decode($item->get_title(), ENT_COMPAT, 'UTF-8'); |
622 | // $entry_title = decode_numeric_entities($entry_title); | |
623 | $entry_title = $item->get_title(); | |
1b35d30c | 624 | |
5d56d100 | 625 | $entry_link = rewrite_relative_url($site_url, $item->get_link()); |
2c08214a | 626 | |
68cccafc AD |
627 | _debug("title $entry_title", $debug_enabled); |
628 | _debug("link $entry_link", $debug_enabled); | |
2c08214a AD |
629 | |
630 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
631 | ||
19b3992b AD |
632 | $entry_content = $item->get_content(); |
633 | if (!$entry_content) $entry_content = $item->get_description(); | |
2c08214a AD |
634 | |
635 | if ($_REQUEST["xdebug"] == 2) { | |
9ec10352 | 636 | print "content: "; |
0bc503ff | 637 | print htmlspecialchars($entry_content); |
3c696512 | 638 | print "\n"; |
2c08214a AD |
639 | } |
640 | ||
12ff230b AD |
641 | $entry_comments = db_escape_string(mb_substr($item->get_comments_url(), 0, 245)); |
642 | $num_comments = (int) $item->get_comments_count(); | |
2c08214a | 643 | |
12ff230b | 644 | $entry_author = $item->get_author(); // escaped later |
a42c55f0 | 645 | $entry_guid = db_escape_string(mb_substr($entry_guid, 0, 245)); |
2c08214a | 646 | |
68cccafc AD |
647 | _debug("author $entry_author", $debug_enabled); |
648 | _debug("num_comments: $num_comments", $debug_enabled); | |
ee78f81c | 649 | _debug("looking for tags...", $debug_enabled); |
2c08214a AD |
650 | |
651 | // parse <category> entries into tags | |
652 | ||
653 | $additional_tags = array(); | |
654 | ||
19b3992b | 655 | $additional_tags_src = $item->get_categories(); |
2c08214a | 656 | |
19b3992b AD |
657 | if (is_array($additional_tags_src)) { |
658 | foreach ($additional_tags_src as $tobj) { | |
cd07592c | 659 | array_push($additional_tags, $tobj); |
2c08214a | 660 | } |
19b3992b | 661 | } |
2c08214a | 662 | |
fa6fbd36 | 663 | $entry_tags = array_unique($additional_tags); |
2c08214a AD |
664 | |
665 | for ($i = 0; $i < count($entry_tags); $i++) | |
666 | $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8'); | |
667 | ||
ee78f81c AD |
668 | _debug("tags found: " . join(",", $entry_tags), $debug_enabled); |
669 | ||
68cccafc | 670 | _debug("done collecting data.", $debug_enabled); |
2c08214a | 671 | |
3318d324 | 672 | $result = db_query("SELECT id, content_hash, lang FROM ttrss_entries |
b1840673 | 673 | WHERE guid = '".db_escape_string($entry_guid)."' OR guid = '$entry_guid_hashed'"); |
b30abdad AD |
674 | |
675 | if (db_num_rows($result) != 0) { | |
b1840673 AD |
676 | $base_entry_id = db_fetch_result($result, 0, "id"); |
677 | $entry_stored_hash = db_fetch_result($result, 0, "content_hash"); | |
4a0da0e5 | 678 | $article_labels = Article::get_article_labels($base_entry_id, $owner_uid); |
3318d324 | 679 | $entry_language = db_fetch_result($result, 0, "lang"); |
a8ac7661 | 680 | |
2ed0d6c4 | 681 | $existing_tags = Article::get_article_tags($base_entry_id, $owner_uid); |
a8ac7661 AD |
682 | $entry_tags = array_unique(array_merge($entry_tags, $existing_tags)); |
683 | ||
b30abdad | 684 | } else { |
b1840673 AD |
685 | $base_entry_id = false; |
686 | $entry_stored_hash = ""; | |
a29fe121 | 687 | $article_labels = array(); |
3318d324 | 688 | $entry_language = ""; |
b30abdad AD |
689 | } |
690 | ||
455b1401 | 691 | $article = array("owner_uid" => $owner_uid, // read only |
b30abdad | 692 | "guid" => $entry_guid, // read only |
59e83455 | 693 | "guid_hashed" => $entry_guid_hashed, // read only |
19b3992b AD |
694 | "title" => $entry_title, |
695 | "content" => $entry_content, | |
696 | "link" => $entry_link, | |
a29fe121 | 697 | "labels" => $article_labels, // current limitation: can add labels to article, can't remove them |
19b3992b | 698 | "tags" => $entry_tags, |
e02555c1 | 699 | "author" => $entry_author, |
c9299c28 | 700 | "force_catchup" => false, // ugly hack for the time being |
6de3a1be | 701 | "score_modifier" => 0, // no previous value, plugin should recalculate score modifier based on content if needed |
3318d324 | 702 | "language" => $entry_language, |
f73e03e0 AD |
703 | "feed" => array("id" => $feed, |
704 | "fetch_url" => $fetch_url, | |
babfadbf J |
705 | "site_url" => $site_url, |
706 | "cache_images" => $cache_images) | |
e6c886bf | 707 | ); |
cc85704f | 708 | |
b1840673 | 709 | $entry_plugin_data = ""; |
e6c886bf | 710 | $entry_current_hash = RSSUtils::calculate_article_hash($article, $pluginhost); |
b1840673 AD |
711 | |
712 | _debug("article hash: $entry_current_hash [stored=$entry_stored_hash]", $debug_enabled); | |
713 | ||
522e8b35 | 714 | if ($entry_current_hash == $entry_stored_hash && !isset($_REQUEST["force_rehash"])) { |
b1840673 AD |
715 | _debug("stored article seems up to date [IID: $base_entry_id], updating timestamp only", $debug_enabled); |
716 | ||
717 | // we keep encountering the entry in feeds, so we need to | |
718 | // update date_updated column so that we don't get horrible | |
719 | // dupes when the entry gets purged and reinserted again e.g. | |
720 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
721 | ||
722 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
723 | ||
724 | db_query("UPDATE ttrss_entries SET date_updated = NOW() | |
725 | WHERE id = '$base_entry_id'"); | |
726 | ||
5bdcb8fd | 727 | continue; |
b1840673 AD |
728 | } |
729 | ||
730 | _debug("hash differs, applying plugin filters:", $debug_enabled); | |
731 | ||
1ffe3391 | 732 | foreach ($pluginhost->get_hooks(PluginHost::HOOK_ARTICLE_FILTER) as $plugin) { |
b1840673 AD |
733 | _debug("... " . get_class($plugin), $debug_enabled); |
734 | ||
735 | $start = microtime(true); | |
19b3992b | 736 | $article = $plugin->hook_article_filter($article); |
0084f0d1 | 737 | |
b1840673 AD |
738 | _debug("=== " . sprintf("%.4f (sec)", microtime(true) - $start), $debug_enabled); |
739 | ||
740 | $entry_plugin_data .= mb_strtolower(get_class($plugin)) . ","; | |
cc85704f AD |
741 | } |
742 | ||
0bc503ff AD |
743 | if ($_REQUEST["xdebug"] == 2) { |
744 | print "processed content: "; | |
745 | print htmlspecialchars($article["content"]); | |
746 | print "\n"; | |
747 | } | |
748 | ||
b1840673 AD |
749 | $entry_plugin_data = db_escape_string($entry_plugin_data); |
750 | ||
751 | _debug("plugin data: $entry_plugin_data", $debug_enabled); | |
752 | ||
35c12dc4 AD |
753 | // Workaround: 4-byte unicode requires utf8mb4 in MySQL. See https://tt-rss.org/forum/viewtopic.php?f=1&t=3377&p=20077#p20077 |
754 | if (DB_TYPE == "mysql") { | |
755 | foreach ($article as $k => $v) { | |
35c37354 AD |
756 | |
757 | // i guess we'll have to take the risk of 4byte unicode labels & tags here | |
dae16f72 | 758 | if (is_string($article[$k])) { |
35c37354 AD |
759 | $article[$k] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $v); |
760 | } | |
35c12dc4 AD |
761 | } |
762 | } | |
763 | ||
b8774453 AD |
764 | /* Collect article tags here so we could filter by them: */ |
765 | ||
557d86fe AD |
766 | $matched_rules = array(); |
767 | ||
e6c886bf | 768 | $article_filters = RSSUtils::get_article_filters($filters, $article["title"], |
7b55001e | 769 | $article["content"], $article["link"], $article["author"], |
557d86fe | 770 | $article["tags"], $matched_rules); |
b8774453 AD |
771 | |
772 | if ($debug_enabled) { | |
557d86fe AD |
773 | _debug("matched filter rules: ", $debug_enabled); |
774 | ||
775 | if (count($matched_rules) != 0) { | |
776 | print_r($matched_rules); | |
777 | } | |
778 | ||
779 | _debug("filter actions: ", $debug_enabled); | |
780 | ||
b8774453 AD |
781 | if (count($article_filters) != 0) { |
782 | print_r($article_filters); | |
783 | } | |
784 | } | |
785 | ||
e6c886bf | 786 | $plugin_filter_names = RSSUtils::find_article_filters($article_filters, "plugin"); |
b8774453 AD |
787 | $plugin_filter_actions = $pluginhost->get_filter_actions(); |
788 | ||
789 | if (count($plugin_filter_names) > 0) { | |
790 | _debug("applying plugin filter actions...", $debug_enabled); | |
791 | ||
792 | foreach ($plugin_filter_names as $pfn) { | |
793 | list($pfclass,$pfaction) = explode(":", $pfn["param"]); | |
794 | ||
795 | if (isset($plugin_filter_actions[$pfclass])) { | |
796 | $plugin = $pluginhost->get_plugin($pfclass); | |
797 | ||
798 | _debug("... $pfclass: $pfaction", $debug_enabled); | |
799 | ||
800 | if ($plugin) { | |
801 | $start = microtime(true); | |
802 | $article = $plugin->hook_article_filter_action($article, $pfaction); | |
803 | ||
804 | _debug("=== " . sprintf("%.4f (sec)", microtime(true) - $start), $debug_enabled); | |
805 | } else { | |
806 | _debug("??? $pfclass: plugin object not found."); | |
807 | } | |
808 | } else { | |
809 | _debug("??? $pfclass: filter plugin not registered."); | |
810 | } | |
811 | } | |
812 | } | |
813 | ||
19b3992b | 814 | $entry_tags = $article["tags"]; |
a42c55f0 AD |
815 | $entry_guid = db_escape_string($entry_guid); |
816 | $entry_title = db_escape_string($article["title"]); | |
12ff230b | 817 | $entry_author = db_escape_string(mb_substr($article["author"], 0, 245)); |
a42c55f0 | 818 | $entry_link = db_escape_string($article["link"]); |
f935d98e | 819 | $entry_content = $article["content"]; // escaped below |
c9299c28 | 820 | $entry_force_catchup = $article["force_catchup"]; |
a29fe121 | 821 | $article_labels = $article["labels"]; |
6de3a1be | 822 | $entry_score_modifier = (int) $article["score_modifier"]; |
3318d324 | 823 | $entry_language = db_escape_string($article["language"]); |
a29fe121 AD |
824 | |
825 | if ($debug_enabled) { | |
826 | _debug("article labels:", $debug_enabled); | |
557d86fe AD |
827 | |
828 | if (count($article_labels) != 0) { | |
829 | print_r($article_labels); | |
830 | } | |
a29fe121 | 831 | } |
c9299c28 AD |
832 | |
833 | _debug("force catchup: $entry_force_catchup"); | |
f935d98e | 834 | |
0a3fd79b | 835 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
e6c886bf | 836 | RSSUtils::cache_media($entry_content, $site_url, $debug_enabled); |
0a3fd79b | 837 | |
a42c55f0 | 838 | $entry_content = db_escape_string($entry_content, false); |
1f45c857 | 839 | |
07877caf | 840 | //db_query("BEGIN"); |
2c08214a | 841 | |
a42c55f0 | 842 | $result = db_query("SELECT id FROM ttrss_entries |
5e3d5480 | 843 | WHERE (guid = '$entry_guid' OR guid = '$entry_guid_hashed')"); |
9e222305 | 844 | |
2c08214a AD |
845 | if (db_num_rows($result) == 0) { |
846 | ||
07d3431e | 847 | _debug("base guid [$entry_guid or $entry_guid_hashed] not found, creating...", $debug_enabled); |
2c08214a AD |
848 | |
849 | // base post entry does not exist, create it | |
850 | ||
07d3431e | 851 | db_query( |
2c08214a AD |
852 | "INSERT INTO ttrss_entries |
853 | (title, | |
854 | guid, | |
855 | link, | |
856 | updated, | |
857 | content, | |
858 | content_hash, | |
859 | no_orig_date, | |
860 | date_updated, | |
861 | date_entered, | |
862 | comments, | |
863 | num_comments, | |
b30abdad | 864 | plugin_data, |
6b461797 | 865 | lang, |
2c08214a AD |
866 | author) |
867 | VALUES | |
868 | ('$entry_title', | |
5e3d5480 | 869 | '$entry_guid_hashed', |
2c08214a AD |
870 | '$entry_link', |
871 | '$entry_timestamp_fmt', | |
872 | '$entry_content', | |
b1840673 | 873 | '$entry_current_hash', |
5ba1ddd4 | 874 | false, |
2c08214a | 875 | NOW(), |
be574731 | 876 | '$date_feed_processed', |
2c08214a AD |
877 | '$entry_comments', |
878 | '$num_comments', | |
b30abdad | 879 | '$entry_plugin_data', |
6b461797 | 880 | '$entry_language', |
2c08214a | 881 | '$entry_author')"); |
e8291805 | 882 | |
2c08214a AD |
883 | } |
884 | ||
885 | // now it should exist, if not - bad luck then | |
886 | ||
b1840673 | 887 | $result = db_query("SELECT id FROM ttrss_entries |
5e3d5480 | 888 | WHERE guid = '$entry_guid' OR guid = '$entry_guid_hashed'"); |
2c08214a AD |
889 | |
890 | $entry_ref_id = 0; | |
891 | $entry_int_id = 0; | |
892 | ||
893 | if (db_num_rows($result) == 1) { | |
894 | ||
68cccafc | 895 | _debug("base guid found, checking for user record", $debug_enabled); |
2c08214a | 896 | |
2c08214a AD |
897 | $ref_id = db_fetch_result($result, 0, "id"); |
898 | $entry_ref_id = $ref_id; | |
899 | ||
5e3d5480 AD |
900 | /* $stored_guid = db_fetch_result($result, 0, "guid"); |
901 | if ($stored_guid != $entry_guid_hashed) { | |
68cccafc | 902 | if ($debug_enabled) _debug("upgrading compat guid to hashed one", $debug_enabled); |
5e3d5480 | 903 | |
a42c55f0 | 904 | db_query("UPDATE ttrss_entries SET guid = '$entry_guid_hashed' WHERE |
5e3d5480 AD |
905 | id = '$ref_id'"); |
906 | } */ | |
907 | ||
e6c886bf | 908 | if (RSSUtils::find_article_filter($article_filters, "filter")) { |
07877caf | 909 | //db_query("COMMIT"); // close transaction in progress |
2c08214a AD |
910 | continue; |
911 | } | |
912 | ||
e6c886bf | 913 | $score = RSSUtils::calculate_article_score($article_filters) + $entry_score_modifier; |
2c08214a | 914 | |
6de3a1be | 915 | _debug("initial score: $score [including plugin modifier: $entry_score_modifier]", $debug_enabled); |
2c08214a | 916 | |
4f186b1f AD |
917 | // check for user post link to main table |
918 | ||
2c08214a | 919 | $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE |
5bdcb8fd | 920 | ref_id = '$ref_id' AND owner_uid = '$owner_uid'"; |
2c08214a AD |
921 | |
922 | // if ($_REQUEST["xdebug"]) print "$query\n"; | |
923 | ||
a42c55f0 | 924 | $result = db_query($query); |
2c08214a AD |
925 | |
926 | // okay it doesn't exist - create user entry | |
927 | if (db_num_rows($result) == 0) { | |
928 | ||
68cccafc | 929 | _debug("user record not found, creating...", $debug_enabled); |
2c08214a | 930 | |
e6c886bf | 931 | if ($score >= -500 && !RSSUtils::find_article_filter($article_filters, 'catchup') && !$entry_force_catchup) { |
2c08214a AD |
932 | $unread = 'true'; |
933 | $last_read_qpart = 'NULL'; | |
934 | } else { | |
935 | $unread = 'false'; | |
936 | $last_read_qpart = 'NOW()'; | |
937 | } | |
938 | ||
e6c886bf | 939 | if (RSSUtils::find_article_filter($article_filters, 'mark') || $score > 1000) { |
2c08214a AD |
940 | $marked = 'true'; |
941 | } else { | |
942 | $marked = 'false'; | |
943 | } | |
944 | ||
e6c886bf | 945 | if (RSSUtils::find_article_filter($article_filters, 'publish')) { |
2c08214a AD |
946 | $published = 'true'; |
947 | } else { | |
948 | $published = 'false'; | |
949 | } | |
950 | ||
7873d588 AD |
951 | $last_marked = ($marked == 'true') ? 'NOW()' : 'NULL'; |
952 | $last_published = ($published == 'true') ? 'NOW()' : 'NULL'; | |
953 | ||
6322ac79 | 954 | $result = db_query( |
2c08214a AD |
955 | "INSERT INTO ttrss_user_entries |
956 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
7873d588 AD |
957 | published, score, tag_cache, label_cache, uuid, |
958 | last_marked, last_published) | |
2c08214a | 959 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
7873d588 AD |
960 | $last_read_qpart, $marked, $published, '$score', '', '', |
961 | '', $last_marked, $last_published)"); | |
2c08214a AD |
962 | |
963 | if (PUBSUBHUBBUB_HUB && $published == 'true') { | |
964 | $rss_link = get_self_url_prefix() . | |
965 | "/public.php?op=rss&id=-2&key=" . | |
a42c55f0 | 966 | get_feed_access_key(-2, false, $owner_uid); |
2c08214a | 967 | |
cb3f8773 | 968 | $p = new pubsubhubbub\publisher\Publisher(PUBSUBHUBBUB_HUB); |
2c08214a | 969 | |
5ba1ddd4 | 970 | /* $pubsub_result = */ $p->publish_update($rss_link); |
2c08214a AD |
971 | } |
972 | ||
6322ac79 | 973 | $result = db_query( |
2c08214a AD |
974 | "SELECT int_id FROM ttrss_user_entries WHERE |
975 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
976 | feed_id = '$feed' LIMIT 1"); | |
977 | ||
978 | if (db_num_rows($result) == 1) { | |
979 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
980 | } | |
981 | } else { | |
68cccafc | 982 | _debug("user record FOUND", $debug_enabled); |
2c08214a AD |
983 | |
984 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
985 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
986 | } | |
987 | ||
68cccafc | 988 | _debug("RID: $entry_ref_id, IID: $entry_int_id", $debug_enabled); |
2c08214a | 989 | |
e854442e | 990 | if (DB_TYPE == "pgsql") { |
e6c886bf | 991 | $tsvector_combined = db_escape_string(mb_substr($entry_title . ' ' . strip_tags(str_replace('<', ' <', $entry_content)), |
e854442e AD |
992 | 0, 1000000)); |
993 | ||
df659891 | 994 | $tsvector_qpart = "tsvector_combined = to_tsvector('$feed_language', '$tsvector_combined'),"; |
e854442e AD |
995 | |
996 | } else { | |
997 | $tsvector_qpart = ""; | |
998 | } | |
999 | ||
b1840673 AD |
1000 | db_query("UPDATE ttrss_entries |
1001 | SET title = '$entry_title', | |
1002 | content = '$entry_content', | |
1003 | content_hash = '$entry_current_hash', | |
1004 | updated = '$entry_timestamp_fmt', | |
e854442e | 1005 | $tsvector_qpart |
b1840673 AD |
1006 | num_comments = '$num_comments', |
1007 | plugin_data = '$entry_plugin_data', | |
1008 | author = '$entry_author', | |
1009 | lang = '$entry_language' | |
1010 | WHERE id = '$ref_id'"); | |
1011 | ||
59e83455 AD |
1012 | // update aux data |
1013 | db_query("UPDATE ttrss_user_entries | |
1014 | SET score = '$score' WHERE ref_id = '$ref_id'"); | |
1015 | ||
b1840673 | 1016 | if ($mark_unread_on_update) { |
24e6ff5d AD |
1017 | _debug("article updated, marking unread as requested.", $debug_enabled); |
1018 | ||
b1840673 AD |
1019 | db_query("UPDATE ttrss_user_entries |
1020 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
2c08214a AD |
1021 | } |
1022 | } | |
1023 | ||
07877caf | 1024 | //db_query("COMMIT"); |
2c08214a | 1025 | |
a29fe121 AD |
1026 | _debug("assigning labels [other]...", $debug_enabled); |
1027 | ||
1028 | foreach ($article_labels as $label) { | |
7c9b5a3f | 1029 | Labels::add_article($entry_ref_id, $label[1], $owner_uid); |
a29fe121 AD |
1030 | } |
1031 | ||
1032 | _debug("assigning labels [filters]...", $debug_enabled); | |
2c08214a | 1033 | |
e6c886bf | 1034 | RSSUtils::assign_article_to_label_filters($entry_ref_id, $article_filters, |
b24504b1 | 1035 | $owner_uid, $article_labels); |
2c08214a | 1036 | |
68cccafc | 1037 | _debug("looking for enclosures...", $debug_enabled); |
2c08214a AD |
1038 | |
1039 | // enclosures | |
1040 | ||
1041 | $enclosures = array(); | |
1042 | ||
19b3992b | 1043 | $encs = $item->get_enclosures(); |
2c08214a | 1044 | |
19b3992b AD |
1045 | if (is_array($encs)) { |
1046 | foreach ($encs as $e) { | |
1047 | $e_item = array( | |
86e53429 AD |
1048 | rewrite_relative_url($site_url, $e->link), |
1049 | $e->type, $e->length, $e->title, $e->width, $e->height); | |
2c08214a | 1050 | array_push($enclosures, $e_item); |
2c08214a AD |
1051 | } |
1052 | } | |
1053 | ||
388d4dfa | 1054 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
e6c886bf | 1055 | RSSUtils::cache_enclosures($enclosures, $site_url, $debug_enabled); |
388d4dfa | 1056 | |
2c08214a | 1057 | if ($debug_enabled) { |
68cccafc | 1058 | _debug("article enclosures:", $debug_enabled); |
2c08214a AD |
1059 | print_r($enclosures); |
1060 | } | |
1061 | ||
dab229cd | 1062 | //db_query("BEGIN"); |
2c08214a | 1063 | |
5c54e683 AD |
1064 | // debugging |
1065 | // db_query("DELETE FROM ttrss_enclosures WHERE post_id = '$entry_ref_id'"); | |
1066 | ||
2c08214a | 1067 | foreach ($enclosures as $enc) { |
a42c55f0 AD |
1068 | $enc_url = db_escape_string($enc[0]); |
1069 | $enc_type = db_escape_string($enc[1]); | |
1070 | $enc_dur = db_escape_string($enc[2]); | |
5c54e683 | 1071 | $enc_title = db_escape_string($enc[3]); |
523bd90b FE |
1072 | $enc_width = intval($enc[4]); |
1073 | $enc_height = intval($enc[5]); | |
2c08214a | 1074 | |
a42c55f0 | 1075 | $result = db_query("SELECT id FROM ttrss_enclosures |
2c08214a AD |
1076 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); |
1077 | ||
1078 | if (db_num_rows($result) == 0) { | |
a42c55f0 | 1079 | db_query("INSERT INTO ttrss_enclosures |
523bd90b FE |
1080 | (content_url, content_type, title, duration, post_id, width, height) VALUES |
1081 | ('$enc_url', '$enc_type', '$enc_title', '$enc_dur', '$entry_ref_id', $enc_width, $enc_height)"); | |
2c08214a AD |
1082 | } |
1083 | } | |
1084 | ||
dab229cd | 1085 | //db_query("COMMIT"); |
2c08214a AD |
1086 | |
1087 | // check for manual tags (we have to do it here since they're loaded from filters) | |
1088 | ||
1089 | foreach ($article_filters as $f) { | |
6aff7845 | 1090 | if ($f["type"] == "tag") { |
2c08214a | 1091 | |
6aff7845 | 1092 | $manual_tags = trim_array(explode(",", $f["param"])); |
2c08214a AD |
1093 | |
1094 | foreach ($manual_tags as $tag) { | |
1095 | if (tag_is_valid($tag)) { | |
1096 | array_push($entry_tags, $tag); | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | // Skip boring tags | |
1103 | ||
6322ac79 | 1104 | $boring_tags = trim_array(explode(",", mb_strtolower(get_pref( |
2c08214a AD |
1105 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); |
1106 | ||
1107 | $filtered_tags = array(); | |
1108 | $tags_to_cache = array(); | |
1109 | ||
1110 | if ($entry_tags && is_array($entry_tags)) { | |
1111 | foreach ($entry_tags as $tag) { | |
1112 | if (array_search($tag, $boring_tags) === false) { | |
1113 | array_push($filtered_tags, $tag); | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | $filtered_tags = array_unique($filtered_tags); | |
1119 | ||
1120 | if ($debug_enabled) { | |
68cccafc | 1121 | _debug("filtered article tags:", $debug_enabled); |
2c08214a AD |
1122 | print_r($filtered_tags); |
1123 | } | |
1124 | ||
1125 | // Save article tags in the database | |
1126 | ||
1127 | if (count($filtered_tags) > 0) { | |
1128 | ||
dab229cd | 1129 | //db_query("BEGIN"); |
2c08214a AD |
1130 | |
1131 | foreach ($filtered_tags as $tag) { | |
1132 | ||
1133 | $tag = sanitize_tag($tag); | |
a42c55f0 | 1134 | $tag = db_escape_string($tag); |
2c08214a AD |
1135 | |
1136 | if (!tag_is_valid($tag)) continue; | |
1137 | ||
a42c55f0 | 1138 | $result = db_query("SELECT id FROM ttrss_tags |
2c08214a AD |
1139 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND |
1140 | owner_uid = '$owner_uid' LIMIT 1"); | |
1141 | ||
e6c886bf | 1142 | if ($result && db_num_rows($result) == 0) { |
2c08214a | 1143 | |
e6c886bf | 1144 | db_query("INSERT INTO ttrss_tags |
2c08214a AD |
1145 | (owner_uid,tag_name,post_int_id) |
1146 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
e6c886bf | 1147 | } |
2c08214a AD |
1148 | |
1149 | array_push($tags_to_cache, $tag); | |
1150 | } | |
1151 | ||
1152 | /* update the cache */ | |
1153 | ||
1154 | $tags_to_cache = array_unique($tags_to_cache); | |
1155 | ||
a42c55f0 | 1156 | $tags_str = db_escape_string(join(",", $tags_to_cache)); |
2c08214a | 1157 | |
a42c55f0 | 1158 | db_query("UPDATE ttrss_user_entries |
2c08214a AD |
1159 | SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id' |
1160 | AND owner_uid = $owner_uid"); | |
1161 | ||
dab229cd | 1162 | //db_query("COMMIT"); |
2c08214a AD |
1163 | } |
1164 | ||
68cccafc | 1165 | _debug("article processed", $debug_enabled); |
2c08214a AD |
1166 | } |
1167 | ||
68cccafc | 1168 | _debug("purging feed...", $debug_enabled); |
2c08214a | 1169 | |
a42c55f0 | 1170 | purge_feed($feed, 0, $debug_enabled); |
2c08214a | 1171 | |
a42c55f0 | 1172 | db_query("UPDATE ttrss_feeds |
f2798eb6 | 1173 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
2c08214a | 1174 | |
a42c55f0 | 1175 | // db_query("COMMIT"); |
2c08214a AD |
1176 | |
1177 | } else { | |
1178 | ||
a42c55f0 | 1179 | $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245)); |
2c08214a | 1180 | |
4ad04ee2 AD |
1181 | _debug("fetch error: $error_msg", $debug_enabled); |
1182 | ||
1183 | if (count($rss->errors()) > 1) { | |
1184 | foreach ($rss->errors() as $error) { | |
1185 | _debug("+ $error"); | |
1186 | } | |
1187 | } | |
2c08214a | 1188 | |
6322ac79 | 1189 | db_query( |
2c08214a | 1190 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
88edaa93 | 1191 | last_updated = NOW() WHERE id = '$feed'"); |
2c08214a | 1192 | |
88edaa93 | 1193 | unset($rss); |
7b55001e | 1194 | return; |
88edaa93 | 1195 | } |
2c08214a | 1196 | |
68cccafc | 1197 | _debug("done", $debug_enabled); |
88edaa93 | 1198 | |
7b55001e | 1199 | return true; |
2c08214a AD |
1200 | } |
1201 | ||
e6c886bf | 1202 | static function cache_enclosures($enclosures, $site_url, $debug) { |
388d4dfa AD |
1203 | foreach ($enclosures as $enc) { |
1204 | ||
1205 | if (preg_match("/(image|audio|video)/", $enc[1])) { | |
1206 | ||
1207 | $src = rewrite_relative_url($site_url, $enc[0]); | |
1208 | ||
1209 | $local_filename = CACHE_DIR . "/images/" . sha1($src); | |
1210 | ||
1211 | if ($debug) _debug("cache_enclosures: downloading: $src to $local_filename"); | |
1212 | ||
1213 | if (!file_exists($local_filename)) { | |
1214 | $file_content = fetch_file_contents($src); | |
1215 | ||
1216 | if ($file_content && strlen($file_content) > _MIN_CACHE_FILE_SIZE) { | |
1217 | file_put_contents($local_filename, $file_content); | |
1218 | } | |
1219 | } else { | |
1220 | touch($local_filename); | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | ||
e6c886bf | 1226 | static function cache_media($html, $site_url, $debug) { |
3c696512 AD |
1227 | libxml_use_internal_errors(true); |
1228 | ||
1229 | $charset_hack = '<head> | |
1230 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
1231 | </head>'; | |
1232 | ||
1233 | $doc = new DOMDocument(); | |
1234 | $doc->loadHTML($charset_hack . $html); | |
1235 | $xpath = new DOMXPath($doc); | |
1236 | ||
388d4dfa | 1237 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])|(//audio/source[@src])'); |
3c696512 AD |
1238 | |
1239 | foreach ($entries as $entry) { | |
5edd605a | 1240 | if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) { |
3c696512 AD |
1241 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); |
1242 | ||
41bead9b | 1243 | $local_filename = CACHE_DIR . "/images/" . sha1($src); |
3c696512 | 1244 | |
41bead9b | 1245 | if ($debug) _debug("cache_media: downloading: $src to $local_filename"); |
3c696512 AD |
1246 | |
1247 | if (!file_exists($local_filename)) { | |
1248 | $file_content = fetch_file_contents($src); | |
1249 | ||
41bead9b | 1250 | if ($file_content && strlen($file_content) > _MIN_CACHE_FILE_SIZE) { |
3c696512 AD |
1251 | file_put_contents($local_filename, $file_content); |
1252 | } | |
4a27966e J |
1253 | } else { |
1254 | touch($local_filename); | |
3c696512 | 1255 | } |
3c696512 AD |
1256 | } |
1257 | } | |
3c696512 AD |
1258 | } |
1259 | ||
e6c886bf | 1260 | static function expire_error_log($debug) { |
e2261e17 AD |
1261 | if ($debug) _debug("Removing old error log entries..."); |
1262 | ||
1263 | if (DB_TYPE == "pgsql") { | |
a42c55f0 | 1264 | db_query("DELETE FROM ttrss_error_log |
e2261e17 AD |
1265 | WHERE created_at < NOW() - INTERVAL '7 days'"); |
1266 | } else { | |
a42c55f0 | 1267 | db_query("DELETE FROM ttrss_error_log |
e2261e17 AD |
1268 | WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)"); |
1269 | } | |
1270 | ||
1271 | } | |
1272 | ||
e6c886bf | 1273 | static function expire_lock_files($debug) { |
65465085 | 1274 | //if ($debug) _debug("Removing old lock files..."); |
2a91b6ff AD |
1275 | |
1276 | $num_deleted = 0; | |
1277 | ||
1278 | if (is_writable(LOCK_DIRECTORY)) { | |
1279 | $files = glob(LOCK_DIRECTORY . "/*.lock"); | |
1280 | ||
1281 | if ($files) { | |
1282 | foreach ($files as $file) { | |
11344971 | 1283 | if (!file_is_locked(basename($file)) && time() - filemtime($file) > 86400*2) { |
2a91b6ff AD |
1284 | unlink($file); |
1285 | ++$num_deleted; | |
1286 | } | |
1287 | } | |
1288 | } | |
1289 | } | |
1290 | ||
65465085 | 1291 | if ($debug) _debug("Removed $num_deleted old lock files."); |
2a91b6ff AD |
1292 | } |
1293 | ||
e6c886bf | 1294 | static function expire_cached_files($debug) { |
3306daec | 1295 | foreach (array("simplepie", "images", "export", "upload") as $dir) { |
3c696512 | 1296 | $cache_dir = CACHE_DIR . "/$dir"; |
2c08214a | 1297 | |
65465085 | 1298 | // if ($debug) _debug("Expiring $cache_dir"); |
2c08214a | 1299 | |
3c696512 AD |
1300 | $num_deleted = 0; |
1301 | ||
1302 | if (is_writable($cache_dir)) { | |
1303 | $files = glob("$cache_dir/*"); | |
1304 | ||
2a91b6ff | 1305 | if ($files) { |
2ab20c31 AD |
1306 | foreach ($files as $file) { |
1307 | if (time() - filemtime($file) > 86400*7) { | |
1308 | unlink($file); | |
3c696512 | 1309 | |
2ab20c31 AD |
1310 | ++$num_deleted; |
1311 | } | |
3c696512 AD |
1312 | } |
1313 | } | |
2a91b6ff | 1314 | } |
3c696512 | 1315 | |
65465085 | 1316 | if ($debug) _debug("$cache_dir: removed $num_deleted files."); |
3c696512 AD |
1317 | } |
1318 | } | |
2c08214a | 1319 | |
a3e0bdcf | 1320 | /** |
e6c886bf AD |
1321 | * Source: http://www.php.net/manual/en/function.parse-url.php#104527 |
1322 | * Returns the url query as associative array | |
1323 | * | |
1324 | * @param string query | |
1325 | * @return array params | |
1326 | */ | |
1327 | static function convertUrlQuery($query) { | |
a3e0bdcf AD |
1328 | $queryParts = explode('&', $query); |
1329 | ||
1330 | $params = array(); | |
1331 | ||
1332 | foreach ($queryParts as $param) { | |
1333 | $item = explode('=', $param); | |
1334 | $params[$item[0]] = $item[1]; | |
1335 | } | |
1336 | ||
1337 | return $params; | |
1338 | } | |
92c14e9d | 1339 | |
e6c886bf | 1340 | static function get_article_filters($filters, $title, $content, $link, $author, $tags, &$matched_rules = false) { |
92c14e9d AD |
1341 | $matches = array(); |
1342 | ||
1343 | foreach ($filters as $filter) { | |
1344 | $match_any_rule = $filter["match_any_rule"]; | |
a3a896a1 | 1345 | $inverse = $filter["inverse"]; |
92c14e9d AD |
1346 | $filter_match = false; |
1347 | ||
1348 | foreach ($filter["rules"] as $rule) { | |
1349 | $match = false; | |
ffa1bd7b | 1350 | $reg_exp = str_replace('/', '\/', $rule["reg_exp"]); |
a3a896a1 | 1351 | $rule_inverse = $rule["inverse"]; |
92c14e9d AD |
1352 | |
1353 | if (!$reg_exp) | |
1354 | continue; | |
1355 | ||
1356 | switch ($rule["type"]) { | |
e6c886bf AD |
1357 | case "title": |
1358 | $match = @preg_match("/$reg_exp/iu", $title); | |
1359 | break; | |
1360 | case "content": | |
1361 | // we don't need to deal with multiline regexps | |
1362 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
d03ae73e | 1363 | |
e6c886bf AD |
1364 | $match = @preg_match("/$reg_exp/iu", $content); |
1365 | break; | |
1366 | case "both": | |
1367 | // we don't need to deal with multiline regexps | |
1368 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
d03ae73e | 1369 | |
e6c886bf AD |
1370 | $match = (@preg_match("/$reg_exp/iu", $title) || @preg_match("/$reg_exp/iu", $content)); |
1371 | break; | |
1372 | case "link": | |
1373 | $match = @preg_match("/$reg_exp/iu", $link); | |
1374 | break; | |
1375 | case "author": | |
1376 | $match = @preg_match("/$reg_exp/iu", $author); | |
1377 | break; | |
1378 | case "tag": | |
1379 | foreach ($tags as $tag) { | |
1380 | if (@preg_match("/$reg_exp/iu", $tag)) { | |
1381 | $match = true; | |
1382 | break; | |
1383 | } | |
7b80b5e1 | 1384 | } |
e6c886bf | 1385 | break; |
92c14e9d AD |
1386 | } |
1387 | ||
a3a896a1 AD |
1388 | if ($rule_inverse) $match = !$match; |
1389 | ||
92c14e9d AD |
1390 | if ($match_any_rule) { |
1391 | if ($match) { | |
1392 | $filter_match = true; | |
1393 | break; | |
1394 | } | |
1395 | } else { | |
1396 | $filter_match = $match; | |
1397 | if (!$match) { | |
1398 | break; | |
1399 | } | |
1400 | } | |
1401 | } | |
1402 | ||
a3a896a1 AD |
1403 | if ($inverse) $filter_match = !$filter_match; |
1404 | ||
92c14e9d | 1405 | if ($filter_match) { |
557d86fe AD |
1406 | if (is_array($matched_rules)) array_push($matched_rules, $rule); |
1407 | ||
92c14e9d AD |
1408 | foreach ($filter["actions"] AS $action) { |
1409 | array_push($matches, $action); | |
5e736e45 AD |
1410 | |
1411 | // if Stop action encountered, perform no further processing | |
fd3e5e8d | 1412 | if (isset($action["type"]) && $action["type"] == "stop") return $matches; |
92c14e9d AD |
1413 | } |
1414 | } | |
1415 | } | |
1416 | ||
1417 | return $matches; | |
1418 | } | |
1419 | ||
e6c886bf | 1420 | static function find_article_filter($filters, $filter_name) { |
92c14e9d AD |
1421 | foreach ($filters as $f) { |
1422 | if ($f["type"] == $filter_name) { | |
1423 | return $f; | |
1424 | }; | |
1425 | } | |
1426 | return false; | |
1427 | } | |
1428 | ||
e6c886bf | 1429 | static function find_article_filters($filters, $filter_name) { |
92c14e9d AD |
1430 | $results = array(); |
1431 | ||
1432 | foreach ($filters as $f) { | |
1433 | if ($f["type"] == $filter_name) { | |
1434 | array_push($results, $f); | |
1435 | }; | |
1436 | } | |
1437 | return $results; | |
1438 | } | |
1439 | ||
e6c886bf | 1440 | static function calculate_article_score($filters) { |
92c14e9d AD |
1441 | $score = 0; |
1442 | ||
1443 | foreach ($filters as $f) { | |
1444 | if ($f["type"] == "score") { | |
1445 | $score += $f["param"]; | |
1446 | }; | |
1447 | } | |
1448 | return $score; | |
1449 | } | |
1450 | ||
e6c886bf | 1451 | static function labels_contains_caption($labels, $caption) { |
b24504b1 AD |
1452 | foreach ($labels as $label) { |
1453 | if ($label[1] == $caption) { | |
1454 | return true; | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | return false; | |
1459 | } | |
1460 | ||
e6c886bf | 1461 | static function assign_article_to_label_filters($id, $filters, $owner_uid, $article_labels) { |
92c14e9d AD |
1462 | foreach ($filters as $f) { |
1463 | if ($f["type"] == "label") { | |
e6c886bf | 1464 | if (!RSSUtils::labels_contains_caption($article_labels, $f["param"])) { |
7c9b5a3f | 1465 | Labels::add_article($id, $f["param"], $owner_uid); |
b24504b1 AD |
1466 | } |
1467 | } | |
92c14e9d AD |
1468 | } |
1469 | } | |
87764a50 | 1470 | |
e6c886bf | 1471 | static function make_guid_from_title($title) { |
87d7e850 AD |
1472 | return preg_replace("/[ \"\',.:;]/", "-", |
1473 | mb_strtolower(strip_tags($title), 'utf-8')); | |
1474 | } | |
1475 | ||
e6c886bf | 1476 | static function cleanup_counters_cache($debug) { |
168cf351 AD |
1477 | $result = db_query("DELETE FROM ttrss_counters_cache |
1478 | WHERE feed_id > 0 AND | |
1479 | (SELECT COUNT(id) FROM ttrss_feeds WHERE | |
1480 | id = feed_id AND | |
1481 | ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid) = 0"); | |
1482 | $frows = db_affected_rows($result); | |
1483 | ||
1484 | $result = db_query("DELETE FROM ttrss_cat_counters_cache | |
1485 | WHERE feed_id > 0 AND | |
1486 | (SELECT COUNT(id) FROM ttrss_feed_categories WHERE | |
1487 | id = feed_id AND | |
1488 | ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid) = 0"); | |
1489 | $crows = db_affected_rows($result); | |
1490 | ||
7b55001e | 1491 | if ($debug) _debug("Removed $frows (feeds) $crows (cats) orphaned counter cache entries."); |
168cf351 AD |
1492 | } |
1493 | ||
e6c886bf | 1494 | static function housekeeping_user($owner_uid) { |
5cbd1fe8 AD |
1495 | $tmph = new PluginHost(); |
1496 | ||
1497 | load_user_plugins($owner_uid, $tmph); | |
1498 | ||
1499 | $tmph->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); | |
1500 | } | |
1501 | ||
e6c886bf AD |
1502 | static function housekeeping_common($debug) { |
1503 | RSSUtils::expire_cached_files($debug); | |
1504 | RSSUtils::expire_lock_files($debug); | |
1505 | RSSUtils::expire_error_log($debug); | |
e2cf81e2 | 1506 | |
e6c886bf | 1507 | $count = RSSUtils::update_feedbrowser_cache(); |
e2cf81e2 AD |
1508 | _debug("Feedbrowser updated, $count feeds processed."); |
1509 | ||
a230bf88 | 1510 | Article::purge_orphans( true); |
e6c886bf | 1511 | RSSUtils::cleanup_counters_cache($debug); |
e2cf81e2 | 1512 | |
9b736a20 AD |
1513 | //$rc = cleanup_tags( 14, 50000); |
1514 | //_debug("Cleaned $rc cached tags."); | |
8e470220 | 1515 | |
00f22824 | 1516 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", ""); |
e2cf81e2 | 1517 | } |
ea79a0e0 | 1518 | |
e6c886bf AD |
1519 | static function check_feed_favicon($site_url, $feed) { |
1520 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
a230bf88 AD |
1521 | |
1522 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
1523 | ||
1524 | if (!file_exists($icon_file)) { | |
1525 | $favicon_url = get_favicon_url($site_url); | |
1526 | ||
1527 | if ($favicon_url) { | |
1528 | // Limiting to "image" type misses those served with text/plain | |
1529 | $contents = fetch_file_contents($favicon_url); // , "image"); | |
1530 | ||
1531 | if ($contents) { | |
1532 | // Crude image type matching. | |
1533 | // Patterns gleaned from the file(1) source code. | |
1534 | if (preg_match('/^\x00\x00\x01\x00/', $contents)) { | |
1535 | // 0 string \000\000\001\000 MS Windows icon resource | |
1536 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); | |
1537 | } | |
1538 | elseif (preg_match('/^GIF8/', $contents)) { | |
1539 | // 0 string GIF8 GIF image data | |
1540 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); | |
1541 | } | |
1542 | elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { | |
1543 | // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data | |
1544 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); | |
1545 | } | |
1546 | elseif (preg_match('/^\xff\xd8/', $contents)) { | |
1547 | // 0 beshort 0xffd8 JPEG image data | |
1548 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); | |
1549 | } | |
1550 | else { | |
1551 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); | |
1552 | $contents = ""; | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | if ($contents) { | |
1557 | $fp = @fopen($icon_file, "w"); | |
1558 | ||
1559 | if ($fp) { | |
1560 | fwrite($fp, $contents); | |
1561 | fclose($fp); | |
1562 | chmod($icon_file, 0644); | |
1563 | } | |
1564 | } | |
1565 | } | |
1566 | return $icon_file; | |
1567 | } | |
1568 | } | |
e6c886bf AD |
1569 | |
1570 | ||
1571 | ||
1572 | } |