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