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