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