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