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