2 require_once 'config.php';
3 require_once 'db-prefs.php';
5 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
7 function purge_old_posts($link) {
9 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds");
11 while ($line = db_fetch_assoc($result)) {
13 $feed_id = $line["id"];
14 $purge_interval = $line["purge_interval"];
16 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
18 if ($purge_interval > 0) {
20 if (DB_TYPE
== "pgsql") {
21 db_query($link, "DELETE FROM ttrss_entries WHERE
22 marked = false AND feed_id = '$feed_id' AND
23 date_entered < NOW() - INTERVAL '$purge_interval days'");
25 db_query($link, "DELETE FROM ttrss_entries WHERE
26 marked = false AND feed_id = '$feed_id' AND
27 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
33 function update_all_feeds($link, $fetch) {
35 if (WEB_DEMO_MODE
) return;
37 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
38 if (!$_GET["daemon"]) {
43 db_query($link, "BEGIN");
45 $result = db_query($link, "SELECT feed_url,id,
46 substring(last_updated,1,19) as last_updated,
47 update_interval FROM ttrss_feeds");
49 while ($line = db_fetch_assoc($result)) {
50 $upd_intl = $line["update_interval"];
52 if (!$upd_intl ||
$upd_intl == 0) {
53 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
56 if (!$line["last_updated"] ||
57 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
59 update_rss_feed($link, $line["feed_url"], $line["id"]);
63 purge_old_posts($link);
65 db_query($link, "COMMIT");
69 function check_feed_favicon($feed_url, $feed) {
70 $feed_url = str_replace("http://", "", $feed_url);
71 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
73 $icon_url = "http://$feed_url/favicon.ico";
74 $icon_file = get_pref($link, 'ICONS_DIR') . "/$feed.ico";
76 if (!file_exists($icon_file)) {
79 $r = fopen($icon_url, "r");
80 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
83 $tmpfname = tempnam("/tmp", "ttrssicon");
85 $t = fopen($tmpfname, "w");
88 $buf = fread($r, 16384);
96 if (!rename($tmpfname, $icon_file)) {
99 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
105 function update_rss_feed($link, $feed_url, $feed) {
107 if (WEB_DEMO_MODE
) return;
109 $feed = db_escape_string($feed);
112 $rss = fetch_rss($feed_url);
114 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
116 db_query($link, "BEGIN");
118 $feed = db_escape_string($feed);
122 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
123 check_feed_favicon($feed_url, $feed);
126 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
128 $registered_title = db_fetch_result($result, 0, "title");
129 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
131 if (!$registered_title) {
132 $feed_title = db_escape_string($rss->channel
["title"]);
133 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
136 // print "I: " . $rss->channel["image"]["url"];
138 $icon_url = $rss->image
["url"];
140 if ($icon_url && !$orig_icon_url) {
141 $icon_url = db_escape_string($icon_url);
142 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
148 $result = db_query($link, "SELECT reg_exp,
149 (SELECT name FROM ttrss_filter_types
150 WHERE id = filter_type) as name
151 FROM ttrss_filters");
153 while ($line = db_fetch_assoc($result)) {
154 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
155 array_push($filters[$line["name"]], $line["reg_exp"]);
158 foreach ($rss->items
as $item) {
160 $entry_guid = $item["id"];
162 if (!$entry_guid) $entry_guid = $item["guid"];
163 if (!$entry_guid) $entry_guid = $item["link"];
165 if (!$entry_guid) continue;
167 $entry_timestamp = "";
169 $rss_2_date = $item['pubdate'];
170 $rss_1_date = $item['dc']['date'];
171 $atom_date = $item['issued'];
173 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
174 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
175 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
177 if ($entry_timestamp == "") {
178 $entry_timestamp = time();
179 $no_orig_date = 'true';
181 $no_orig_date = 'false';
184 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
186 $entry_title = $item["title"];
187 $entry_link = $item["link"];
189 if (!$entry_title) continue;
190 if (!$entry_link) continue;
192 $entry_content = $item["content:escaped"];
194 if (!$entry_content) $entry_content = $item["content:encoded"];
195 if (!$entry_content) $entry_content = $item["content"];
196 if (!$entry_content) $entry_content = $item["description"];
198 // if (!$entry_content) continue;
201 if (is_array($entry_content)) {
202 $entry_content = $entry_content["encoded"];
203 if (!$entry_content) $entry_content = $entry_content["escaped"];
207 // print_r($entry_content);
209 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
211 $entry_comments = $item["comments"];
213 $entry_guid = db_escape_string($entry_guid);
215 $result = db_query($link, "
217 id,last_read,no_orig_date,title,feed_id,content_hash,
218 substring(updated,1,19) as updated
222 guid = '$entry_guid'");
224 // print db_num_rows($result) . "$entry_guid<br/>";
226 if (db_num_rows($result) == 0) {
229 if (is_filtered($entry_title, $entry_content, $filters)) {
232 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
234 //$entry_guid = db_escape_string($entry_guid);
235 $entry_content = db_escape_string($entry_content);
236 $entry_title = db_escape_string($entry_title);
237 $entry_link = db_escape_string($entry_link);
238 $entry_comments = db_escape_string($entry_comments);
256 '$entry_timestamp_fmt',
264 $result = db_query($link, $query);
268 $orig_entry_id = db_fetch_result($result, 0, "id");
269 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
271 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
273 if ($orig_feed_id != $feed) {
274 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
278 $entry_is_modified = false;
280 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
282 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
283 $orig_last_read = db_fetch_result($result, 0, "last_read");
284 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
285 $orig_title = db_fetch_result($result, 0, "title");
287 $last_read_qpart = "";
289 if ($orig_content_hash != $content_hash) {
290 // print "$orig_content_hash :: $content_hash<br>";
292 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
293 $last_read_qpart = 'last_read = null,';
295 $entry_is_modified = true;
298 if ($orig_title != $entry_title) {
299 $entry_is_modified = true;
302 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
303 $entry_is_modified = true;
306 if ($entry_is_modified) {
308 // print "$entry_guid Modified!<br>";
310 $entry_comments = db_escape_string($entry_comments);
311 $entry_content = db_escape_string($entry_content);
312 $entry_title = db_escape_string($entry_title);
313 $entry_link = db_escape_string($entry_link);
315 $query = "UPDATE ttrss_entries
318 title = '$entry_title',
319 link = '$entry_link',
320 updated = '$entry_timestamp_fmt',
321 content = '$entry_content',
322 comments = '$entry_comments',
323 content_hash = '$content_hash'
325 id = '$orig_entry_id'";
327 $result = db_query($link, $query);
332 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
336 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
339 $entry_tags = $entry_tags[1];
341 if (count($entry_tags) > 0) {
343 $result = db_query($link, "SELECT id FROM ttrss_entries
344 WHERE guid = '$entry_guid'");
346 if (!$result ||
db_num_rows($result) != 1) {
350 $entry_id = db_fetch_result($result, 0, "id");
352 foreach ($entry_tags as $tag) {
353 $tag = db_escape_string(strtolower($tag));
355 $result = db_query($link, "SELECT id FROM ttrss_tags
356 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
358 if ($result && db_num_rows($result) == 0) {
360 // print "tagging $entry_id as $tag<br>";
362 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
363 VALUES ('$tag', '$entry_id')");
369 db_query($link, "UPDATE ttrss_feeds
370 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
373 $error_msg = db_escape_string(magpie_error());
375 "UPDATE ttrss_feeds SET last_error = '$error_msg',
376 last_updated = NOW() WHERE id = '$feed'");
379 db_query($link, "COMMIT");
383 function print_select($id, $default, $values, $attributes = "") {
384 print "<select id=\"$id\" $attributes>";
385 foreach ($values as $v) {
391 print "<option$sel>$v</option>";
396 function is_filtered($title, $content, $filters) {
398 if ($filters["title"]) {
399 foreach ($filters["title"] as $title_filter) {
400 if (preg_match("/$title_filter/i", $title))
405 if ($filters["content"]) {
406 foreach ($filters["content"] as $content_filter) {
407 if (preg_match("/$content_filter/i", $content))
412 if ($filters["both"]) {
413 foreach ($filters["both"] as $filter) {
414 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
422 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
424 if (file_exists($icon_file) && filesize($icon_file) > 0) {
425 $feed_icon = "<img src=\"$icon_file\">";
427 $feed_icon = "<img src=\"images/blank_icon.gif\">";
430 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
432 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
433 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
437 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
442 $fctr_class = "class=\"invisible\"";
445 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
446 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
452 function getmicrotime() {
453 list($usec, $sec) = explode(" ",microtime());
454 return ((float)$usec +
(float)$sec);