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