]>
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 AD |
143 | expire_cached_files($debug); |
144 | ||
2c08214a AD |
145 | // For each feed, we call the feed update function. |
146 | while ($line = array_pop($feeds_to_update)) { | |
147 | ||
148 | if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); | |
149 | ||
150 | update_rss_feed($link, $line["id"], true); | |
151 | ||
152 | sleep(1); // prevent flood (FIXME make this an option?) | |
153 | } | |
154 | ||
155 | // Send feed digests by email if needed. | |
8437c066 | 156 | send_headlines_digests($link, $debug); |
2c08214a AD |
157 | |
158 | } // function update_daemon_common | |
159 | ||
87764a50 | 160 | // ignore_daemon is not used |
0e4a7d7a | 161 | function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false, |
2c08214a AD |
162 | $override_url = false) { |
163 | ||
164 | require_once "lib/simplepie/simplepie.inc"; | |
165 | require_once "lib/magpierss/rss_fetch.inc"; | |
166 | require_once 'lib/magpierss/rss_utils.inc'; | |
167 | ||
2c08214a AD |
168 | $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug']; |
169 | ||
2c08214a AD |
170 | if ($debug_enabled) { |
171 | _debug("update_rss_feed: start"); | |
172 | } | |
173 | ||
87764a50 AD |
174 | $result = db_query($link, "SELECT id,update_interval,auth_login, |
175 | feed_url,auth_pass,cache_images,update_method,last_updated,cache_content, | |
176 | mark_unread_on_update, owner_uid, update_on_checksum_change, | |
177 | pubsub_state | |
178 | FROM ttrss_feeds WHERE id = '$feed'"); | |
2c08214a AD |
179 | |
180 | if (db_num_rows($result) == 0) { | |
181 | if ($debug_enabled) { | |
182 | _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED"); | |
183 | } | |
184 | return false; | |
185 | } | |
186 | ||
187 | $update_method = db_fetch_result($result, 0, "update_method"); | |
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")); | |
192 | $update_on_checksum_change = sql_bool_to_bool(db_fetch_result($result, | |
193 | 0, "update_on_checksum_change")); | |
194 | $pubsub_state = db_fetch_result($result, 0, "pubsub_state"); | |
195 | ||
196 | db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW() | |
197 | WHERE id = '$feed'"); | |
198 | ||
199 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
200 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
201 | ||
202 | if ($update_method == 0) | |
203 | $update_method = DEFAULT_UPDATE_METHOD + 1; | |
204 | ||
205 | // 1 - Magpie | |
206 | // 2 - SimplePie | |
207 | // 3 - Twitter OAuth | |
208 | ||
209 | if ($update_method == 2) | |
210 | $use_simplepie = true; | |
211 | else | |
212 | $use_simplepie = false; | |
213 | ||
214 | if ($debug_enabled) { | |
215 | _debug("update method: $update_method (feed setting: $update_method) (use simplepie: $use_simplepie)\n"); | |
216 | } | |
217 | ||
218 | if ($update_method == 1) { | |
219 | $auth_login = urlencode($auth_login); | |
220 | $auth_pass = urlencode($auth_pass); | |
221 | } | |
222 | ||
2c08214a | 223 | $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images")); |
87764a50 | 224 | $cache_content = sql_bool_to_bool(db_fetch_result($result, 0, "cache_content")); |
2c08214a AD |
225 | $fetch_url = db_fetch_result($result, 0, "feed_url"); |
226 | ||
2c08214a AD |
227 | $feed = db_escape_string($feed); |
228 | ||
229 | if ($auth_login && $auth_pass ){ | |
230 | $url_parts = array(); | |
231 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
232 | ||
233 | if ($url_parts[1] && $url_parts[2]) { | |
234 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
235 | } | |
236 | ||
237 | } | |
238 | ||
239 | if ($override_url) | |
240 | $fetch_url = $override_url; | |
241 | ||
242 | if ($debug_enabled) { | |
243 | _debug("update_rss_feed: fetching [$fetch_url]..."); | |
244 | } | |
245 | ||
0e4a7d7a AD |
246 | // Ignore cache if new feed or manual update. |
247 | $cache_age = (is_null($last_updated) || $last_updated == '1970-01-01 00:00:00') ? | |
248 | -1 : get_feed_update_interval($link, $feed) * 60; | |
2c08214a | 249 | |
304aadb9 | 250 | if ($update_method == 1) { |
2c08214a | 251 | |
0e4a7d7a AD |
252 | define('MAGPIE_CACHE_AGE', $cache_age); |
253 | define('MAGPIE_CACHE_ON', !$no_cache); | |
c0c2abba | 254 | define('MAGPIE_FETCH_TIME_OUT', $no_cache ? 15 : 60); |
0e4a7d7a | 255 | define('MAGPIE_CACHE_DIR', CACHE_DIR . "/magpie"); |
2c08214a | 256 | |
0e4a7d7a | 257 | $rss = @fetch_rss($fetch_url); |
2c08214a | 258 | } else { |
0e4a7d7a | 259 | $simplepie_cache_dir = CACHE_DIR . "/simplepie"; |
2c08214a | 260 | |
0e4a7d7a AD |
261 | if (!is_dir($simplepie_cache_dir)) { |
262 | mkdir($simplepie_cache_dir); | |
263 | } | |
2c08214a | 264 | |
0e4a7d7a AD |
265 | $rss = new SimplePie(); |
266 | $rss->set_useragent(SELF_USER_AGENT); | |
c0c2abba | 267 | $rss->set_timeout($no_cache ? 15 : 60); |
0e4a7d7a AD |
268 | $rss->set_feed_url($fetch_url); |
269 | $rss->set_output_encoding('UTF-8'); | |
270 | //$rss->force_feed(true); | |
2c08214a | 271 | |
0e4a7d7a AD |
272 | if ($debug_enabled) { |
273 | _debug("feed update interval (sec): " . | |
274 | get_feed_update_interval($link, $feed)*60); | |
275 | } | |
2c08214a | 276 | |
0e4a7d7a | 277 | $rss->enable_cache(!$no_cache); |
2c08214a | 278 | |
0e4a7d7a AD |
279 | if (!$no_cache) { |
280 | $rss->set_cache_location($simplepie_cache_dir); | |
281 | $rss->set_cache_duration($cache_age); | |
2c08214a AD |
282 | } |
283 | ||
0e4a7d7a | 284 | $rss->init(); |
2c08214a AD |
285 | } |
286 | ||
287 | // print_r($rss); | |
288 | ||
289 | if ($debug_enabled) { | |
290 | _debug("update_rss_feed: fetch done, parsing..."); | |
291 | } | |
292 | ||
293 | $feed = db_escape_string($feed); | |
294 | ||
295 | if ($update_method == 2) { | |
296 | $fetch_ok = !$rss->error(); | |
297 | } else { | |
298 | $fetch_ok = !!$rss; | |
299 | } | |
300 | ||
301 | if ($fetch_ok) { | |
302 | ||
303 | if ($debug_enabled) { | |
304 | _debug("update_rss_feed: processing feed data..."); | |
305 | } | |
306 | ||
307 | // db_query($link, "BEGIN"); | |
308 | ||
382268c6 AD |
309 | if (DB_TYPE == "pgsql") { |
310 | $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'"; | |
311 | } else { | |
312 | $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)"; | |
313 | } | |
314 | ||
315 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid, | |
316 | (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS | |
317 | favicon_needs_check | |
2c08214a AD |
318 | FROM ttrss_feeds WHERE id = '$feed'"); |
319 | ||
320 | $registered_title = db_fetch_result($result, 0, "title"); | |
321 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
322 | $orig_site_url = db_fetch_result($result, 0, "site_url"); | |
382268c6 AD |
323 | $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0, |
324 | "favicon_needs_check")); | |
2c08214a AD |
325 | |
326 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
327 | ||
328 | if ($use_simplepie) { | |
0cf81637 | 329 | $site_url = db_escape_string(trim($rss->get_link())); |
2c08214a | 330 | } else { |
0cf81637 AD |
331 | $site_url = db_escape_string(trim($rss->channel["link"])); |
332 | } | |
333 | ||
334 | // weird, weird Magpie | |
335 | if (!$use_simplepie) { | |
336 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
2c08214a AD |
337 | } |
338 | ||
339 | $site_url = rewrite_relative_url($fetch_url, $site_url); | |
0cf81637 | 340 | $site_url = substr($site_url, 0, 250); |
2c08214a AD |
341 | |
342 | if ($debug_enabled) { | |
343 | _debug("update_rss_feed: checking favicon..."); | |
344 | } | |
345 | ||
f2798eb6 AD |
346 | if ($favicon_needs_check) { |
347 | check_feed_favicon($site_url, $feed, $link); | |
348 | ||
349 | db_query($link, "UPDATE ttrss_feeds SET favicon_last_checked = NOW() | |
350 | WHERE id = '$feed'"); | |
351 | } | |
2c08214a AD |
352 | |
353 | if (!$registered_title || $registered_title == "[Unknown]") { | |
354 | ||
355 | if ($use_simplepie) { | |
356 | $feed_title = db_escape_string($rss->get_title()); | |
357 | } else { | |
358 | $feed_title = db_escape_string($rss->channel["title"]); | |
359 | } | |
360 | ||
361 | if ($debug_enabled) { | |
362 | _debug("update_rss_feed: registering title: $feed_title"); | |
363 | } | |
364 | ||
365 | db_query($link, "UPDATE ttrss_feeds SET | |
366 | title = '$feed_title' WHERE id = '$feed'"); | |
367 | } | |
368 | ||
0cf81637 | 369 | if ($site_url && $orig_site_url != $site_url) { |
2c08214a AD |
370 | db_query($link, "UPDATE ttrss_feeds SET |
371 | site_url = '$site_url' WHERE id = '$feed'"); | |
372 | } | |
373 | ||
374 | // print "I: " . $rss->channel["image"]["url"]; | |
375 | ||
376 | if (!$use_simplepie) { | |
0cf81637 | 377 | $icon_url = db_escape_string(trim($rss->image["url"])); |
2c08214a | 378 | } else { |
0cf81637 | 379 | $icon_url = db_escape_string(trim($rss->get_image_url())); |
2c08214a AD |
380 | } |
381 | ||
0cf81637 | 382 | $icon_url = rewrite_relative_url($fetch_url, $icon_url); |
2c08214a AD |
383 | $icon_url = substr($icon_url, 0, 250); |
384 | ||
385 | if ($icon_url && $orig_icon_url != $icon_url) { | |
386 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
387 | } | |
388 | ||
389 | if ($debug_enabled) { | |
b24504b1 | 390 | _debug("update_rss_feed: loading filters & labels..."); |
2c08214a AD |
391 | } |
392 | ||
393 | $filters = load_filters($link, $feed, $owner_uid); | |
b24504b1 | 394 | $labels = get_all_labels($link, $owner_uid); |
2c08214a | 395 | |
67bd0b1f AD |
396 | if ($debug_enabled) { |
397 | //print_r($filters); | |
398 | _debug("update_rss_feed: " . count($filters) . " filters loaded."); | |
399 | } | |
2c08214a | 400 | |
cc85704f AD |
401 | $filter_plugins = array(); |
402 | ||
403 | if (defined('_ARTICLE_FILTER_PLUGINS')) { | |
404 | foreach (explode(",", _ARTICLE_FILTER_PLUGINS) as $p) { | |
405 | $pclass = "filter_" . trim($p); | |
406 | ||
407 | if (class_exists($pclass)) { | |
408 | $plugin = new $pclass($link); | |
409 | array_push($filter_plugins, $plugin); | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
414 | if ($debug_enabled) { | |
415 | _debug("update_rss_feed: " . count($filter_plugins) . " filter plugins loaded."); | |
416 | } | |
417 | ||
2c08214a AD |
418 | if ($use_simplepie) { |
419 | $iterator = $rss->get_items(); | |
420 | } else { | |
421 | $iterator = $rss->items; | |
422 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; | |
423 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
424 | } | |
425 | ||
426 | if (!is_array($iterator)) { | |
427 | /* db_query($link, "UPDATE ttrss_feeds | |
428 | SET last_error = 'Parse error: can\'t find any articles.' | |
429 | WHERE id = '$feed'"); */ | |
430 | ||
431 | // clear any errors and mark feed as updated if fetched okay | |
432 | // even if it's blank | |
433 | ||
434 | if ($debug_enabled) { | |
435 | _debug("update_rss_feed: entry iterator is not an array, no articles?"); | |
436 | } | |
437 | ||
438 | db_query($link, "UPDATE ttrss_feeds | |
439 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
440 | ||
441 | return; // no articles | |
442 | } | |
443 | ||
444 | if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) { | |
445 | ||
446 | if ($debug_enabled) _debug("update_rss_feed: checking for PUSH hub..."); | |
447 | ||
448 | $feed_hub_url = false; | |
449 | if ($use_simplepie) { | |
450 | $links = $rss->get_links('hub'); | |
451 | ||
452 | if ($links && is_array($links)) { | |
453 | foreach ($links as $l) { | |
454 | $feed_hub_url = $l; | |
455 | break; | |
456 | } | |
457 | } | |
458 | ||
459 | } else { | |
460 | $atom = $rss->channel['atom']; | |
461 | ||
462 | if ($atom) { | |
463 | if ($atom['link@rel'] == 'hub') { | |
464 | $feed_hub_url = $atom['link@href']; | |
465 | } | |
466 | ||
467 | if (!$feed_hub_url && $atom['link#'] > 1) { | |
468 | for ($i = 2; $i <= $atom['link#']; $i++) { | |
469 | if ($atom["link#$i@rel"] == 'hub') { | |
470 | $feed_hub_url = $atom["link#$i@href"]; | |
471 | break; | |
472 | } | |
473 | } | |
474 | } | |
475 | } else { | |
476 | $feed_hub_url = $rss->channel['link_hub']; | |
477 | } | |
478 | } | |
479 | ||
480 | if ($debug_enabled) _debug("update_rss_feed: feed hub url: $feed_hub_url"); | |
481 | ||
482 | if ($feed_hub_url && function_exists('curl_init') && | |
483 | !ini_get("open_basedir")) { | |
484 | ||
485 | require_once 'lib/pubsubhubbub/subscriber.php'; | |
486 | ||
487 | $callback_url = get_self_url_prefix() . | |
488 | "/public.php?op=pubsub&id=$feed"; | |
489 | ||
490 | $s = new Subscriber($feed_hub_url, $callback_url); | |
491 | ||
492 | $rc = $s->subscribe($fetch_url); | |
493 | ||
494 | if ($debug_enabled) | |
495 | _debug("update_rss_feed: feed hub url found, subscribe request sent."); | |
496 | ||
497 | db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1 | |
498 | WHERE id = '$feed'"); | |
499 | } | |
500 | } | |
501 | ||
502 | if ($debug_enabled) { | |
503 | _debug("update_rss_feed: processing articles..."); | |
504 | } | |
505 | ||
506 | foreach ($iterator as $item) { | |
2c08214a AD |
507 | if ($_REQUEST['xdebug'] == 2) { |
508 | print_r($item); | |
509 | } | |
510 | ||
511 | if ($use_simplepie) { | |
512 | $entry_guid = $item->get_id(); | |
513 | if (!$entry_guid) $entry_guid = $item->get_link(); | |
514 | if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title()); | |
515 | ||
516 | } else { | |
517 | ||
518 | $entry_guid = $item["id"]; | |
519 | ||
520 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
521 | if (!$entry_guid) $entry_guid = $item["about"]; | |
522 | if (!$entry_guid) $entry_guid = $item["link"]; | |
523 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); | |
524 | } | |
525 | ||
765509c5 | 526 | if ($cache_content) { |
d5e9cf28 | 527 | $entry_guid = "ccache:$entry_guid"; |
765509c5 AD |
528 | } |
529 | ||
530 | if ($auth_login || $auth_pass) { | |
531 | $entry_guid = "auth,$owner_uid:$entry_guid"; | |
532 | } | |
533 | ||
2c08214a AD |
534 | if ($debug_enabled) { |
535 | _debug("update_rss_feed: guid $entry_guid"); | |
536 | } | |
537 | ||
538 | if (!$entry_guid) continue; | |
539 | ||
540 | $entry_timestamp = ""; | |
541 | ||
542 | if ($use_simplepie) { | |
543 | $entry_timestamp = strtotime($item->get_date()); | |
544 | } else { | |
545 | $rss_2_date = $item['pubdate']; | |
546 | $rss_1_date = $item['dc']['date']; | |
547 | $atom_date = $item['issued']; | |
548 | if (!$atom_date) $atom_date = $item['updated']; | |
549 | ||
550 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); | |
551 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
552 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
553 | ||
554 | } | |
555 | ||
556 | if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) { | |
557 | $entry_timestamp = time(); | |
558 | $no_orig_date = 'true'; | |
559 | } else { | |
560 | $no_orig_date = 'false'; | |
561 | } | |
562 | ||
563 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
564 | ||
565 | if ($debug_enabled) { | |
566 | _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]"); | |
567 | } | |
568 | ||
569 | if ($use_simplepie) { | |
570 | $entry_title = $item->get_title(); | |
571 | } else { | |
572 | $entry_title = trim(strip_tags($item["title"])); | |
573 | } | |
574 | ||
575 | if ($use_simplepie) { | |
576 | $entry_link = $item->get_link(); | |
577 | } else { | |
578 | // strange Magpie workaround | |
579 | $entry_link = $item["link_"]; | |
580 | if (!$entry_link) $entry_link = $item["link"]; | |
581 | } | |
582 | ||
583 | $entry_link = rewrite_relative_url($site_url, $entry_link); | |
584 | ||
585 | if ($debug_enabled) { | |
586 | _debug("update_rss_feed: title $entry_title"); | |
587 | _debug("update_rss_feed: link $entry_link"); | |
588 | } | |
589 | ||
590 | if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);; | |
591 | ||
592 | $entry_link = strip_tags($entry_link); | |
593 | ||
594 | if ($use_simplepie) { | |
595 | $entry_content = $item->get_content(); | |
596 | if (!$entry_content) $entry_content = $item->get_description(); | |
597 | } else { | |
598 | $entry_content = $item["content:escaped"]; | |
599 | ||
600 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
eb38af4e | 601 | if (!$entry_content && is_array($entry_content)) $entry_content = $item["content"]["encoded"]; |
2c08214a AD |
602 | if (!$entry_content) $entry_content = $item["content"]; |
603 | ||
604 | if (is_array($entry_content)) $entry_content = $entry_content[0]; | |
605 | ||
606 | // Magpie bugs are getting ridiculous | |
607 | if (trim($entry_content) == "Array") $entry_content = false; | |
608 | ||
609 | if (!$entry_content) $entry_content = $item["atom_content"]; | |
610 | if (!$entry_content) $entry_content = $item["summary"]; | |
611 | ||
612 | if (!$entry_content || | |
613 | strlen($entry_content) < strlen($item["description"])) { | |
614 | $entry_content = $item["description"]; | |
615 | }; | |
616 | ||
617 | // WTF | |
618 | if (is_array($entry_content)) { | |
619 | $entry_content = $entry_content["encoded"]; | |
620 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
621 | } | |
622 | } | |
623 | ||
c5867798 | 624 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
3c696512 AD |
625 | $entry_content = cache_images($entry_content, $site_url, $debug_enabled); |
626 | ||
2c08214a AD |
627 | if ($_REQUEST["xdebug"] == 2) { |
628 | print "update_rss_feed: content: "; | |
487f0750 | 629 | print $entry_content; |
3c696512 | 630 | print "\n"; |
2c08214a AD |
631 | } |
632 | ||
633 | $entry_content_unescaped = $entry_content; | |
87764a50 | 634 | $entry_cached_content = ""; |
2c08214a AD |
635 | |
636 | if ($use_simplepie) { | |
637 | $entry_comments = strip_tags($item->data["comments"]); | |
638 | if ($item->get_author()) { | |
639 | $entry_author_item = $item->get_author(); | |
640 | $entry_author = $entry_author_item->get_name(); | |
641 | if (!$entry_author) $entry_author = $entry_author_item->get_email(); | |
642 | ||
643 | $entry_author = db_escape_string($entry_author); | |
644 | } | |
645 | } else { | |
646 | $entry_comments = strip_tags($item["comments"]); | |
647 | ||
648 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); | |
649 | ||
650 | if ($item['author']) { | |
651 | ||
652 | if (is_array($item['author'])) { | |
653 | ||
654 | if (!$entry_author) { | |
655 | $entry_author = db_escape_string(strip_tags($item['author']['name'])); | |
656 | } | |
657 | ||
658 | if (!$entry_author) { | |
659 | $entry_author = db_escape_string(strip_tags($item['author']['email'])); | |
660 | } | |
661 | } | |
662 | ||
663 | if (!$entry_author) { | |
664 | $entry_author = db_escape_string(strip_tags($item['author'])); | |
665 | } | |
666 | } | |
667 | } | |
668 | ||
669 | if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = ''; | |
670 | ||
671 | $entry_guid = db_escape_string(strip_tags($entry_guid)); | |
672 | $entry_guid = mb_substr($entry_guid, 0, 250); | |
673 | ||
674 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
675 | WHERE guid = '$entry_guid'"); | |
676 | ||
677 | $entry_content = db_escape_string($entry_content, false); | |
678 | ||
2c08214a AD |
679 | $entry_title = db_escape_string($entry_title); |
680 | $entry_link = db_escape_string($entry_link); | |
681 | $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250); | |
682 | $entry_author = mb_substr($entry_author, 0, 250); | |
683 | ||
684 | if ($use_simplepie) { | |
685 | $num_comments = 0; #FIXME# | |
686 | } else { | |
687 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
688 | } | |
689 | ||
690 | if (!$num_comments) $num_comments = 0; | |
691 | ||
692 | if ($debug_enabled) { | |
693 | _debug("update_rss_feed: looking for tags [1]..."); | |
694 | } | |
695 | ||
696 | // parse <category> entries into tags | |
697 | ||
698 | $additional_tags = array(); | |
699 | ||
700 | if ($use_simplepie) { | |
701 | ||
702 | $additional_tags_src = $item->get_categories(); | |
703 | ||
704 | if (is_array($additional_tags_src)) { | |
705 | foreach ($additional_tags_src as $tobj) { | |
706 | array_push($additional_tags, $tobj->get_term()); | |
707 | } | |
708 | } | |
709 | ||
710 | if ($debug_enabled) { | |
711 | _debug("update_rss_feed: category tags:"); | |
712 | print_r($additional_tags); | |
713 | } | |
714 | ||
715 | } else { | |
716 | ||
717 | $t_ctr = $item['category#']; | |
718 | ||
719 | if ($t_ctr == 0) { | |
720 | $additional_tags = array(); | |
721 | } else if ($t_ctr > 0) { | |
722 | $additional_tags = array($item['category']); | |
723 | ||
724 | if ($item['category@term']) { | |
725 | array_push($additional_tags, $item['category@term']); | |
726 | } | |
727 | ||
728 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
729 | if ($item["category#$i"]) { | |
730 | array_push($additional_tags, $item["category#$i"]); | |
731 | } | |
732 | ||
733 | if ($item["category#$i@term"]) { | |
734 | array_push($additional_tags, $item["category#$i@term"]); | |
735 | } | |
736 | } | |
737 | } | |
738 | ||
739 | // parse <dc:subject> elements | |
740 | ||
741 | $t_ctr = $item['dc']['subject#']; | |
742 | ||
743 | if ($t_ctr > 0) { | |
744 | array_push($additional_tags, $item['dc']['subject']); | |
745 | ||
746 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
747 | if ($item['dc']["subject#$i"]) { | |
748 | array_push($additional_tags, $item['dc']["subject#$i"]); | |
749 | } | |
750 | } | |
751 | } | |
752 | } | |
753 | ||
754 | if ($debug_enabled) { | |
755 | _debug("update_rss_feed: looking for tags [2]..."); | |
756 | } | |
757 | ||
758 | /* taaaags */ | |
759 | // <a href="..." rel="tag">Xorg</a>, // | |
760 | ||
761 | $entry_tags = null; | |
762 | ||
763 | preg_match_all("/<a.*?rel=['\"]tag['\"].*?\>([^<]+)<\/a>/i", | |
764 | $entry_content_unescaped, $entry_tags); | |
765 | ||
766 | $entry_tags = $entry_tags[1]; | |
767 | ||
768 | $entry_tags = array_merge($entry_tags, $additional_tags); | |
769 | $entry_tags = array_unique($entry_tags); | |
770 | ||
771 | for ($i = 0; $i < count($entry_tags); $i++) | |
772 | $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8'); | |
773 | ||
774 | if ($debug_enabled) { | |
6aff7845 AD |
775 | //_debug("update_rss_feed: unfiltered tags found:"); |
776 | //print_r($entry_tags); | |
2c08214a AD |
777 | } |
778 | ||
2c08214a AD |
779 | if ($debug_enabled) { |
780 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); | |
781 | } | |
782 | ||
cc85704f AD |
783 | // TODO: less memory-hungry implementation |
784 | if (count($filter_plugins) > 0) { | |
785 | if ($debug_enabled) { | |
786 | _debug("update_rss_feed: applying plugin filters..."); | |
787 | } | |
788 | ||
789 | $article = array("owner_uid" => $owner_uid, | |
790 | "title" => $entry_title, | |
791 | "content" => $entry_content, | |
792 | "link" => $entry_link, | |
793 | "tags" => $entry_tags, | |
794 | "author" => $entry_author); | |
795 | ||
796 | foreach ($filter_plugins as $plugin) { | |
797 | $article = $plugin->filter_article($article); | |
798 | } | |
799 | ||
800 | $entry_title = $article["title"]; | |
801 | $entry_content = $article["content"]; | |
802 | $entry_tags = $article["tags"]; | |
803 | $entry_author = $article["author"]; | |
804 | } | |
805 | ||
806 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
807 | ||
2c08214a AD |
808 | db_query($link, "BEGIN"); |
809 | ||
810 | if (db_num_rows($result) == 0) { | |
811 | ||
812 | if ($debug_enabled) { | |
813 | _debug("update_rss_feed: base guid not found"); | |
814 | } | |
815 | ||
87764a50 AD |
816 | if ($cache_content) { |
817 | if ($debug_enabled) { | |
8054439f | 818 | _debug("update_rss_feed: caching content (initial)..."); |
87764a50 AD |
819 | } |
820 | ||
821 | $entry_cached_content = cache_content($link, $entry_link, $auth_login, $auth_pass); | |
822 | ||
823 | if ($cache_images && is_writable(CACHE_DIR . '/images')) | |
824 | $entry_cached_content = cache_images($entry_cached_content, $site_url, $debug_enabled); | |
825 | ||
826 | $entry_cached_content = db_escape_string($entry_cached_content, false); | |
87764a50 AD |
827 | } |
828 | ||
2c08214a AD |
829 | // base post entry does not exist, create it |
830 | ||
831 | $result = db_query($link, | |
832 | "INSERT INTO ttrss_entries | |
833 | (title, | |
834 | guid, | |
835 | link, | |
836 | updated, | |
837 | content, | |
838 | content_hash, | |
87764a50 | 839 | cached_content, |
2c08214a AD |
840 | no_orig_date, |
841 | date_updated, | |
842 | date_entered, | |
843 | comments, | |
844 | num_comments, | |
845 | author) | |
846 | VALUES | |
847 | ('$entry_title', | |
848 | '$entry_guid', | |
849 | '$entry_link', | |
850 | '$entry_timestamp_fmt', | |
851 | '$entry_content', | |
852 | '$content_hash', | |
cb93a5de | 853 | '$entry_cached_content', |
2c08214a AD |
854 | $no_orig_date, |
855 | NOW(), | |
856 | NOW(), | |
857 | '$entry_comments', | |
858 | '$num_comments', | |
859 | '$entry_author')"); | |
e8291805 AD |
860 | |
861 | $article_labels = array(); | |
862 | ||
2c08214a AD |
863 | } else { |
864 | // we keep encountering the entry in feeds, so we need to | |
865 | // update date_updated column so that we don't get horrible | |
866 | // dupes when the entry gets purged and reinserted again e.g. | |
867 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
868 | ||
869 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
870 | ||
871 | db_query($link, "UPDATE ttrss_entries SET date_updated = NOW() | |
872 | WHERE id = '$base_entry_id'"); | |
e8291805 AD |
873 | |
874 | $article_labels = get_article_labels($link, $base_entry_id, $owner_uid); | |
2c08214a AD |
875 | } |
876 | ||
877 | // now it should exist, if not - bad luck then | |
878 | ||
879 | $result = db_query($link, "SELECT | |
880 | id,content_hash,no_orig_date,title, | |
881 | ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated, | |
882 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated, | |
130b0781 | 883 | num_comments, cached_content |
2c08214a AD |
884 | FROM |
885 | ttrss_entries | |
886 | WHERE guid = '$entry_guid'"); | |
887 | ||
888 | $entry_ref_id = 0; | |
889 | $entry_int_id = 0; | |
890 | ||
891 | if (db_num_rows($result) == 1) { | |
892 | ||
893 | if ($debug_enabled) { | |
894 | _debug("update_rss_feed: base guid found, checking for user record"); | |
895 | } | |
896 | ||
897 | // this will be used below in update handler | |
898 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
899 | $orig_title = db_fetch_result($result, 0, "title"); | |
900 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
130b0781 | 901 | $orig_cached_content = trim(db_fetch_result($result, 0, "cached_content")); |
2c08214a AD |
902 | $orig_date_updated = strtotime(db_fetch_result($result, |
903 | 0, "date_updated")); | |
904 | ||
905 | $ref_id = db_fetch_result($result, 0, "id"); | |
906 | $entry_ref_id = $ref_id; | |
907 | ||
908 | // check for user post link to main table | |
909 | ||
910 | // do we allow duplicate posts with same GUID in different feeds? | |
911 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { | |
912 | $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)"; | |
913 | } else { | |
914 | $dupcheck_qpart = ""; | |
915 | } | |
916 | ||
917 | /* Collect article tags here so we could filter by them: */ | |
918 | ||
919 | $article_filters = get_article_filters($filters, $entry_title, | |
4021d61a | 920 | $entry_content, $entry_link, $entry_timestamp, $entry_author, |
2c08214a AD |
921 | $entry_tags); |
922 | ||
923 | if ($debug_enabled) { | |
924 | _debug("update_rss_feed: article filters: "); | |
925 | if (count($article_filters) != 0) { | |
926 | print_r($article_filters); | |
927 | } | |
928 | } | |
929 | ||
930 | if (find_article_filter($article_filters, "filter")) { | |
931 | db_query($link, "COMMIT"); // close transaction in progress | |
932 | continue; | |
933 | } | |
934 | ||
935 | $score = calculate_article_score($article_filters); | |
936 | ||
937 | if ($debug_enabled) { | |
938 | _debug("update_rss_feed: initial score: $score"); | |
939 | } | |
940 | ||
941 | $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE | |
942 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
943 | $dupcheck_qpart"; | |
944 | ||
945 | // if ($_REQUEST["xdebug"]) print "$query\n"; | |
946 | ||
947 | $result = db_query($link, $query); | |
948 | ||
949 | // okay it doesn't exist - create user entry | |
950 | if (db_num_rows($result) == 0) { | |
951 | ||
952 | if ($debug_enabled) { | |
953 | _debug("update_rss_feed: user record not found, creating..."); | |
954 | } | |
955 | ||
956 | if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) { | |
957 | $unread = 'true'; | |
958 | $last_read_qpart = 'NULL'; | |
959 | } else { | |
960 | $unread = 'false'; | |
961 | $last_read_qpart = 'NOW()'; | |
962 | } | |
963 | ||
964 | if (find_article_filter($article_filters, 'mark') || $score > 1000) { | |
965 | $marked = 'true'; | |
966 | } else { | |
967 | $marked = 'false'; | |
968 | } | |
969 | ||
970 | if (find_article_filter($article_filters, 'publish')) { | |
971 | $published = 'true'; | |
972 | } else { | |
973 | $published = 'false'; | |
974 | } | |
975 | ||
2ea9bbfd AD |
976 | // N-grams |
977 | ||
978 | if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) { | |
979 | ||
980 | $result = db_query($link, "SELECT COUNT(*) AS similar FROM | |
981 | ttrss_entries,ttrss_user_entries | |
982 | WHERE ref_id = id AND updated >= NOW() - INTERVAL '7 day' | |
983 | AND similarity(title, '$entry_title') >= "._NGRAM_TITLE_DUPLICATE_THRESHOLD." | |
984 | AND owner_uid = $owner_uid"); | |
985 | ||
986 | $ngram_similar = db_fetch_result($result, 0, "similar"); | |
987 | ||
988 | if ($debug_enabled) { | |
989 | _debug("update_rss_feed: N-gram similar results: $ngram_similar"); | |
990 | } | |
991 | ||
992 | if ($ngram_similar > 0) { | |
993 | $unread = 'false'; | |
994 | } | |
995 | } | |
996 | ||
2c08214a AD |
997 | $result = db_query($link, |
998 | "INSERT INTO ttrss_user_entries | |
999 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
1000 | published, score, tag_cache, label_cache, uuid) | |
1001 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
1002 | $last_read_qpart, $marked, $published, '$score', '', '', '')"); | |
1003 | ||
1004 | if (PUBSUBHUBBUB_HUB && $published == 'true') { | |
1005 | $rss_link = get_self_url_prefix() . | |
1006 | "/public.php?op=rss&id=-2&key=" . | |
1007 | get_feed_access_key($link, -2, false, $owner_uid); | |
1008 | ||
1009 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
1010 | ||
1011 | $pubsub_result = $p->publish_update($rss_link); | |
1012 | } | |
1013 | ||
1014 | $result = db_query($link, | |
1015 | "SELECT int_id FROM ttrss_user_entries WHERE | |
1016 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND | |
1017 | feed_id = '$feed' LIMIT 1"); | |
1018 | ||
1019 | if (db_num_rows($result) == 1) { | |
1020 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1021 | } | |
1022 | } else { | |
1023 | if ($debug_enabled) { | |
1024 | _debug("update_rss_feed: user record FOUND"); | |
1025 | } | |
1026 | ||
1027 | $entry_ref_id = db_fetch_result($result, 0, "ref_id"); | |
1028 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
1029 | } | |
1030 | ||
1031 | if ($debug_enabled) { | |
1032 | _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id"); | |
1033 | } | |
1034 | ||
1035 | $post_needs_update = false; | |
1036 | $update_insignificant = false; | |
130b0781 | 1037 | $cached_content_needs_update = false; |
2c08214a AD |
1038 | |
1039 | if ($orig_num_comments != $num_comments) { | |
1040 | $post_needs_update = true; | |
1041 | $update_insignificant = true; | |
1042 | } | |
1043 | ||
1044 | if ($content_hash != $orig_content_hash) { | |
1045 | $post_needs_update = true; | |
1046 | $update_insignificant = false; | |
130b0781 AD |
1047 | $cached_content_needs_update = true; |
1048 | } | |
87764a50 | 1049 | |
130b0781 AD |
1050 | if ($cache_content) { |
1051 | if ($debug_enabled) { | |
1052 | _debug("update_rss_feed: caching content because original checksum changed..."); | |
1053 | } | |
87764a50 | 1054 | |
130b0781 | 1055 | $entry_cached_content = cache_content($link, $entry_link, $auth_login, $auth_pass); |
87764a50 | 1056 | |
130b0781 | 1057 | if ($entry_cached_content) { |
87764a50 AD |
1058 | if ($cache_images && is_writable(CACHE_DIR . '/images')) |
1059 | $entry_cached_content = cache_images($entry_cached_content, $site_url, $debug_enabled); | |
1060 | ||
1061 | $entry_cached_content = db_escape_string($entry_cached_content, false); | |
130b0781 AD |
1062 | $post_needs_update = true; |
1063 | } else { | |
1064 | $entry_cached_content = db_escape_string($orig_cached_content); | |
87764a50 | 1065 | } |
130b0781 AD |
1066 | } else { |
1067 | $entry_cached_content = db_escape_string($orig_cached_content); | |
2c08214a AD |
1068 | } |
1069 | ||
1070 | if (db_escape_string($orig_title) != $entry_title) { | |
1071 | $post_needs_update = true; | |
1072 | $update_insignificant = false; | |
1073 | } | |
1074 | ||
1075 | // if post needs update, update it and mark all user entries | |
1076 | // linking to this post as updated | |
1077 | if ($post_needs_update) { | |
1078 | ||
1079 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
1080 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
1081 | } | |
1082 | ||
1083 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
1084 | ||
1085 | db_query($link, "UPDATE ttrss_entries | |
1086 | SET title = '$entry_title', content = '$entry_content', | |
1087 | content_hash = '$content_hash', | |
87764a50 | 1088 | cached_content = '$entry_cached_content', |
2c08214a AD |
1089 | updated = '$entry_timestamp_fmt', |
1090 | num_comments = '$num_comments' | |
1091 | WHERE id = '$ref_id'"); | |
1092 | ||
1093 | if (!$update_insignificant) { | |
1094 | if ($mark_unread_on_update) { | |
1095 | db_query($link, "UPDATE ttrss_user_entries | |
1096 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
1097 | } else if ($update_on_checksum_change) { | |
1098 | db_query($link, "UPDATE ttrss_user_entries | |
1099 | SET last_read = null WHERE ref_id = '$ref_id' | |
1100 | AND unread = false"); | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | db_query($link, "COMMIT"); | |
1107 | ||
1108 | if ($debug_enabled) { | |
1109 | _debug("update_rss_feed: assigning labels..."); | |
1110 | } | |
1111 | ||
92c14e9d | 1112 | assign_article_to_label_filters($link, $entry_ref_id, $article_filters, |
b24504b1 | 1113 | $owner_uid, $article_labels); |
2c08214a AD |
1114 | |
1115 | if ($debug_enabled) { | |
1116 | _debug("update_rss_feed: looking for enclosures..."); | |
1117 | } | |
1118 | ||
1119 | // enclosures | |
1120 | ||
1121 | $enclosures = array(); | |
1122 | ||
1123 | if ($use_simplepie) { | |
1124 | $encs = $item->get_enclosures(); | |
1125 | ||
1126 | if (is_array($encs)) { | |
1127 | foreach ($encs as $e) { | |
1128 | $e_item = array( | |
1129 | $e->link, $e->type, $e->length); | |
1130 | ||
1131 | array_push($enclosures, $e_item); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | } else { | |
1136 | // <enclosure> | |
1137 | ||
1138 | $e_ctr = $item['enclosure#']; | |
1139 | ||
1140 | if ($e_ctr > 0) { | |
1141 | $e_item = array($item['enclosure@url'], | |
1142 | $item['enclosure@type'], | |
1143 | $item['enclosure@length']); | |
1144 | ||
1145 | array_push($enclosures, $e_item); | |
1146 | ||
1147 | for ($i = 0; $i <= $e_ctr; $i++ ) { | |
1148 | ||
1149 | if ($item["enclosure#$i@url"]) { | |
1150 | $e_item = array($item["enclosure#$i@url"], | |
1151 | $item["enclosure#$i@type"], | |
1152 | $item["enclosure#$i@length"]); | |
1153 | array_push($enclosures, $e_item); | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | // <media:content> | |
1159 | // can there be many of those? yes -fox | |
1160 | ||
1161 | $m_ctr = $item['media']['content#']; | |
1162 | ||
1163 | if ($m_ctr > 0) { | |
1164 | $e_item = array($item['media']['content@url'], | |
1165 | $item['media']['content@medium'], | |
1166 | $item['media']['content@length']); | |
1167 | ||
1168 | array_push($enclosures, $e_item); | |
1169 | ||
1170 | for ($i = 0; $i <= $m_ctr; $i++ ) { | |
1171 | ||
1172 | if ($item["media"]["content#$i@url"]) { | |
1173 | $e_item = array($item["media"]["content#$i@url"], | |
1174 | $item["media"]["content#$i@medium"], | |
1175 | $item["media"]["content#$i@length"]); | |
1176 | array_push($enclosures, $e_item); | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | } | |
1181 | } | |
1182 | ||
1183 | ||
1184 | if ($debug_enabled) { | |
1185 | _debug("update_rss_feed: article enclosures:"); | |
1186 | print_r($enclosures); | |
1187 | } | |
1188 | ||
1189 | db_query($link, "BEGIN"); | |
1190 | ||
1191 | foreach ($enclosures as $enc) { | |
1192 | $enc_url = db_escape_string($enc[0]); | |
1193 | $enc_type = db_escape_string($enc[1]); | |
1194 | $enc_dur = db_escape_string($enc[2]); | |
1195 | ||
1196 | $result = db_query($link, "SELECT id FROM ttrss_enclosures | |
1197 | WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'"); | |
1198 | ||
1199 | if (db_num_rows($result) == 0) { | |
1200 | db_query($link, "INSERT INTO ttrss_enclosures | |
1201 | (content_url, content_type, title, duration, post_id) VALUES | |
1202 | ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')"); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | db_query($link, "COMMIT"); | |
1207 | ||
1208 | // check for manual tags (we have to do it here since they're loaded from filters) | |
1209 | ||
1210 | foreach ($article_filters as $f) { | |
6aff7845 | 1211 | if ($f["type"] == "tag") { |
2c08214a | 1212 | |
6aff7845 | 1213 | $manual_tags = trim_array(explode(",", $f["param"])); |
2c08214a AD |
1214 | |
1215 | foreach ($manual_tags as $tag) { | |
1216 | if (tag_is_valid($tag)) { | |
1217 | array_push($entry_tags, $tag); | |
1218 | } | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | // Skip boring tags | |
1224 | ||
1225 | $boring_tags = trim_array(explode(",", mb_strtolower(get_pref($link, | |
1226 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
1227 | ||
1228 | $filtered_tags = array(); | |
1229 | $tags_to_cache = array(); | |
1230 | ||
1231 | if ($entry_tags && is_array($entry_tags)) { | |
1232 | foreach ($entry_tags as $tag) { | |
1233 | if (array_search($tag, $boring_tags) === false) { | |
1234 | array_push($filtered_tags, $tag); | |
1235 | } | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | $filtered_tags = array_unique($filtered_tags); | |
1240 | ||
1241 | if ($debug_enabled) { | |
1242 | _debug("update_rss_feed: filtered article tags:"); | |
1243 | print_r($filtered_tags); | |
1244 | } | |
1245 | ||
1246 | // Save article tags in the database | |
1247 | ||
1248 | if (count($filtered_tags) > 0) { | |
1249 | ||
1250 | db_query($link, "BEGIN"); | |
1251 | ||
1252 | foreach ($filtered_tags as $tag) { | |
1253 | ||
1254 | $tag = sanitize_tag($tag); | |
1255 | $tag = db_escape_string($tag); | |
1256 | ||
1257 | if (!tag_is_valid($tag)) continue; | |
1258 | ||
1259 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
1260 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
1261 | owner_uid = '$owner_uid' LIMIT 1"); | |
1262 | ||
1263 | if ($result && db_num_rows($result) == 0) { | |
1264 | ||
1265 | db_query($link, "INSERT INTO ttrss_tags | |
1266 | (owner_uid,tag_name,post_int_id) | |
1267 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
1268 | } | |
1269 | ||
1270 | array_push($tags_to_cache, $tag); | |
1271 | } | |
1272 | ||
1273 | /* update the cache */ | |
1274 | ||
1275 | $tags_to_cache = array_unique($tags_to_cache); | |
1276 | ||
1277 | $tags_str = db_escape_string(join(",", $tags_to_cache)); | |
1278 | ||
1279 | db_query($link, "UPDATE ttrss_user_entries | |
1280 | SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id' | |
1281 | AND owner_uid = $owner_uid"); | |
1282 | ||
1283 | db_query($link, "COMMIT"); | |
1284 | } | |
1285 | ||
b24504b1 AD |
1286 | if (get_pref($link, "AUTO_ASSIGN_LABELS", $owner_uid, false)) { |
1287 | if ($debug_enabled) { | |
1288 | _debug("update_rss_feed: auto-assigning labels..."); | |
1289 | } | |
1290 | ||
1291 | foreach ($labels as $label) { | |
1292 | $caption = $label["caption"]; | |
1293 | ||
3fc6e71a | 1294 | if (preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) { |
b24504b1 AD |
1295 | if (!labels_contains_caption($article_labels, $caption)) { |
1296 | label_add_article($link, $entry_ref_id, $caption, $owner_uid); | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | ||
2c08214a AD |
1302 | if ($debug_enabled) { |
1303 | _debug("update_rss_feed: article processed"); | |
1304 | } | |
1305 | } | |
1306 | ||
1307 | if (!$last_updated) { | |
1308 | if ($debug_enabled) { | |
1309 | _debug("update_rss_feed: new feed, catching it up..."); | |
1310 | } | |
1311 | catchup_feed($link, $feed, false, $owner_uid); | |
1312 | } | |
1313 | ||
1314 | if ($debug_enabled) { | |
1315 | _debug("purging feed..."); | |
1316 | } | |
1317 | ||
1318 | purge_feed($link, $feed, 0, $debug_enabled); | |
1319 | ||
1320 | db_query($link, "UPDATE ttrss_feeds | |
f2798eb6 | 1321 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); |
2c08214a AD |
1322 | |
1323 | // db_query($link, "COMMIT"); | |
1324 | ||
1325 | } else { | |
1326 | ||
1327 | if ($use_simplepie) { | |
1328 | $error_msg = mb_substr($rss->error(), 0, 250); | |
1329 | } else { | |
1330 | $error_msg = mb_substr(magpie_error(), 0, 250); | |
1331 | } | |
1332 | ||
1333 | if ($debug_enabled) { | |
1334 | _debug("update_rss_feed: error fetching feed: $error_msg"); | |
1335 | } | |
1336 | ||
1337 | $error_msg = db_escape_string($error_msg); | |
1338 | ||
1339 | db_query($link, | |
1340 | "UPDATE ttrss_feeds SET last_error = '$error_msg', | |
1341 | last_updated = NOW() WHERE id = '$feed'"); | |
1342 | } | |
1343 | ||
1344 | if ($use_simplepie) { | |
1345 | unset($rss); | |
1346 | } | |
1347 | ||
1348 | if ($debug_enabled) { | |
1349 | _debug("update_rss_feed: done"); | |
1350 | } | |
1351 | ||
1352 | } | |
1353 | ||
3c696512 AD |
1354 | function cache_images($html, $site_url, $debug) { |
1355 | $cache_dir = CACHE_DIR . "/images"; | |
1356 | ||
1357 | libxml_use_internal_errors(true); | |
1358 | ||
1359 | $charset_hack = '<head> | |
1360 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
1361 | </head>'; | |
1362 | ||
1363 | $doc = new DOMDocument(); | |
1364 | $doc->loadHTML($charset_hack . $html); | |
1365 | $xpath = new DOMXPath($doc); | |
1366 | ||
1367 | $entries = $xpath->query('(//img[@src])'); | |
1368 | ||
1369 | foreach ($entries as $entry) { | |
1370 | if ($entry->hasAttribute('src')) { | |
1371 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); | |
1372 | ||
1373 | $local_filename = CACHE_DIR . "/images/" . sha1($src) . ".png"; | |
1374 | ||
1375 | if ($debug) _debug("cache_images: downloading: $src to $local_filename"); | |
1376 | ||
1377 | if (!file_exists($local_filename)) { | |
1378 | $file_content = fetch_file_contents($src); | |
1379 | ||
b8379e69 | 1380 | if ($file_content && strlen($file_content) > 1024) { |
3c696512 AD |
1381 | file_put_contents($local_filename, $file_content); |
1382 | } | |
1383 | } | |
1384 | ||
1385 | if (file_exists($local_filename)) { | |
1386 | $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' . | |
487f0750 | 1387 | base64_encode($src)); |
3c696512 AD |
1388 | } |
1389 | } | |
1390 | } | |
1391 | ||
1392 | $node = $doc->getElementsByTagName('body')->item(0); | |
1393 | ||
7b8ff151 | 1394 | return $doc->saveXML($node, LIBXML_NOEMPTYTAG); |
3c696512 AD |
1395 | } |
1396 | ||
1397 | function expire_cached_files($debug) { | |
d3158e22 | 1398 | foreach (array("magpie", "simplepie", "images", "export") as $dir) { |
3c696512 | 1399 | $cache_dir = CACHE_DIR . "/$dir"; |
2c08214a | 1400 | |
3c696512 | 1401 | if ($debug) _debug("Expiring $cache_dir"); |
2c08214a | 1402 | |
3c696512 AD |
1403 | $num_deleted = 0; |
1404 | ||
1405 | if (is_writable($cache_dir)) { | |
1406 | $files = glob("$cache_dir/*"); | |
1407 | ||
2ab20c31 AD |
1408 | if ($files) |
1409 | foreach ($files as $file) { | |
1410 | if (time() - filemtime($file) > 86400*7) { | |
1411 | unlink($file); | |
3c696512 | 1412 | |
2ab20c31 AD |
1413 | ++$num_deleted; |
1414 | } | |
3c696512 AD |
1415 | } |
1416 | } | |
3c696512 AD |
1417 | |
1418 | if ($debug) _debug("Removed $num_deleted files."); | |
1419 | } | |
1420 | } | |
2c08214a | 1421 | |
a3e0bdcf AD |
1422 | /** |
1423 | * Source: http://www.php.net/manual/en/function.parse-url.php#104527 | |
1424 | * Returns the url query as associative array | |
1425 | * | |
1426 | * @param string query | |
1427 | * @return array params | |
1428 | */ | |
1429 | function convertUrlQuery($query) { | |
1430 | $queryParts = explode('&', $query); | |
1431 | ||
1432 | $params = array(); | |
1433 | ||
1434 | foreach ($queryParts as $param) { | |
1435 | $item = explode('=', $param); | |
1436 | $params[$item[0]] = $item[1]; | |
1437 | } | |
1438 | ||
1439 | return $params; | |
1440 | } | |
92c14e9d AD |
1441 | |
1442 | function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) { | |
1443 | $matches = array(); | |
1444 | ||
1445 | foreach ($filters as $filter) { | |
1446 | $match_any_rule = $filter["match_any_rule"]; | |
1447 | $filter_match = false; | |
1448 | ||
1449 | foreach ($filter["rules"] as $rule) { | |
1450 | $match = false; | |
1451 | $reg_exp = $rule["reg_exp"]; | |
1452 | ||
1453 | if (!$reg_exp) | |
1454 | continue; | |
1455 | ||
1456 | switch ($rule["type"]) { | |
1457 | case "title": | |
1458 | $match = @preg_match("/$reg_exp/i", $title); | |
1459 | break; | |
1460 | case "content": | |
d03ae73e AD |
1461 | // we don't need to deal with multiline regexps |
1462 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
1463 | ||
92c14e9d AD |
1464 | $match = @preg_match("/$reg_exp/i", $content); |
1465 | break; | |
1466 | case "both": | |
d03ae73e AD |
1467 | // we don't need to deal with multiline regexps |
1468 | $content = preg_replace("/[\r\n\t]/", "", $content); | |
1469 | ||
72d1d067 | 1470 | $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $content)); |
92c14e9d AD |
1471 | break; |
1472 | case "link": | |
1473 | $match = @preg_match("/$reg_exp/i", $link); | |
1474 | break; | |
1475 | case "author": | |
1476 | $match = @preg_match("/$reg_exp/i", $author); | |
1477 | break; | |
1478 | case "tag": | |
1479 | $tag_string = join(",", $tags); | |
1480 | $match = @preg_match("/$reg_exp/i", $tag_string); | |
1481 | break; | |
1482 | } | |
1483 | ||
1484 | if ($match_any_rule) { | |
1485 | if ($match) { | |
1486 | $filter_match = true; | |
1487 | break; | |
1488 | } | |
1489 | } else { | |
1490 | $filter_match = $match; | |
1491 | if (!$match) { | |
1492 | break; | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | if ($filter_match) { | |
1498 | foreach ($filter["actions"] AS $action) { | |
1499 | array_push($matches, $action); | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | return $matches; | |
1505 | } | |
1506 | ||
1507 | function find_article_filter($filters, $filter_name) { | |
1508 | foreach ($filters as $f) { | |
1509 | if ($f["type"] == $filter_name) { | |
1510 | return $f; | |
1511 | }; | |
1512 | } | |
1513 | return false; | |
1514 | } | |
1515 | ||
1516 | function find_article_filters($filters, $filter_name) { | |
1517 | $results = array(); | |
1518 | ||
1519 | foreach ($filters as $f) { | |
1520 | if ($f["type"] == $filter_name) { | |
1521 | array_push($results, $f); | |
1522 | }; | |
1523 | } | |
1524 | return $results; | |
1525 | } | |
1526 | ||
1527 | function calculate_article_score($filters) { | |
1528 | $score = 0; | |
1529 | ||
1530 | foreach ($filters as $f) { | |
1531 | if ($f["type"] == "score") { | |
1532 | $score += $f["param"]; | |
1533 | }; | |
1534 | } | |
1535 | return $score; | |
1536 | } | |
1537 | ||
b24504b1 AD |
1538 | function labels_contains_caption($labels, $caption) { |
1539 | foreach ($labels as $label) { | |
1540 | if ($label[1] == $caption) { | |
1541 | return true; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | return false; | |
1546 | } | |
1547 | ||
1548 | function assign_article_to_label_filters($link, $id, $filters, $owner_uid, $article_labels) { | |
92c14e9d AD |
1549 | foreach ($filters as $f) { |
1550 | if ($f["type"] == "label") { | |
b24504b1 AD |
1551 | if (!labels_contains_caption($article_labels, $f["param"])) { |
1552 | label_add_article($link, $id, $f["param"], $owner_uid); | |
1553 | } | |
1554 | } | |
92c14e9d AD |
1555 | } |
1556 | } | |
87764a50 AD |
1557 | |
1558 | function cache_content($link, $url, $login, $pass) { | |
1559 | ||
1560 | $content = fetch_file_contents($url, $login, $pass); | |
1561 | ||
1562 | if ($content) { | |
1563 | $doc = new DOMDocument(); | |
1564 | @$doc->loadHTML($content); | |
1565 | $xpath = new DOMXPath($doc); | |
1566 | ||
1567 | $node = $doc->getElementsByTagName('body')->item(0); | |
1568 | ||
1569 | if ($node) { | |
1570 | $content = $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
1571 | ||
1572 | return $content; | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | return ""; | |
1577 | } | |
2c08214a | 1578 | ?> |