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