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