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;
161 if (is_array($entry_content)) {
162 $entry_content = $entry_content["encoded"];
165 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
167 $entry_comments = $item["comments"];
169 $entry_guid = db_escape_string($entry_guid);
171 if (DB_TYPE
== "pgsql") {
172 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
175 $result = db_query($link, "
177 id,last_read,no_orig_date,title,feed_id,content_hash
182 guid = '$entry_guid'");
184 if (db_num_rows($result) == 0) {
187 if (is_filtered($entry_title, $entry_content, $filters)) {
190 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
192 //$entry_guid = db_escape_string($entry_guid);
193 $entry_content = db_escape_string($entry_content);
194 $entry_title = db_escape_string($entry_title);
195 $entry_link = db_escape_string($entry_link);
196 $entry_comments = db_escape_string($entry_comments);
213 '$entry_timestamp_fmt',
220 $result = db_query($link, $query);
224 $orig_entry_id = db_fetch_result($result, 0, "id");
225 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
227 if ($orig_feed_id != $feed) {
228 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
232 $entry_is_modified = false;
234 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
235 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
236 $orig_last_read = db_fetch_result($result, 0, "last_read");
237 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
238 $orig_title = db_fetch_result($result, 0, "title");
240 $last_read_qpart = "";
242 if ($orig_content_hash != $content_hash) {
243 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
244 $last_read_qpart = 'last_read = null,';
246 $entry_is_modified = true;
249 if ($orig_title != $entry_title) {
250 $entry_is_modified = true;
253 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
254 $entry_is_modified = true;
257 if ($entry_is_modified) {
259 $entry_comments = db_escape_string($entry_comments);
260 $entry_content = db_escape_string($entry_content);
261 $entry_title = db_escape_string($entry_title);
262 $entry_link = db_escape_string($entry_link);
264 $query = "UPDATE ttrss_entries
267 title = '$entry_title',
268 link = '$entry_link',
269 updated = '$entry_timestamp_fmt',
270 content = '$entry_content',
271 comments = '$entry_comments',
272 content_hash = '$content_hash'
274 id = '$orig_entry_id'";
276 $result = db_query($link, $query);
281 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
285 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
288 $entry_tags = $entry_tags[1];
290 if (count($entry_tags) > 0) {
292 $result = db_query($link, "SELECT id FROM ttrss_entries
293 WHERE guid = '$entry_guid'");
295 if (!$result ||
db_num_rows($result) != 1) {
299 $entry_id = db_fetch_result($result, 0, "id");
301 foreach ($entry_tags as $tag) {
302 $tag = db_escape_string(strtolower($tag));
304 $result = db_query($link, "SELECT id FROM ttrss_tags
305 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
307 if ($result && db_num_rows($result) == 0) {
309 // print "tagging $entry_id as $tag<br>";
311 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
312 VALUES ('$tag', '$entry_id')");
318 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
322 db_query($link, "COMMIT");
326 function print_select($id, $default, $values, $attributes = "") {
327 print "<select id=\"$id\" $attributes>";
328 foreach ($values as $v) {
334 print "<option$sel>$v</option>";
339 function is_filtered($title, $content, $filters) {
341 if ($filters["title"]) {
342 foreach ($filters["title"] as $title_filter) {
343 if (preg_match("/$title_filter/i", $title))
348 if ($filters["content"]) {
349 foreach ($filters["content"] as $content_filter) {
350 if (preg_match("/$content_filter/i", $content))
355 if ($filters["both"]) {
356 foreach ($filters["both"] as $filter) {
357 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
365 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
367 if (file_exists($icon_file) && filesize($icon_file) > 0) {
368 $feed_icon = "<img src=\"$icon_file\">";
370 $feed_icon = "<img src=\"images/blank_icon.gif\">";
373 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
375 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
376 if (ENABLE_FEED_ICONS
) {
380 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
385 $fctr_class = "class=\"invisible\"";
388 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
389 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";