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