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