]>
Commit | Line | Data |
---|---|---|
2c08214a | 1 | <?php |
09e8bdfd AD |
2 | define('DAEMON_UPDATE_LOGIN_LIMIT', 30); |
3 | define('DAEMON_FEED_LIMIT', 100); | |
0f556e3e | 4 | define('DAEMON_SLEEP_INTERVAL', 60); |
09e8bdfd | 5 | |
79178062 AD |
6 | function update_feedbrowser_cache($link) { |
7 | ||
8 | $result = db_query($link, "SELECT feed_url, site_url, title, COUNT(id) AS subscribers | |
9 | FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf | |
10 | WHERE tf.feed_url = ttrss_feeds.feed_url | |
11 | AND (private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%')) | |
12 | GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000"); | |
13 | ||
14 | db_query($link, "BEGIN"); | |
15 | ||
16 | db_query($link, "DELETE FROM ttrss_feedbrowser_cache"); | |
17 | ||
18 | $count = 0; | |
19 | ||
20 | while ($line = db_fetch_assoc($result)) { | |
21 | $subscribers = db_escape_string($line["subscribers"]); | |
22 | $feed_url = db_escape_string($line["feed_url"]); | |
23 | $title = db_escape_string($line["title"]); | |
24 | $site_url = db_escape_string($line["site_url"]); | |
25 | ||
26 | $tmp_result = db_query($link, "SELECT subscribers FROM | |
27 | ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'"); | |
28 | ||
29 | if (db_num_rows($tmp_result) == 0) { | |
30 | ||
31 | db_query($link, "INSERT INTO ttrss_feedbrowser_cache | |
32 | (feed_url, site_url, title, subscribers) VALUES ('$feed_url', | |
33 | '$site_url', '$title', '$subscribers')"); | |
34 | ||
35 | ++$count; | |
36 | ||
37 | } | |
38 | ||
39 | } | |
40 | ||
41 | db_query($link, "COMMIT"); | |
42 | ||
43 | return $count; | |
44 | ||
45 | } | |
46 | ||
47 | ||
2c08214a AD |
48 | /** |
49 | * Update a feed batch. | |
50 | * Used by daemons to update n feeds by run. | |
51 | * Only update feed needing a update, and not being processed | |
52 | * by another process. | |
53 | * | |
54 | * @param mixed $link Database link | |
55 | * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT. | |
56 | * @param boolean $from_http Set to true if you call this function from http to disable cli specific code. | |
57 | * @param boolean $debug Set to false to disable debug output. Default to true. | |
58 | * @return void | |
59 | */ | |
60 | function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) { | |
61 | // Process all other feeds using last_updated and interval parameters | |
62 | ||
61c1812f AD |
63 | define('PREFS_NO_CACHE', true); |
64 | ||
2c08214a | 65 | // Test if the user has loggued in recently. If not, it does not update its feeds. |
09e8bdfd | 66 | if (!SINGLE_USER_MODE && DAEMON_UPDATE_LOGIN_LIMIT > 0) { |
2c08214a AD |
67 | if (DB_TYPE == "pgsql") { |
68 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
69 | } else { | |
70 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
71 | } | |
72 | } else { | |
73 | $login_thresh_qpart = ""; | |
74 | } | |
75 | ||
76 | // Test if the feed need a update (update interval exceded). | |
77 | if (DB_TYPE == "pgsql") { | |
78 | $update_limit_qpart = "AND (( | |
79 | ttrss_feeds.update_interval = 0 | |
80 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL) | |
81 | ) OR ( | |
82 | ttrss_feeds.update_interval > 0 | |
83 | AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL) | |
941e48a4 AD |
84 | ) OR ttrss_feeds.last_updated IS NULL |
85 | OR last_updated = '1970-01-01 00:00:00')"; | |
2c08214a AD |
86 | } else { |
87 | $update_limit_qpart = "AND (( | |
88 | ttrss_feeds.update_interval = 0 | |
89 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE) | |
90 | ) OR ( | |
91 | ttrss_feeds.update_interval > 0 | |
92 | AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE) | |
941e48a4 AD |
93 | ) OR ttrss_feeds.last_updated IS NULL |
94 | OR last_updated = '1970-01-01 00:00:00')"; | |
2c08214a AD |
95 | } |
96 | ||
97 | // Test if feed is currently being updated by another process. | |
98 | if (DB_TYPE == "pgsql") { | |
99 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')"; | |
100 | } else { | |
101 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))"; | |
102 | } | |
103 | ||
104 | // Test if there is a limit to number of updated feeds | |
105 | $query_limit = ""; | |
106 | if($limit) $query_limit = sprintf("LIMIT %d", $limit); | |
107 | ||
108 | $random_qpart = sql_random_function(); | |
109 | ||
110 | // We search for feed needing update. | |
111 | $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid, | |
112 | ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, | |
113 | ttrss_feeds.update_interval | |
114 | FROM | |
115 | ttrss_feeds, ttrss_users, ttrss_user_prefs | |
116 | WHERE | |
117 | ttrss_feeds.owner_uid = ttrss_users.id | |
118 | AND ttrss_users.id = ttrss_user_prefs.owner_uid | |
119 | AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' | |
120 | $login_thresh_qpart $update_limit_qpart | |
121 | $updstart_thresh_qpart | |
122 | ORDER BY $random_qpart $query_limit"); | |
123 | ||
124 | $user_prefs_cache = array(); | |
125 | ||
126 | if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result))); | |
127 | ||
128 | // Here is a little cache magic in order to minimize risk of double feed updates. | |
129 | $feeds_to_update = array(); | |
130 | while ($line = db_fetch_assoc($result)) { | |
131 | $feeds_to_update[$line['id']] = $line; | |
132 | } | |
133 | ||
134 | // We update the feed last update started date before anything else. | |
135 | // There is no lag due to feed contents downloads | |
136 | // It prevent an other process to update the same feed. | |
137 | $feed_ids = array_keys($feeds_to_update); | |
138 | if($feed_ids) { | |
139 | db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW() | |
140 | WHERE id IN (%s)", implode(',', $feed_ids))); | |
141 | } | |
142 | ||
3c696512 | 143 | expire_cached_files($debug); |
2a91b6ff | 144 | expire_lock_files($debug); |
3c696512 | 145 | |
2c08214a AD |
146 | // For each feed, we call the feed update function. |
147 | while ($line = array_pop($feeds_to_update)) { | |
148 | ||
149 | if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); | |
150 | ||
151 | update_rss_feed($link, $line["id"], true); | |
152 | ||
153 | sleep(1); // prevent flood (FIXME make this an option?) | |
154 | } | |
155 | ||
f1611683 AD |
156 | require_once "digest.php"; |
157 | ||
2c08214a | 158 | // Send feed digests by email if needed. |
8437c066 | 159 | send_headlines_digests($link, $debug); |
2c08214a | 160 | |
2ea09bde AD |
161 | // Purge feeds with stale data OR not being updated for a while to keep DB size down |
162 | ||
163 | if ($debug) _debug("Purging inactive feeds..."); | |
164 | ||
165 | if (DB_TYPE == "pgsql") { | |
166 | $interval_qpart = "NOW() - INTERVAL '1 month'"; | |
167 | } else { | |
168 | $interval_qpart = "DATE_SUB(NOW(), INTERVAL 1 MONTH)"; | |
169 | } | |
170 | ||
171 | $result = db_query($link, "SELECT id, feed_url FROM ttrss_feeds WHERE | |
172 | (SELECT MAX(updated) FROM ttrss_entries, ttrss_user_entries WHERE | |
173 | ttrss_entries.id = ref_id AND | |
174 | ttrss_user_entries.feed_id = ttrss_feeds.id) < $interval_qpart OR | |
175 | last_updated < $interval_qpart"); | |
176 | ||
177 | $feeds_purged = 0; | |
178 | $articles_removed = 0; | |
179 | ||
180 | while ($line = db_fetch_assoc($result)) { | |
181 | $articles_removed += purge_feed($link, $line["id"], 0, false); | |
182 | ++$feeds_purged; | |
183 | } | |
184 | ||
185 | if ($debug && $articles_removed > 0) | |
186 | _debug(sprintf("Purged %d feeds (%d articles).", $feeds_purged, | |
187 | $articles_removed)); | |
188 | ||
2c08214a AD |
189 | } // function update_daemon_common |
190 | ||
87764a50 | 191 | // ignore_daemon is not used |
0e4a7d7a | 192 | function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false, |
2c08214a AD |
193 | $override_url = false) { |
194 | ||
195 | require_once "lib/simplepie/simplepie.inc"; | |
2c08214a | 196 | |
2c08214a AD |
197 | $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']; |
198 | ||
2c08214a AD |
199 | if ($debug_enabled) { |
200 | _debug("update_rss_feed: start"); | |
201 | } | |
202 | ||
87764a50 | 203 | $result = db_query($link, "SELECT id,update_interval,auth_login, |
0a3fd79b | 204 | feed_url,auth_pass,cache_images,last_updated, |
5321e775 | 205 | mark_unread_on_update, owner_uid, |
87764a50 AD |
206 | pubsub_state |
207 | FROM ttrss_feeds WHERE id = '$feed'"); | |
2c08214a AD |
208 | |
209 | if (db_num_rows($result) == 0) { | |
210 | if ($debug_enabled) { | |
211 | _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED"); | |
212 | } | |
213 | return false; | |
214 | } | |
215 | ||
2c08214a AD |
216 | $last_updated = db_fetch_result($result, 0, "last_updated"); |
217 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
218 | $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result, | |
219 | 0, "mark_unread_on_update")); | |
2c08214a AD |
220 | $pubsub_state = db_fetch_result($result, 0, "pubsub_state"); |
221 | ||
222 | db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW() | |
223 | WHERE id = '$feed'"); | |
224 | ||
225 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
226 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
227 | ||
2c08214a AD |
228 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
229 | $fetch_url = db_fetch_result($result, 0, "feed_url"); | |
230 | ||
2c08214a AD |
231 | $feed = db_escape_string($feed); |
232 | ||
017401dd | 233 | /* if ($auth_login && $auth_pass ){ |
2c08214a AD |
234 | $url_parts = array(); |
235 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
236 | ||
237 | if ($url_parts[1] && $url_parts[2]) { | |
238 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
239 | } | |
017401dd | 240 | } */ |
2c08214a AD |
241 | |
242 | if ($override_url) | |
243 | $fetch_url = $override_url; | |
244 | ||
245 | if ($debug_enabled) { | |
246 | _debug("update_rss_feed: fetching [$fetch_url]..."); | |
247 | } | |
248 | ||
0e4a7d7a AD |
249 | // Ignore cache if new feed or manual update. |
250 | $cache_age = (is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ? | |
251 | -1 : get_feed_update_interval($link, $feed) * 60; | |
2c08214a | 252 | |
19b3992b | 253 | $simplepie_cache_dir = CACHE_DIR . "/simplepie"; |
2c08214a | 254 | |
19b3992b AD |
255 | if (!is_dir($simplepie_cache_dir)) { |
256 | mkdir($simplepie_cache_dir); | |
257 | } | |
2c08214a | 258 | |
017401dd AD |
259 | $feed_data = fetch_file_contents($fetch_url, false, |
260 | $auth_login, $auth_pass, false, $no_cache ? 15 : 45); | |
261 | ||
262 | if (!$feed_data) { | |
263 | global $fetch_last_error; | |
264 | ||
265 | if ($debug_enabled) { | |
266 | _debug("update_rss_feed: unable to fetch: $fetch_last_error"); | |
267 | } | |
268 | ||
269 | db_query($link, | |
270 | "UPDATE ttrss_feeds SET last_error = '$fetch_last_error', | |
271 | last_updated = NOW() WHERE id = '$feed'"); | |
272 | ||
273 | return; | |
274 | } | |
275 | ||
276 | $pluginhost = new PluginHost($link); | |
ab457a9c AD |
277 | $user_plugins = get_pref($link, "_ENABLED_PLUGINS", $owner_uid); |
278 | ||
279 | $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL); | |
280 | $pluginhost->load($user_plugins, $pluginhost::KIND_USER, $owner_uid); | |
281 | $pluginhost->load_data(); | |
017401dd AD |
282 | |
283 | foreach ($pluginhost->get_hooks($pluginhost::HOOK_FEED_FETCHED) as $plugin) { | |
284 | $feed_data = $plugin->hook_feed_fetched($feed_data); | |
285 | } | |
286 | ||
5de51df7 AD |
287 | if ($debug_enabled) { |
288 | _debug("update_rss_feed: fetch done, parsing..."); | |
289 | } | |
290 | ||
19b3992b | 291 | $rss = new SimplePie(); |
5d56d100 AD |
292 | $rss->set_sanitize_class("SanitizeDummy"); |
293 | // simplepie ignores the above and creates default sanitizer anyway, | |
294 | // so let's override it... | |
295 | $rss->sanitize = new SanitizeDummy(); | |
19b3992b | 296 | $rss->set_output_encoding('UTF-8'); |
017401dd | 297 | $rss->set_raw_data($feed_data); |
2c08214a | 298 | |
19b3992b AD |
299 | if ($debug_enabled) { |
300 | _debug("feed update interval (sec): " . | |
301 | get_feed_update_interval($link, $feed)*60); | |
302 | } | |
2c08214a | 303 | |
19b3992b | 304 | $rss->enable_cache(!$no_cache); |
2c08214a | 305 | |
19b3992b AD |
306 | if (!$no_cache) { |
307 | $rss->set_cache_location($simplepie_cache_dir); | |
308 | $rss->set_cache_duration($cache_age); | |
2c08214a AD |
309 | } |
310 | ||
ada103f2 | 311 | @$rss->init(); |
19b3992b | 312 | |
2c08214a AD |
313 | // print_r($rss); |
314 | ||
2c08214a AD |
315 | $feed = db_escape_string($feed); |
316 | ||
19b3992b | 317 | if (!$rss->error()) { |
2c08214a | 318 | |
d2a421e3 | 319 | // We use local pluginhost here because we need to load different per-user feed plugins |
4412b877 AD |
320 | $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss); |
321 | ||
2c08214a AD |
322 | if ($debug_enabled) { |
323 | _debug("update_rss_feed: processing feed data..."); | |
324 | } | |
325 | ||
326 | // db_query($link, "BEGIN"); | |
327 | ||
382268c6 AD |
328 | if (DB_TYPE == "pgsql") { |
329 | $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'"; | |
330 | } else { | |
331 | $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)"; | |
332 | } | |
333 | ||
750cfcd2 | 334 | $result = db_query($link, "SELECT title,site_url,owner_uid, |
382268c6 AD |
335 | (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS |
336 | favicon_needs_check | |
2c08214a AD |
337 | FROM ttrss_feeds WHERE id = '$feed'"); |
338 | ||
339 | $registered_title = db_fetch_result($result, 0, "title"); | |
2c08214a | 340 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
382268c6 AD |
341 | $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0, |
342 | "favicon_needs_check")); | |
2c08214a AD |
343 | |
344 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
345 | ||
bb566dc2 | 346 | $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245)); |
2c08214a AD |
347 | |
348 | if ($debug_enabled) { | |
349 | _debug("update_rss_feed: checking favicon..."); | |
350 | } | |
351 | ||
f2798eb6 AD |
352 | if ($favicon_needs_check) { |
353 | check_feed_favicon($site_url, $feed, $link); | |
354 | ||
355 | db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW() | |
356 | WHERE id = '$feed'"); | |
357 | } | |
2c08214a AD |
358 | |
359 | if (!$registered_title || $registered_title == "[Unknown]") { | |
360 | ||
19b3992b | 361 | $feed_title = db_escape_string($rss->get_title()); |
2c08214a AD |
362 | |
363 | if ($debug_enabled) { | |
364 | _debug("update_rss_feed: registering title: $feed_title"); | |
365 | } | |
366 | ||
367 | db_query($link, "UPDATE ttrss_feeds SET | |
368 | title = '$feed_title' WHERE id = '$feed'"); | |
369 | } | |
370 | ||
0cf81637 | 371 | if ($site_url && $orig_site_url != $site_url) { |
2c08214a AD |
372 | db_query($link, "UPDATE ttrss_feeds SET |
373 | site_url = '$site_url' WHERE id = '$feed'"); | |
374 | } | |
375 | ||
2c08214a | 376 | if ($debug_enabled) { |
b24504b1 | 377 | _debug("update_rss_feed: loading filters & labels..."); |
2c08214a AD |
378 | } |
379 | ||
380 | $filters = load_filters($link, $feed, $owner_uid); | |
b24504b1 | 381 | $labels = get_all_labels($link, $owner_uid); |
2c08214a | 382 | |
67bd0b1f AD |
383 | if ($debug_enabled) { |
384 | //print_r($filters); | |
385 | _debug("update_rss_feed: " . count($filters) . " filters loaded."); | |
386 | } | |
2c08214a | 387 | |
19b3992b | 388 | $items = $rss->get_items(); |
2c08214a | 389 | |
19b3992b | 390 | if (!is_array($items)) { |
2c08214a | 391 | if ($debug_enabled) { |
19b3992b | 392 | _debug("update_rss_feed: no articles found."); |
2c08214a AD |
393 | } |
394 | ||
395 | db_query($link, "UPDATE ttrss_feeds | |
396 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
397 | ||
398 | return; // no articles | |
399 | } | |
400 | ||
401 | if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) { | |
402 | ||
403 | if ($debug_enabled) _debug("update_rss_feed: checking for PUSH hub..."); | |
404 | ||
405 | $feed_hub_url = false; | |
2c08214a | 406 | |
19b3992b | 407 | $links = $rss->get_links('hub'); |
2c08214a | 408 | |
19b3992b AD |
409 | if ($links && is_array($links)) { |
410 | foreach ($links as $l) { | |
411 | $feed_hub_url = $l; | |
412 | break; | |
2c08214a AD |
413 | } |
414 | } | |
415 | ||
416 | if ($debug_enabled) _debug("update_rss_feed: feed hub url: $feed_hub_url"); | |
417 | ||
418 | if ($feed_hub_url && function_exists('curl_init') && | |
419 | !ini_get("open_basedir")) { | |
420 | ||
421 | require_once 'lib/pubsubhubbub/subscriber.php'; | |
422 | ||
423 | $callback_url = get_self_url_prefix() . | |
424 | "/public.php?op=pubsub&id=$feed"; | |
425 | ||
426 | $s = new Subscriber($feed_hub_url, $callback_url); | |
427 | ||
428 | $rc = $s->subscribe($fetch_url); | |
429 | ||
430 | if ($debug_enabled) | |
431 | _debug("update_rss_feed: feed hub url found, subscribe request sent."); | |
432 | ||
433 | db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1 | |
434 | WHERE id = '$feed'"); | |
435 | } | |
436 | } | |
437 | ||
438 | if ($debug_enabled) { | |
439 | _debug("update_rss_feed: processing articles..."); | |
440 | } | |
441 | ||
19b3992b | 442 | foreach ($items as $item) { |
5d56d100 | 443 | if ($_REQUEST['xdebug'] == 3) { |
2c08214a AD |
444 | print_r($item); |
445 | } | |
446 | ||
19b3992b AD |
447 | $entry_guid = $item->get_id(); |
448 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
449 | if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); | |
2c08214a AD |
450 | |
451 | if ($debug_enabled) { | |
452 | _debug("update_rss_feed: guid $entry_guid"); | |
453 | } | |
454 | ||
455 | if (!$entry_guid) continue; | |
456 | ||
3a4c8973 AD |
457 | $entry_guid = "$owner_uid,$entry_guid"; |
458 | ||
2c08214a AD |
459 | $entry_timestamp = ""; |
460 | ||
19b3992b | 461 | $entry_timestamp = strtotime($item->get_date()); |
2c08214a | 462 | |
19b3992b | 463 | if ($entry_timestamp == -1 || !$entry_timestamp) { |
2c08214a AD |
464 | $entry_timestamp = time(); |
465 | $no_orig_date = 'true'; | |
466 | } else { | |
467 | $no_orig_date = 'false'; | |
468 | } | |
469 | ||
470 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
471 | ||
472 | if ($debug_enabled) { | |
473 | _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]"); | |
474 | } | |
475 | ||
19b3992b | 476 | $entry_title = $item->get_title(); |
1b35d30c | 477 | |
5d56d100 | 478 | $entry_link = rewrite_relative_url($site_url, $item->get_link()); |
2c08214a AD |
479 | |
480 | if ($debug_enabled) { | |
481 | _debug("update_rss_feed: title $entry_title"); | |
482 | _debug("update_rss_feed: link $entry_link"); | |
483 | } | |
484 | ||
485 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
486 | ||
19b3992b AD |
487 | $entry_content = $item->get_content(); |
488 | if (!$entry_content) $entry_content = $item->get_description(); | |
2c08214a AD |
489 | |
490 | if ($_REQUEST["xdebug"] == 2) { | |
491 | print "update_rss_feed: content: "; | |
487f0750 | 492 | print $entry_content; |
3c696512 | 493 | print "\n"; |
2c08214a AD |
494 | } |
495 | ||
19b3992b | 496 | $entry_comments = $item->data["comments"]; |
2c08214a | 497 | |
19b3992b AD |
498 | if ($item->get_author()) { |
499 | $entry_author_item = $item->get_author(); | |
500 | $entry_author = $entry_author_item->get_name(); | |
501 | if (!$entry_author) $entry_author = $entry_author_item->get_email(); | |
2c08214a | 502 | |
19b3992b | 503 | $entry_author = db_escape_string($entry_author); |
2c08214a AD |
504 | } |
505 | ||
bb566dc2 | 506 | $entry_guid = db_escape_string(mb_substr($entry_guid, 0, 245)); |
2c08214a | 507 | |
bb566dc2 AD |
508 | $entry_comments = db_escape_string(mb_substr($entry_comments, 0, 245)); |
509 | $entry_author = db_escape_string(mb_substr($entry_author, 0, 245)); | |
2c08214a | 510 | |
19b3992b | 511 | $num_comments = $item->get_item_tags('http://purl.org/rss/1.0/modules/slash/', 'comments'); |
83e6e313 | 512 | |
19b3992b AD |
513 | if (is_array($num_comments) && is_array($num_comments[0])) { |
514 | $num_comments = (int) $num_comments[0]["data"]; | |
2c08214a | 515 | } else { |
19b3992b | 516 | $num_comments = 0; |
2c08214a AD |
517 | } |
518 | ||
2c08214a | 519 | if ($debug_enabled) { |
83e6e313 | 520 | _debug("update_rss_feed: num_comments: $num_comments"); |
2c08214a AD |
521 | _debug("update_rss_feed: looking for tags [1]..."); |
522 | } | |
523 | ||
524 | // parse <category> entries into tags | |
525 | ||
526 | $additional_tags = array(); | |
527 | ||
19b3992b | 528 | $additional_tags_src = $item->get_categories(); |
2c08214a | 529 | |
19b3992b AD |
530 | if (is_array($additional_tags_src)) { |
531 | foreach ($additional_tags_src as $tobj) { | |
532 | array_push($additional_tags, $tobj->get_term()); | |
2c08214a | 533 | } |
19b3992b | 534 | } |
2c08214a | 535 | |
19b3992b AD |
536 | if ($debug_enabled) { |
537 | _debug("update_rss_feed: category tags:"); | |
538 | print_r($additional_tags); | |
2c08214a AD |
539 | } |
540 | ||
541 | if ($debug_enabled) { | |
542 | _debug("update_rss_feed: looking for tags [2]..."); | |
543 | } | |
544 | ||
fa6fbd36 | 545 | $entry_tags = array_unique($additional_tags); |
2c08214a AD |
546 | |
547 | for ($i = 0; $i < count($entry_tags); $i++) | |
548 | $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8'); | |
549 | ||
550 | if ($debug_enabled) { | |
6aff7845 AD |
551 | //_debug("update_rss_feed: unfiltered tags found:"); |
552 | //print_r($entry_tags); | |
2c08214a AD |
553 | } |
554 | ||
2c08214a AD |
555 | if ($debug_enabled) { |
556 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); | |
557 | } | |
558 | ||
cc85704f | 559 | // TODO: less memory-hungry implementation |
19c73507 | 560 | |
19b3992b AD |
561 | if ($debug_enabled) { |
562 | _debug("update_rss_feed: applying plugin filters.."); | |
563 | } | |
cc85704f | 564 | |
e02555c1 AD |
565 | // FIXME not sure if owner_uid is a good idea here, we may have a base entry without user entry (?) |
566 | $result = db_query($link, "SELECT plugin_data,title,content,link,tag_cache,author FROM ttrss_entries, ttrss_user_entries | |
567 | WHERE ref_id = id AND guid = '".db_escape_string($entry_guid)."' AND owner_uid = $owner_uid"); | |
b30abdad AD |
568 | |
569 | if (db_num_rows($result) != 0) { | |
570 | $entry_plugin_data = db_fetch_result($result, 0, "plugin_data"); | |
e02555c1 AD |
571 | $stored_article = array("title" => db_fetch_result($result, 0, "title"), |
572 | "content" => db_fetch_result($result, 0, "content"), | |
573 | "link" => db_fetch_result($result, 0, "link"), | |
574 | "tags" => explode(",", db_fetch_result($result, 0, "tag_cache")), | |
575 | "author" => db_fetch_result($result, 0, "author")); | |
b30abdad AD |
576 | } else { |
577 | $entry_plugin_data = ""; | |
e02555c1 | 578 | $stored_article = array(); |
b30abdad AD |
579 | } |
580 | ||
455b1401 | 581 | $article = array("owner_uid" => $owner_uid, // read only |
b30abdad | 582 | "guid" => $entry_guid, // read only |
19b3992b AD |
583 | "title" => $entry_title, |
584 | "content" => $entry_content, | |
585 | "link" => $entry_link, | |
586 | "tags" => $entry_tags, | |
b30abdad | 587 | "plugin_data" => $entry_plugin_data, |
e02555c1 AD |
588 | "author" => $entry_author, |
589 | "stored" => $stored_article); | |
cc85704f | 590 | |
19b3992b AD |
591 | foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) { |
592 | $article = $plugin->hook_article_filter($article); | |
cc85704f AD |
593 | } |
594 | ||
19b3992b | 595 | $entry_tags = $article["tags"]; |
b30abdad | 596 | $entry_guid = db_escape_string($entry_guid); |
1b35d30c AD |
597 | $entry_content = db_escape_string($article["content"], false); |
598 | $entry_title = db_escape_string($article["title"]); | |
599 | $entry_author = db_escape_string($article["author"]); | |
600 | $entry_link = db_escape_string($article["link"]); | |
b30abdad AD |
601 | $entry_plugin_data = db_escape_string($article["plugin_data"]); |
602 | ||
603 | if ($debug_enabled) { | |
604 | _debug("update_rss_feed: plugin data: $entry_plugin_data"); | |
605 | } | |
2bbd6994 | 606 | |
0a3fd79b AD |
607 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
608 | $entry_content = cache_images($entry_content, $site_url, $debug_enabled); | |
609 | ||
9e222305 | 610 | $content_hash = "SHA1:" . sha1($entry_content); |
cc85704f | 611 | |
2c08214a AD |
612 | db_query($link, "BEGIN"); |
613 | ||
9e222305 AD |
614 | $result = db_query($link, "SELECT id FROM ttrss_entries |
615 | WHERE guid = '$entry_guid'"); | |
616 | ||
2c08214a AD |
617 | if (db_num_rows($result) == 0) { |
618 | ||
619 | if ($debug_enabled) { | |
9e222305 | 620 | _debug("update_rss_feed: base guid [$entry_guid] not found"); |
2c08214a AD |
621 | } |
622 | ||
623 | // base post entry does not exist, create it | |
624 | ||
625 | $result = db_query($link, | |
626 | "INSERT INTO ttrss_entries | |
627 | (title, | |
628 | guid, | |
629 | link, | |
630 | updated, | |
631 | content, | |
632 | content_hash, | |
87764a50 | 633 | cached_content, |
2c08214a AD |
634 | no_orig_date, |
635 | date_updated, | |
636 | date_entered, | |
637 | comments, | |
638 | num_comments, | |
b30abdad | 639 | plugin_data, |
2c08214a AD |
640 | author) |
641 | VALUES | |
642 | ('$entry_title', | |
643 | '$entry_guid', | |
644 | '$entry_link', | |
645 | '$entry_timestamp_fmt', | |
646 | '$entry_content', | |
647 | '$content_hash', | |
0a3fd79b | 648 | '', |
2c08214a AD |
649 | $no_orig_date, |
650 | NOW(), | |
651 | NOW(), | |
652 | '$entry_comments', | |
653 | '$num_comments', | |
b30abdad | 654 | '$entry_plugin_data', |
2c08214a | 655 | '$entry_author')"); |
e8291805 AD |
656 | |
657 | $article_labels = array(); | |
658 | ||
2c08214a AD |
659 | } else { |
660 | // we keep encountering the entry in feeds, so we need to | |
661 | // update date_updated column so that we don't get horrible | |
662 | // dupes when the entry gets purged and reinserted again e.g. | |
663 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
664 | ||
665 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
666 | ||
667 | db_query($link, "UPDATE ttrss_entries SET date_updated = NOW() | |
668 | WHERE id = '$base_entry_id'"); | |
e8291805 AD |
669 | |
670 | $article_labels = get_article_labels($link, $base_entry_id, $owner_uid); | |
2c08214a AD |
671 | } |
672 | ||
673 | // now it should exist, if not - bad luck then | |
674 | ||
675 | $result = db_query($link, "SELECT | |
b30abdad | 676 | id,content_hash,no_orig_date,title,plugin_data, |
2c08214a AD |
677 | ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated, |
678 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated, | |
0a3fd79b | 679 | num_comments |
2c08214a AD |
680 | FROM |
681 | ttrss_entries | |
682 | WHERE guid = '$entry_guid'"); | |
683 | ||
684 | $entry_ref_id = 0; | |
685 | $entry_int_id = 0; | |
686 | ||
687 | if (db_num_rows($result) == 1) { | |
688 | ||
689 | if ($debug_enabled) { | |
9e222305 | 690 | _debug("update_rss_feed: base guid [$entry_guid] found, checking for user record"); |
2c08214a AD |
691 | } |
692 | ||
693 | // this will be used below in update handler | |
694 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
695 | $orig_title = db_fetch_result($result, 0, "title"); | |
696 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
697 | $orig_date_updated = strtotime(db_fetch_result($result, | |
698 | 0, "date_updated")); | |
b30abdad | 699 | $orig_plugin_data = db_fetch_result($result, 0, "plugin_data"); |
2c08214a AD |
700 | |
701 | $ref_id = db_fetch_result($result, 0, "id"); | |
702 | $entry_ref_id = $ref_id; | |
703 | ||
704 | // check for user post link to main table | |
705 | ||
706 | // do we allow duplicate posts with same GUID in different feeds? | |
707 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { | |
708 | $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)"; | |
709 | } else { | |
710 | $dupcheck_qpart = ""; | |
711 | } | |
712 | ||
713 | /* Collect article tags here so we could filter by them: */ | |
714 | ||
715 | $article_filters = get_article_filters($filters, $entry_title, | |
4021d61a | 716 | $entry_content, $entry_link, $entry_timestamp, $entry_author, |
2c08214a AD |
717 | $entry_tags); |
718 | ||
719 | if ($debug_enabled) { | |
720 | _debug("update_rss_feed: article filters: "); | |
721 | if (count($article_filters) != 0) { | |
722 | print_r($article_filters); | |
723 | } | |
724 | } | |
725 | ||
726 | if (find_article_filter($article_filters, "filter")) { | |
727 | db_query($link, "COMMIT"); // close transaction in progress | |
728 | continue; | |
729 | } | |
730 | ||
731 | $score = calculate_article_score($article_filters); | |
732 | ||
733 | if ($debug_enabled) { | |
734 | _debug("update_rss_feed: initial score: $score"); | |
735 | } | |
736 | ||
737 | $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE | |
738 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
739 | $dupcheck_qpart"; | |
740 | ||
741 | // if ($_REQUEST["xdebug"]) print "$query\n"; | |
742 | ||
743 | $result = db_query($link, $query); | |
744 | ||
745 | // okay it doesn't exist - create user entry | |
746 | if (db_num_rows($result) == 0) { | |
747 | ||
748 | if ($debug_enabled) { | |
749 | _debug("update_rss_feed: user record not found, creating..."); | |
750 | } | |
751 | ||
752 | if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) { | |
753 | $unread = 'true'; | |
754 | $last_read_qpart = 'NULL'; | |
755 | } else { | |
756 | $unread = 'false'; | |
757 | $last_read_qpart = 'NOW()'; | |
758 | } | |
759 | ||
760 | if (find_article_filter($article_filters, 'mark') || $score > 1000) { | |
761 | $marked = 'true'; | |
762 | } else { | |
763 | $marked = 'false'; | |
764 | } | |
765 | ||
766 | if (find_article_filter($article_filters, 'publish')) { | |
767 | $published = 'true'; | |
768 | } else { | |
769 | $published = 'false'; | |
770 | } | |
771 | ||
2ea9bbfd AD |
772 | // N-grams |
773 | ||
774 | if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) { | |
775 | ||
776 | $result = db_query($link, "SELECT COUNT(*) AS similar FROM | |
777 | ttrss_entries,ttrss_user_entries | |
778 | WHERE ref_id = id AND updated >= NOW() - INTERVAL '7 day' | |
779 | AND similarity(title, '$entry_title') >= "._NGRAM_TITLE_DUPLICATE_THRESHOLD." | |
780 | AND owner_uid = $owner_uid"); | |
781 | ||
782 | $ngram_similar = db_fetch_result($result, 0, "similar"); | |
783 | ||
784 | if ($debug_enabled) { | |
785 | _debug("update_rss_feed: N-gram similar results: $ngram_similar"); | |
786 | } | |
787 | ||
788 | if ($ngram_similar > 0) { | |
789 | $unread = 'false'; | |
790 | } | |
791 | } | |
792 | ||
2c08214a AD |
793 | $result = db_query($link, |
794 | "INSERT INTO ttrss_user_entries | |
795 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
796 | published, score, tag_cache, label_cache, uuid) | |
797 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
798 | $last_read_qpart, $marked, $published, '$score', '', '', '')"); | |
799 | ||
800 | if (PUBSUBHUBBUB_HUB && $published == 'true') { | |
801 | $rss_link = get_self_url_prefix() . | |
802 | "/public.php?op=rss&id=-2&key=" . | |
803 | get_feed_access_key($link, -2, false, $owner_uid); | |
804 | ||
805 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
806 | ||
807 | $pubsub_result = $p->publish_update($rss_link); | |
808 | } | |
809 | ||
810 | $result = db_query($link, | |
811 | "SELECT int_id FROM ttrss_user_entries WHERE | |
812 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
813 | feed_id = '$feed' LIMIT 1"); | |
814 | ||
815 | if (db_num_rows($result) == 1) { | |
816 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
817 | } | |
818 | } else { | |
819 | if ($debug_enabled) { | |
820 | _debug("update_rss_feed: user record FOUND"); | |
821 | } | |
822 | ||
823 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
824 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
825 | } | |
826 | ||
827 | if ($debug_enabled) { | |
828 | _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id"); | |
829 | } | |
830 | ||
831 | $post_needs_update = false; | |
832 | $update_insignificant = false; | |
833 | ||
834 | if ($orig_num_comments != $num_comments) { | |
835 | $post_needs_update = true; | |
836 | $update_insignificant = true; | |
837 | } | |
838 | ||
b30abdad AD |
839 | if ($entry_plugin_data != $orig_plugin_data) { |
840 | $post_needs_update = true; | |
841 | $update_insignificant = true; | |
842 | } | |
843 | ||
2c08214a AD |
844 | if ($content_hash != $orig_content_hash) { |
845 | $post_needs_update = true; | |
846 | $update_insignificant = false; | |
847 | } | |
848 | ||
849 | if (db_escape_string($orig_title) != $entry_title) { | |
850 | $post_needs_update = true; | |
851 | $update_insignificant = false; | |
852 | } | |
853 | ||
854 | // if post needs update, update it and mark all user entries | |
855 | // linking to this post as updated | |
856 | if ($post_needs_update) { | |
857 | ||
858 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
859 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
860 | } | |
861 | ||
862 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
863 | ||
864 | db_query($link, "UPDATE ttrss_entries | |
865 | SET title = '$entry_title', content = '$entry_content', | |
866 | content_hash = '$content_hash', | |
867 | updated = '$entry_timestamp_fmt', | |
b30abdad AD |
868 | num_comments = '$num_comments', |
869 | plugin_data = '$entry_plugin_data' | |
2c08214a AD |
870 | WHERE id = '$ref_id'"); |
871 | ||
872 | if (!$update_insignificant) { | |
873 | if ($mark_unread_on_update) { | |
874 | db_query($link, "UPDATE ttrss_user_entries | |
875 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
2c08214a AD |
876 | } |
877 | } | |
878 | } | |
879 | } | |
880 | ||
881 | db_query($link, "COMMIT"); | |
882 | ||
883 | if ($debug_enabled) { | |
884 | _debug("update_rss_feed: assigning labels..."); | |
885 | } | |
886 | ||
92c14e9d | 887 | assign_article_to_label_filters($link, $entry_ref_id, $article_filters, |
b24504b1 | 888 | $owner_uid, $article_labels); |
2c08214a AD |
889 | |
890 | if ($debug_enabled) { | |
891 | _debug("update_rss_feed: looking for enclosures..."); | |
892 | } | |
893 | ||
894 | // enclosures | |
895 | ||
896 | $enclosures = array(); | |
897 | ||
19b3992b | 898 | $encs = $item->get_enclosures(); |
2c08214a | 899 | |
19b3992b AD |
900 | if (is_array($encs)) { |
901 | foreach ($encs as $e) { | |
902 | $e_item = array( | |
903 | $e->link, $e->type, $e->length); | |
2c08214a | 904 | array_push($enclosures, $e_item); |
2c08214a AD |
905 | } |
906 | } | |
907 | ||
2c08214a AD |
908 | if ($debug_enabled) { |
909 | _debug("update_rss_feed: article enclosures:"); | |
910 | print_r($enclosures); | |
911 | } | |
912 | ||
913 | db_query($link, "BEGIN"); | |
914 | ||
915 | foreach ($enclosures as $enc) { | |
916 | $enc_url = db_escape_string($enc[0]); | |
917 | $enc_type = db_escape_string($enc[1]); | |
918 | $enc_dur = db_escape_string($enc[2]); | |
919 | ||
920 | $result = db_query($link, "SELECT id FROM ttrss_enclosures | |
921 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); | |
922 | ||
923 | if (db_num_rows($result) == 0) { | |
924 | db_query($link, "INSERT INTO ttrss_enclosures | |
925 | (content_url, content_type, title, duration, post_id) VALUES | |
926 | ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')"); | |
927 | } | |
928 | } | |
929 | ||
930 | db_query($link, "COMMIT"); | |
931 | ||
932 | // check for manual tags (we have to do it here since they're loaded from filters) | |
933 | ||
934 | foreach ($article_filters as $f) { | |
6aff7845 | 935 | if ($f["type"] == "tag") { |
2c08214a | 936 | |
6aff7845 | 937 | $manual_tags = trim_array(explode(",", $f["param"])); |
2c08214a AD |
938 | |
939 | foreach ($manual_tags as $tag) { | |
940 | if (tag_is_valid($tag)) { | |
941 | array_push($entry_tags, $tag); | |
942 | } | |
943 | } | |
944 | } | |
945 | } | |
946 | ||
947 | // Skip boring tags | |
948 | ||
949 | $boring_tags = trim_array(explode(",", mb_strtolower(get_pref($link, | |
950 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
951 | ||
952 | $filtered_tags = array(); | |
953 | $tags_to_cache = array(); | |
954 | ||
955 | if ($entry_tags && is_array($entry_tags)) { | |
956 | foreach ($entry_tags as $tag) { | |
957 | if (array_search($tag, $boring_tags) === false) { | |
958 | array_push($filtered_tags, $tag); | |
959 | } | |
960 | } | |
961 | } | |
962 | ||
963 | $filtered_tags = array_unique($filtered_tags); | |
964 | ||
965 | if ($debug_enabled) { | |
966 | _debug("update_rss_feed: filtered article tags:"); | |
967 | print_r($filtered_tags); | |
968 | } | |
969 | ||
970 | // Save article tags in the database | |
971 | ||
972 | if (count($filtered_tags) > 0) { | |
973 | ||
974 | db_query($link, "BEGIN"); | |
975 | ||
976 | foreach ($filtered_tags as $tag) { | |
977 | ||
978 | $tag = sanitize_tag($tag); | |
979 | $tag = db_escape_string($tag); | |
980 | ||
981 | if (!tag_is_valid($tag)) continue; | |
982 | ||
983 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
984 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
985 | owner_uid = '$owner_uid' LIMIT 1"); | |
986 | ||
987 | if ($result && db_num_rows($result) == 0) { | |
988 | ||
989 | db_query($link, "INSERT INTO ttrss_tags | |
990 | (owner_uid,tag_name,post_int_id) | |
991 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
992 | } | |
993 | ||
994 | array_push($tags_to_cache, $tag); | |
995 | } | |
996 | ||
997 | /* update the cache */ | |
998 | ||
999 | $tags_to_cache = array_unique($tags_to_cache); | |
1000 | ||
1001 | $tags_str = db_escape_string(join(",", $tags_to_cache)); | |
1002 | ||
1003 | db_query($link, "UPDATE ttrss_user_entries | |
1004 | SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id' | |
1005 | AND owner_uid = $owner_uid"); | |
1006 | ||
1007 | db_query($link, "COMMIT"); | |
1008 | } | |
1009 | ||
b24504b1 AD |
1010 | if (get_pref($link, "AUTO_ASSIGN_LABELS", $owner_uid, false)) { |
1011 | if ($debug_enabled) { | |
1012 | _debug("update_rss_feed: auto-assigning labels..."); | |
1013 | } | |
1014 | ||
1015 | foreach ($labels as $label) { | |
1016 | $caption = $label["caption"]; | |
1017 | ||
3fc6e71a | 1018 | if (preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) { |
b24504b1 AD |
1019 | if (!labels_contains_caption($article_labels, $caption)) { |
1020 | label_add_article($link, $entry_ref_id, $caption, $owner_uid); | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
2c08214a AD |
1026 | if ($debug_enabled) { |
1027 | _debug("update_rss_feed: article processed"); | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | if (!$last_updated) { | |
1032 | if ($debug_enabled) { | |
1033 | _debug("update_rss_feed: new feed, catching it up..."); | |
1034 | } | |
1035 | catchup_feed($link, $feed, false, $owner_uid); | |
1036 | } | |
1037 | ||
1038 | if ($debug_enabled) { | |
1039 | _debug("purging feed..."); | |
1040 | } | |
1041 | ||
1042 | purge_feed($link, $feed, 0, $debug_enabled); | |
1043 | ||
1044 | db_query($link, "UPDATE ttrss_feeds | |
f2798eb6 | 1045 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
2c08214a AD |
1046 | |
1047 | // db_query($link, "COMMIT"); | |
1048 | ||
1049 | } else { | |
1050 | ||
bb566dc2 | 1051 | $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245)); |
2c08214a AD |
1052 | |
1053 | if ($debug_enabled) { | |
1054 | _debug("update_rss_feed: error fetching feed: $error_msg"); | |
1055 | } | |
1056 | ||
2c08214a AD |
1057 | db_query($link, |
1058 | "UPDATE ttrss_feeds SET last_error = '$error_msg', | |
1059 | last_updated = NOW() WHERE id = '$feed'"); | |
1060 | } | |
1061 | ||
19b3992b | 1062 | unset($rss); |
2c08214a AD |
1063 | |
1064 | if ($debug_enabled) { | |
1065 | _debug("update_rss_feed: done"); | |
1066 | } | |
1067 | ||
1068 | } | |
1069 | ||
3c696512 AD |
1070 | function cache_images($html, $site_url, $debug) { |
1071 | $cache_dir = CACHE_DIR . "/images"; | |
1072 | ||
1073 | libxml_use_internal_errors(true); | |
1074 | ||
1075 | $charset_hack = '<head> | |
1076 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
1077 | </head>'; | |
1078 | ||
1079 | $doc = new DOMDocument(); | |
1080 | $doc->loadHTML($charset_hack . $html); | |
1081 | $xpath = new DOMXPath($doc); | |
1082 | ||
1083 | $entries = $xpath->query('(//img[@src])'); | |
1084 | ||
1085 | foreach ($entries as $entry) { | |
1086 | if ($entry->hasAttribute('src')) { | |
1087 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
1088 | ||
1089 | $local_filename = CACHE_DIR . "/images/" . sha1($src) . ".png"; | |
1090 | ||
1091 | if ($debug) _debug("cache_images: downloading: $src to $local_filename"); | |
1092 | ||
1093 | if (!file_exists($local_filename)) { | |
1094 | $file_content = fetch_file_contents($src); | |
1095 | ||
b8379e69 | 1096 | if ($file_content && strlen($file_content) > 1024) { |
3c696512 AD |
1097 | file_put_contents($local_filename, $file_content); |
1098 | } | |
1099 | } | |
1100 | ||
1101 | if (file_exists($local_filename)) { | |
1102 | $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' . | |
487f0750 | 1103 | base64_encode($src)); |
3c696512 AD |
1104 | } |
1105 | } | |
1106 | } | |
1107 | ||
1108 | $node = $doc->getElementsByTagName('body')->item(0); | |
1109 | ||
7b8ff151 | 1110 | return $doc->saveXML($node, LIBXML_NOEMPTYTAG); |
3c696512 AD |
1111 | } |
1112 | ||
2a91b6ff AD |
1113 | function expire_lock_files($debug) { |
1114 | if ($debug) _debug("Removing old lock files..."); | |
1115 | ||
1116 | $num_deleted = 0; | |
1117 | ||
1118 | if (is_writable(LOCK_DIRECTORY)) { | |
1119 | $files = glob(LOCK_DIRECTORY . "/*.lock"); | |
1120 | ||
1121 | if ($files) { | |
1122 | foreach ($files as $file) { | |
1123 | if (!file_is_locked($file) && time() - filemtime($file) > 86400*2) { | |
1124 | unlink($file); | |
1125 | ++$num_deleted; | |
1126 | } | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | if ($debug) _debug("Removed $num_deleted files."); | |
1132 | } | |
1133 | ||
3c696512 | 1134 | function expire_cached_files($debug) { |
19b3992b | 1135 | foreach (array("simplepie", "images", "export") as $dir) { |
3c696512 | 1136 | $cache_dir = CACHE_DIR . "/$dir"; |
2c08214a | 1137 | |
3c696512 | 1138 | if ($debug) _debug("Expiring $cache_dir"); |
2c08214a | 1139 | |
3c696512 AD |
1140 | $num_deleted = 0; |
1141 | ||
1142 | if (is_writable($cache_dir)) { | |
1143 | $files = glob("$cache_dir/*"); | |
1144 | ||
2a91b6ff | 1145 | if ($files) { |
2ab20c31 AD |
1146 | foreach ($files as $file) { |
1147 | if (time() - filemtime($file) > 86400*7) { | |
1148 | unlink($file); | |
3c696512 | 1149 | |
2ab20c31 AD |
1150 | ++$num_deleted; |
1151 | } | |
3c696512 AD |
1152 | } |
1153 | } | |
2a91b6ff | 1154 | } |
3c696512 AD |
1155 | |
1156 | if ($debug) _debug("Removed $num_deleted files."); | |
1157 | } | |
1158 | } | |
2c08214a | 1159 | |
a3e0bdcf AD |
1160 | /** |
1161 | * Source: http://www.php.net/manual/en/function.parse-url.php#104527 | |
1162 | * Returns the url query as associative array | |
1163 | * | |
1164 | * @param string query | |
1165 | * @return array params | |
1166 | */ | |
1167 | function convertUrlQuery($query) { | |
1168 | $queryParts = explode('&', $query); | |
1169 | ||
1170 | $params = array(); | |
1171 | ||
1172 | foreach ($queryParts as $param) { | |
1173 | $item = explode('=', $param); | |
1174 | $params[$item[0]] = $item[1]; | |
1175 | } | |
1176 | ||
1177 | return $params; | |
1178 | } | |
92c14e9d AD |
1179 | |
1180 | function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) { | |
1181 | $matches = array(); | |
1182 | ||
1183 | foreach ($filters as $filter) { | |
1184 | $match_any_rule = $filter["match_any_rule"]; | |
1185 | $filter_match = false; | |
1186 | ||
1187 | foreach ($filter["rules"] as $rule) { | |
1188 | $match = false; | |
1189 | $reg_exp = $rule["reg_exp"]; | |
1190 | ||
1191 | if (!$reg_exp) | |
1192 | continue; | |
1193 | ||
1194 | switch ($rule["type"]) { | |
1195 | case "title": | |
1196 | $match = @preg_match("/$reg_exp/i", $title); | |
1197 | break; | |
1198 | case "content": | |
d03ae73e AD |
1199 | // we don't need to deal with multiline regexps |
1200 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
1201 | ||
92c14e9d AD |
1202 | $match = @preg_match("/$reg_exp/i", $content); |
1203 | break; | |
1204 | case "both": | |
d03ae73e AD |
1205 | // we don't need to deal with multiline regexps |
1206 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
1207 | ||
72d1d067 | 1208 | $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $content)); |
92c14e9d AD |
1209 | break; |
1210 | case "link": | |
1211 | $match = @preg_match("/$reg_exp/i", $link); | |
1212 | break; | |
1213 | case "author": | |
1214 | $match = @preg_match("/$reg_exp/i", $author); | |
1215 | break; | |
1216 | case "tag": | |
1217 | $tag_string = join(",", $tags); | |
1218 | $match = @preg_match("/$reg_exp/i", $tag_string); | |
1219 | break; | |
1220 | } | |
1221 | ||
1222 | if ($match_any_rule) { | |
1223 | if ($match) { | |
1224 | $filter_match = true; | |
1225 | break; | |
1226 | } | |
1227 | } else { | |
1228 | $filter_match = $match; | |
1229 | if (!$match) { | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | if ($filter_match) { | |
1236 | foreach ($filter["actions"] AS $action) { | |
1237 | array_push($matches, $action); | |
1238 | } | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | return $matches; | |
1243 | } | |
1244 | ||
1245 | function find_article_filter($filters, $filter_name) { | |
1246 | foreach ($filters as $f) { | |
1247 | if ($f["type"] == $filter_name) { | |
1248 | return $f; | |
1249 | }; | |
1250 | } | |
1251 | return false; | |
1252 | } | |
1253 | ||
1254 | function find_article_filters($filters, $filter_name) { | |
1255 | $results = array(); | |
1256 | ||
1257 | foreach ($filters as $f) { | |
1258 | if ($f["type"] == $filter_name) { | |
1259 | array_push($results, $f); | |
1260 | }; | |
1261 | } | |
1262 | return $results; | |
1263 | } | |
1264 | ||
1265 | function calculate_article_score($filters) { | |
1266 | $score = 0; | |
1267 | ||
1268 | foreach ($filters as $f) { | |
1269 | if ($f["type"] == "score") { | |
1270 | $score += $f["param"]; | |
1271 | }; | |
1272 | } | |
1273 | return $score; | |
1274 | } | |
1275 | ||
b24504b1 AD |
1276 | function labels_contains_caption($labels, $caption) { |
1277 | foreach ($labels as $label) { | |
1278 | if ($label[1] == $caption) { | |
1279 | return true; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | return false; | |
1284 | } | |
1285 | ||
1286 | function assign_article_to_label_filters($link, $id, $filters, $owner_uid, $article_labels) { | |
92c14e9d AD |
1287 | foreach ($filters as $f) { |
1288 | if ($f["type"] == "label") { | |
b24504b1 AD |
1289 | if (!labels_contains_caption($article_labels, $f["param"])) { |
1290 | label_add_article($link, $id, $f["param"], $owner_uid); | |
1291 | } | |
1292 | } | |
92c14e9d AD |
1293 | } |
1294 | } | |
87764a50 AD |
1295 | |
1296 | function cache_content($link, $url, $login, $pass) { | |
1297 | ||
1298 | $content = fetch_file_contents($url, $login, $pass); | |
1299 | ||
1300 | if ($content) { | |
1301 | $doc = new DOMDocument(); | |
1302 | @$doc->loadHTML($content); | |
1303 | $xpath = new DOMXPath($doc); | |
1304 | ||
1305 | $node = $doc->getElementsByTagName('body')->item(0); | |
1306 | ||
1307 | if ($node) { | |
1308 | $content = $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
1309 | ||
1310 | return $content; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | return ""; | |
1315 | } | |
87d7e850 AD |
1316 | |
1317 | function make_guid_from_title($title) { | |
1318 | return preg_replace("/[ \"\',.:;]/", "-", | |
1319 | mb_strtolower(strip_tags($title), 'utf-8')); | |
1320 | } | |
1321 | ||
1322 | ||
2c08214a | 1323 | ?> |