]> git.wh0rd.org - tt-rss.git/blame - include/rssfuncs.php
force disable simplepie unnecessarily sanitizing data
[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();
5d56d100
AD
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();
19b3992b 298 $rss->set_output_encoding('UTF-8');
017401dd 299 $rss->set_raw_data($feed_data);
2c08214a 300
19b3992b
AD
301 if ($debug_enabled) {
302 _debug("feed update interval (sec): " .
303 get_feed_update_interval($link, $feed)*60);
304 }
2c08214a 305
19b3992b 306 $rss->enable_cache(!$no_cache);
2c08214a 307
19b3992b
AD
308 if (!$no_cache) {
309 $rss->set_cache_location($simplepie_cache_dir);
310 $rss->set_cache_duration($cache_age);
2c08214a
AD
311 }
312
ada103f2 313 @$rss->init();
19b3992b 314
2c08214a
AD
315// print_r($rss);
316
2c08214a
AD
317 $feed = db_escape_string($feed);
318
19b3992b 319 if (!$rss->error()) {
2c08214a 320
d2a421e3 321 // We use local pluginhost here because we need to load different per-user feed plugins
4412b877
AD
322 $pluginhost->run_hooks($pluginhost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);
323
2c08214a
AD
324 if ($debug_enabled) {
325 _debug("update_rss_feed: processing feed data...");
326 }
327
328// db_query($link, "BEGIN");
329
382268c6
AD
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
750cfcd2 336 $result = db_query($link, "SELECT title,site_url,owner_uid,
382268c6
AD
337 (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS
338 favicon_needs_check
2c08214a
AD
339 FROM ttrss_feeds WHERE id = '$feed'");
340
341 $registered_title = db_fetch_result($result, 0, "title");
2c08214a 342 $orig_site_url = db_fetch_result($result, 0, "site_url");
382268c6
AD
343 $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0,
344 "favicon_needs_check"));
2c08214a
AD
345
346 $owner_uid = db_fetch_result($result, 0, "owner_uid");
347
bb566dc2 348 $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
2c08214a
AD
349
350 if ($debug_enabled) {
351 _debug("update_rss_feed: checking favicon...");
352 }
353
f2798eb6
AD
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 }
2c08214a
AD
360
361 if (!$registered_title || $registered_title == "[Unknown]") {
362
19b3992b 363 $feed_title = db_escape_string($rss->get_title());
2c08214a
AD
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
0cf81637 373 if ($site_url && $orig_site_url != $site_url) {
2c08214a
AD
374 db_query($link, "UPDATE ttrss_feeds SET
375 site_url = '$site_url' WHERE id = '$feed'");
376 }
377
2c08214a 378 if ($debug_enabled) {
b24504b1 379 _debug("update_rss_feed: loading filters & labels...");
2c08214a
AD
380 }
381
382 $filters = load_filters($link, $feed, $owner_uid);
b24504b1 383 $labels = get_all_labels($link, $owner_uid);
2c08214a 384
67bd0b1f
AD
385 if ($debug_enabled) {
386 //print_r($filters);
387 _debug("update_rss_feed: " . count($filters) . " filters loaded.");
388 }
2c08214a 389
19b3992b 390 $items = $rss->get_items();
2c08214a 391
19b3992b 392 if (!is_array($items)) {
2c08214a 393 if ($debug_enabled) {
19b3992b 394 _debug("update_rss_feed: no articles found.");
2c08214a
AD
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;
2c08214a 408
19b3992b 409 $links = $rss->get_links('hub');
2c08214a 410
19b3992b
AD
411 if ($links && is_array($links)) {
412 foreach ($links as $l) {
413 $feed_hub_url = $l;
414 break;
2c08214a
AD
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
19b3992b 444 foreach ($items as $item) {
5d56d100 445 if ($_REQUEST['xdebug'] == 3) {
2c08214a
AD
446 print_r($item);
447 }
448
19b3992b
AD
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());
2c08214a
AD
452
453 if ($debug_enabled) {
454 _debug("update_rss_feed: guid $entry_guid");
455 }
456
457 if (!$entry_guid) continue;
458
3a4c8973
AD
459 $entry_guid = "$owner_uid,$entry_guid";
460
2c08214a
AD
461 $entry_timestamp = "";
462
19b3992b 463 $entry_timestamp = strtotime($item->get_date());
2c08214a 464
19b3992b 465 if ($entry_timestamp == -1 || !$entry_timestamp) {
2c08214a
AD
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
19b3992b 478 $entry_title = $item->get_title();
1b35d30c 479
5d56d100 480 $entry_link = rewrite_relative_url($site_url, $item->get_link());
2c08214a
AD
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
19b3992b
AD
489 $entry_content = $item->get_content();
490 if (!$entry_content) $entry_content = $item->get_description();
2c08214a
AD
491
492 if ($_REQUEST["xdebug"] == 2) {
493 print "update_rss_feed: content: ";
487f0750 494 print $entry_content;
3c696512 495 print "\n";
2c08214a
AD
496 }
497
19b3992b 498 $entry_comments = $item->data["comments"];
2c08214a 499
19b3992b
AD
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();
2c08214a 504
19b3992b 505 $entry_author = db_escape_string($entry_author);
2c08214a
AD
506 }
507
bb566dc2 508 $entry_guid = db_escape_string(mb_substr($entry_guid, 0, 245));
2c08214a 509
bb566dc2
AD
510 $entry_comments = db_escape_string(mb_substr($entry_comments, 0, 245));
511 $entry_author = db_escape_string(mb_substr($entry_author, 0, 245));
2c08214a 512
19b3992b 513 $num_comments = $item->get_item_tags('http://purl.org/rss/1.0/modules/slash/', 'comments');
83e6e313 514
19b3992b
AD
515 if (is_array($num_comments) && is_array($num_comments[0])) {
516 $num_comments = (int) $num_comments[0]["data"];
2c08214a 517 } else {
19b3992b 518 $num_comments = 0;
2c08214a
AD
519 }
520
2c08214a 521 if ($debug_enabled) {
83e6e313 522 _debug("update_rss_feed: num_comments: $num_comments");
2c08214a
AD
523 _debug("update_rss_feed: looking for tags [1]...");
524 }
525
526 // parse <category> entries into tags
527
528 $additional_tags = array();
529
19b3992b 530 $additional_tags_src = $item->get_categories();
2c08214a 531
19b3992b
AD
532 if (is_array($additional_tags_src)) {
533 foreach ($additional_tags_src as $tobj) {
534 array_push($additional_tags, $tobj->get_term());
2c08214a 535 }
19b3992b 536 }
2c08214a 537
19b3992b
AD
538 if ($debug_enabled) {
539 _debug("update_rss_feed: category tags:");
540 print_r($additional_tags);
2c08214a
AD
541 }
542
543 if ($debug_enabled) {
544 _debug("update_rss_feed: looking for tags [2]...");
545 }
546
fa6fbd36 547 $entry_tags = array_unique($additional_tags);
2c08214a
AD
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) {
6aff7845
AD
553 //_debug("update_rss_feed: unfiltered tags found:");
554 //print_r($entry_tags);
2c08214a
AD
555 }
556
2c08214a
AD
557 if ($debug_enabled) {
558 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
559 }
560
cc85704f 561 // TODO: less memory-hungry implementation
19c73507 562
19b3992b
AD
563 if ($debug_enabled) {
564 _debug("update_rss_feed: applying plugin filters..");
565 }
cc85704f 566
e02555c1
AD
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");
b30abdad
AD
570
571 if (db_num_rows($result) != 0) {
572 $entry_plugin_data = db_fetch_result($result, 0, "plugin_data");
e02555c1
AD
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"));
b30abdad
AD
578 } else {
579 $entry_plugin_data = "";
e02555c1 580 $stored_article = array();
b30abdad
AD
581 }
582
455b1401 583 $article = array("owner_uid" => $owner_uid, // read only
b30abdad 584 "guid" => $entry_guid, // read only
19b3992b
AD
585 "title" => $entry_title,
586 "content" => $entry_content,
587 "link" => $entry_link,
588 "tags" => $entry_tags,
b30abdad 589 "plugin_data" => $entry_plugin_data,
e02555c1
AD
590 "author" => $entry_author,
591 "stored" => $stored_article);
cc85704f 592
19b3992b
AD
593 foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_FILTER) as $plugin) {
594 $article = $plugin->hook_article_filter($article);
cc85704f
AD
595 }
596
19b3992b 597 $entry_tags = $article["tags"];
b30abdad 598 $entry_guid = db_escape_string($entry_guid);
1b35d30c
AD
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"]);
b30abdad
AD
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 }
2bbd6994 608
0a3fd79b
AD
609 if ($cache_images && is_writable(CACHE_DIR . '/images'))
610 $entry_content = cache_images($entry_content, $site_url, $debug_enabled);
611
9e222305 612 $content_hash = "SHA1:" . sha1($entry_content);
cc85704f 613
2c08214a
AD
614 db_query($link, "BEGIN");
615
9e222305
AD
616 $result = db_query($link, "SELECT id FROM ttrss_entries
617 WHERE guid = '$entry_guid'");
618
2c08214a
AD
619 if (db_num_rows($result) == 0) {
620
621 if ($debug_enabled) {
9e222305 622 _debug("update_rss_feed: base guid [$entry_guid] not found");
2c08214a
AD
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,
87764a50 635 cached_content,
2c08214a
AD
636 no_orig_date,
637 date_updated,
638 date_entered,
639 comments,
640 num_comments,
b30abdad 641 plugin_data,
2c08214a
AD
642 author)
643 VALUES
644 ('$entry_title',
645 '$entry_guid',
646 '$entry_link',
647 '$entry_timestamp_fmt',
648 '$entry_content',
649 '$content_hash',
0a3fd79b 650 '',
2c08214a
AD
651 $no_orig_date,
652 NOW(),
653 NOW(),
654 '$entry_comments',
655 '$num_comments',
b30abdad 656 '$entry_plugin_data',
2c08214a 657 '$entry_author')");
e8291805
AD
658
659 $article_labels = array();
660
2c08214a
AD
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'");
e8291805
AD
671
672 $article_labels = get_article_labels($link, $base_entry_id, $owner_uid);
2c08214a
AD
673 }
674
675 // now it should exist, if not - bad luck then
676
677 $result = db_query($link, "SELECT
b30abdad 678 id,content_hash,no_orig_date,title,plugin_data,
2c08214a
AD
679 ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated,
680 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
0a3fd79b 681 num_comments
2c08214a
AD
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) {
9e222305 692 _debug("update_rss_feed: base guid [$entry_guid] found, checking for user record");
2c08214a
AD
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"));
b30abdad 701 $orig_plugin_data = db_fetch_result($result, 0, "plugin_data");
2c08214a
AD
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,
4021d61a 718 $entry_content, $entry_link, $entry_timestamp, $entry_author,
2c08214a
AD
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
2ea9bbfd
AD
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
2c08214a
AD
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
b30abdad
AD
841 if ($entry_plugin_data != $orig_plugin_data) {
842 $post_needs_update = true;
843 $update_insignificant = true;
844 }
845
2c08214a
AD
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',
b30abdad
AD
870 num_comments = '$num_comments',
871 plugin_data = '$entry_plugin_data'
2c08214a
AD
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
92c14e9d 893 assign_article_to_label_filters($link, $entry_ref_id, $article_filters,
b24504b1 894 $owner_uid, $article_labels);
2c08214a
AD
895
896 if ($debug_enabled) {
897 _debug("update_rss_feed: looking for enclosures...");
898 }
899
900 // enclosures
901
902 $enclosures = array();
903
19b3992b 904 $encs = $item->get_enclosures();
2c08214a 905
19b3992b
AD
906 if (is_array($encs)) {
907 foreach ($encs as $e) {
908 $e_item = array(
909 $e->link, $e->type, $e->length);
2c08214a 910 array_push($enclosures, $e_item);
2c08214a
AD
911 }
912 }
913
2c08214a
AD
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) {
6aff7845 941 if ($f["type"] == "tag") {
2c08214a 942
6aff7845 943 $manual_tags = trim_array(explode(",", $f["param"]));
2c08214a
AD
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
b24504b1
AD
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
3fc6e71a 1024 if (preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) {
b24504b1
AD
1025 if (!labels_contains_caption($article_labels, $caption)) {
1026 label_add_article($link, $entry_ref_id, $caption, $owner_uid);
1027 }
1028 }
1029 }
1030 }
1031
2c08214a
AD
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
f2798eb6 1051 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
2c08214a
AD
1052
1053// db_query($link, "COMMIT");
1054
1055 } else {
1056
bb566dc2 1057 $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245));
2c08214a
AD
1058
1059 if ($debug_enabled) {
1060 _debug("update_rss_feed: error fetching feed: $error_msg");
1061 }
1062
2c08214a
AD
1063 db_query($link,
1064 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1065 last_updated = NOW() WHERE id = '$feed'");
1066 }
1067
19b3992b 1068 unset($rss);
2c08214a
AD
1069
1070 if ($debug_enabled) {
1071 _debug("update_rss_feed: done");
1072 }
1073
1074 }
1075
3c696512
AD
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
b8379e69 1102 if ($file_content && strlen($file_content) > 1024) {
3c696512
AD
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=' .
487f0750 1109 base64_encode($src));
3c696512
AD
1110 }
1111 }
1112 }
1113
1114 $node = $doc->getElementsByTagName('body')->item(0);
1115
7b8ff151 1116 return $doc->saveXML($node, LIBXML_NOEMPTYTAG);
3c696512
AD
1117 }
1118
2a91b6ff
AD
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
3c696512 1140 function expire_cached_files($debug) {
19b3992b 1141 foreach (array("simplepie", "images", "export") as $dir) {
3c696512 1142 $cache_dir = CACHE_DIR . "/$dir";
2c08214a 1143
3c696512 1144 if ($debug) _debug("Expiring $cache_dir");
2c08214a 1145
3c696512
AD
1146 $num_deleted = 0;
1147
1148 if (is_writable($cache_dir)) {
1149 $files = glob("$cache_dir/*");
1150
2a91b6ff 1151 if ($files) {
2ab20c31
AD
1152 foreach ($files as $file) {
1153 if (time() - filemtime($file) > 86400*7) {
1154 unlink($file);
3c696512 1155
2ab20c31
AD
1156 ++$num_deleted;
1157 }
3c696512
AD
1158 }
1159 }
2a91b6ff 1160 }
3c696512
AD
1161
1162 if ($debug) _debug("Removed $num_deleted files.");
1163 }
1164 }
2c08214a 1165
a3e0bdcf
AD
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 }
92c14e9d
AD
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":
d03ae73e
AD
1205 // we don't need to deal with multiline regexps
1206 $content = preg_replace("/[\r\n\t]/", "", $content);
1207
92c14e9d
AD
1208 $match = @preg_match("/$reg_exp/i", $content);
1209 break;
1210 case "both":
d03ae73e
AD
1211 // we don't need to deal with multiline regexps
1212 $content = preg_replace("/[\r\n\t]/", "", $content);
1213
72d1d067 1214 $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $content));
92c14e9d
AD
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
b24504b1
AD
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) {
92c14e9d
AD
1293 foreach ($filters as $f) {
1294 if ($f["type"] == "label") {
b24504b1
AD
1295 if (!labels_contains_caption($article_labels, $f["param"])) {
1296 label_add_article($link, $id, $f["param"], $owner_uid);
1297 }
1298 }
92c14e9d
AD
1299 }
1300 }
87764a50
AD
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 }
87d7e850
AD
1322
1323 function make_guid_from_title($title) {
1324 return preg_replace("/[ \"\',.:;]/", "-",
1325 mb_strtolower(strip_tags($title), 'utf-8'));
1326 }
1327
1328
2c08214a 1329?>