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