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