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