2 require_once 'config.php';
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS
> 0) {
8 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
10 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS
." days'");
14 function update_all_feeds($link, $fetch) {
16 if (WEB_DEMO_MODE
) return;
18 db_query($link, "BEGIN");
20 $result = db_query($link, "SELECT feed_url,id,last_updated FROM ttrss_feeds");
22 while ($line = db_fetch_assoc($result)) {
23 if ($line["last_updated"] && time() - strtotime($line["last_updated"]) > 1800) {
24 update_rss_feed($link, $line["feed_url"], $line["id"]);
28 purge_old_posts($link);
30 db_query($link, "COMMIT");
34 function check_feed_favicon($feed_url, $feed) {
35 $feed_url = str_replace("http://", "", $feed_url);
36 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
38 $icon_url = "http://$feed_url/favicon.ico";
39 $icon_file = ICONS_DIR
. "/$feed.ico";
41 if (!file_exists($icon_file)) {
44 $r = fopen($icon_url, "r");
45 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
48 $tmpfname = tempnam("/tmp", "ttrssicon");
50 $t = fopen($tmpfname, "w");
53 $buf = fread($r, 16384);
61 if (!rename($tmpfname, $icon_file)) {
64 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
70 function update_rss_feed($link, $feed_url, $feed) {
72 if (WEB_DEMO_MODE
) return;
75 $rss = fetch_rss($feed_url);
76 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
78 db_query($link, "BEGIN");
80 $feed = db_escape_string($feed);
84 if (ENABLE_FEED_ICONS
) {
85 check_feed_favicon($feed_url, $feed);
88 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
90 $registered_title = db_fetch_result($result, 0, "title");
91 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
93 if (!$registered_title) {
94 $feed_title = $rss->channel
["title"];
95 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
98 // print "I: " . $rss->channel["image"]["url"];
100 $icon_url = $rss->image
["url"];
102 if ($icon_url && !$orig_icon_url) {
103 $icon_url = db_escape_string($icon_url);
104 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
110 $result = db_query($link, "SELECT reg_exp,
111 (SELECT name FROM ttrss_filter_types
112 WHERE id = filter_type) as name
113 FROM ttrss_filters");
115 while ($line = db_fetch_assoc($result)) {
116 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
117 array_push($filters[$line["name"]], $line["reg_exp"]);
120 foreach ($rss->items
as $item) {
122 $entry_guid = $item["id"];
124 if (!$entry_guid) $entry_guid = $item["guid"];
125 if (!$entry_guid) $entry_guid = $item["link"];
127 if (!$entry_guid) continue;
129 $entry_timestamp = "";
131 $rss_2_date = $item['pubdate'];
132 $rss_1_date = $item['dc']['date'];
133 $atom_date = $item['issued'];
135 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
136 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
137 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
139 if ($entry_timestamp == "") {
140 $entry_timestamp = time();
141 $no_orig_date = 'true';
143 $no_orig_date = 'false';
146 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
148 $entry_title = $item["title"];
149 $entry_link = $item["link"];
151 if (!$entry_title) continue;
152 if (!$entry_link) continue;
154 $entry_content = $item["description"];
155 if (!$entry_content) $entry_content = $item["content:escaped"];
156 if (!$entry_content) $entry_content = $item["content"];
158 // if (!$entry_content) continue;
160 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
162 $entry_comments = $item["comments"];
164 $entry_guid = db_escape_string($entry_guid);
166 if (DB_TYPE
== "pgsql") {
167 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
170 $result = db_query($link, "
172 id,last_read,no_orig_date,title,feed_id,content_hash
177 guid = '$entry_guid'");
179 if (db_num_rows($result) == 0) {
182 if (is_filtered($entry_title, $entry_content, $filters)) {
185 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
187 //$entry_guid = db_escape_string($entry_guid);
188 $entry_content = db_escape_string($entry_content);
189 $entry_title = db_escape_string($entry_title);
190 $entry_link = db_escape_string($entry_link);
191 $entry_comments = db_escape_string($entry_comments);
208 '$entry_timestamp_fmt',
215 $result = db_query($link, $query);
219 $orig_entry_id = db_fetch_result($result, 0, "id");
220 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
222 if ($orig_feed_id != $feed) {
223 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
227 $entry_is_modified = false;
229 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
230 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
231 $orig_last_read = db_fetch_result($result, 0, "last_read");
232 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
233 $orig_title = db_fetch_result($result, 0, "title");
235 $last_read_qpart = "";
237 if ($orig_content_hash != $content_hash) {
238 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
239 $last_read_qpart = 'last_read = null,';
241 $entry_is_modified = true;
244 if ($orig_title != $entry_title) {
245 $entry_is_modified = true;
248 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
249 $entry_is_modified = true;
252 if ($entry_is_modified) {
254 $entry_comments = db_escape_string($entry_comments);
255 $entry_content = db_escape_string($entry_content);
256 $entry_title = db_escape_string($entry_title);
257 $entry_link = db_escape_string($entry_link);
259 $query = "UPDATE ttrss_entries
262 title = '$entry_title',
263 link = '$entry_link',
264 updated = '$entry_timestamp_fmt',
265 content = '$entry_content',
266 comments = '$entry_comments',
267 content_hash = '$content_hash'
269 id = '$orig_entry_id'";
271 $result = db_query($link, $query);
276 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
280 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
283 $entry_tags = $entry_tags[1];
285 if (count($entry_tags) > 0) {
287 $result = db_query($link, "SELECT id FROM ttrss_entries
288 WHERE guid = '$entry_guid'");
290 if (!$result ||
db_num_rows($result) != 1) {
294 $entry_id = db_fetch_result($result, 0, "id");
296 foreach ($entry_tags as $tag) {
297 $tag = db_escape_string(strtolower($tag));
299 $result = db_query($link, "SELECT id FROM ttrss_tags
300 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
302 if ($result && db_num_rows($result) == 0) {
304 // print "tagging $entry_id as $tag<br>";
306 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
307 VALUES ('$tag', '$entry_id')");
313 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
317 db_query($link, "COMMIT");
321 function print_select($id, $default, $values, $attributes = "") {
322 print "<select id=\"$id\" $attributes>";
323 foreach ($values as $v) {
329 print "<option$sel>$v</option>";
334 function is_filtered($title, $content, $filters) {
336 if ($filters["title"]) {
337 foreach ($filters["title"] as $title_filter) {
338 if (preg_match("/$title_filter/i", $title))
343 if ($filters["content"]) {
344 foreach ($filters["content"] as $content_filter) {
345 if (preg_match("/$content_filter/i", $content))
350 if ($filters["both"]) {
351 foreach ($filters["both"] as $filter) {
352 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
360 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
362 if (file_exists($icon_file) && filesize($icon_file) > 0) {
363 $feed_icon = "<img src=\"$icon_file\">";
365 $feed_icon = "<img src=\"images/blank_icon.gif\">";
368 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
370 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
371 if (ENABLE_FEED_ICONS
) {
375 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
380 $fctr_class = "class=\"invisible\"";
383 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
384 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";