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