2 require_once 'config.php';
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
6 function purge_old_posts($link) {
7 if (PURGE_OLD_DAYS
> 0) {
9 if (DB_TYPE
== "pgsql") {
10 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
12 date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS
." days'");
14 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
16 date_entered < DATE_SUB(NOW(), INTERVAL ".PURGE_OLD_DAYS
." DAY)");
21 function update_all_feeds($link, $fetch) {
23 if (WEB_DEMO_MODE
) return;
25 db_query($link, "BEGIN");
27 $result = db_query($link, "SELECT feed_url,id,last_updated FROM ttrss_feeds");
29 while ($line = db_fetch_assoc($result)) {
30 if (!$line["last_updated"] ||
time() - strtotime($line["last_updated"]) > 1800) {
31 update_rss_feed($link, $line["feed_url"], $line["id"]);
35 purge_old_posts($link);
37 db_query($link, "COMMIT");
41 function check_feed_favicon($feed_url, $feed) {
42 $feed_url = str_replace("http://", "", $feed_url);
43 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
45 $icon_url = "http://$feed_url/favicon.ico";
46 $icon_file = ICONS_DIR
. "/$feed.ico";
48 if (!file_exists($icon_file)) {
51 $r = fopen($icon_url, "r");
52 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
55 $tmpfname = tempnam("/tmp", "ttrssicon");
57 $t = fopen($tmpfname, "w");
60 $buf = fread($r, 16384);
68 if (!rename($tmpfname, $icon_file)) {
71 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
77 function update_rss_feed($link, $feed_url, $feed) {
79 if (WEB_DEMO_MODE
) return;
82 $rss = fetch_rss($feed_url);
83 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
85 db_query($link, "BEGIN");
87 $feed = db_escape_string($feed);
91 if (ENABLE_FEED_ICONS
) {
92 check_feed_favicon($feed_url, $feed);
95 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
97 $registered_title = db_fetch_result($result, 0, "title");
98 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
100 if (!$registered_title) {
101 $feed_title = db_escape_string($rss->channel
["title"]);
102 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
105 // print "I: " . $rss->channel["image"]["url"];
107 $icon_url = $rss->image
["url"];
109 if ($icon_url && !$orig_icon_url) {
110 $icon_url = db_escape_string($icon_url);
111 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
117 $result = db_query($link, "SELECT reg_exp,
118 (SELECT name FROM ttrss_filter_types
119 WHERE id = filter_type) as name
120 FROM ttrss_filters");
122 while ($line = db_fetch_assoc($result)) {
123 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
124 array_push($filters[$line["name"]], $line["reg_exp"]);
127 foreach ($rss->items
as $item) {
129 $entry_guid = $item["id"];
131 if (!$entry_guid) $entry_guid = $item["guid"];
132 if (!$entry_guid) $entry_guid = $item["link"];
134 if (!$entry_guid) continue;
136 $entry_timestamp = "";
138 $rss_2_date = $item['pubdate'];
139 $rss_1_date = $item['dc']['date'];
140 $atom_date = $item['issued'];
142 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
143 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
144 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
146 if ($entry_timestamp == "") {
147 $entry_timestamp = time();
148 $no_orig_date = 'true';
150 $no_orig_date = 'false';
153 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
155 $entry_title = $item["title"];
156 $entry_link = $item["link"];
158 if (!$entry_title) continue;
159 if (!$entry_link) continue;
161 $entry_content = $item["description"];
162 if (!$entry_content) $entry_content = $item["content:escaped"];
163 if (!$entry_content) $entry_content = $item["content"];
165 // if (!$entry_content) continue;
168 if (is_array($entry_content)) {
169 $entry_content = $entry_content["encoded"];
172 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
174 $entry_comments = $item["comments"];
176 $entry_guid = db_escape_string($entry_guid);
178 if (DB_TYPE
== "pgsql") {
179 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
182 $result = db_query($link, "
184 id,last_read,no_orig_date,title,feed_id,content_hash
189 guid = '$entry_guid'");
191 if (db_num_rows($result) == 0) {
194 if (is_filtered($entry_title, $entry_content, $filters)) {
197 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
199 //$entry_guid = db_escape_string($entry_guid);
200 $entry_content = db_escape_string($entry_content);
201 $entry_title = db_escape_string($entry_title);
202 $entry_link = db_escape_string($entry_link);
203 $entry_comments = db_escape_string($entry_comments);
221 '$entry_timestamp_fmt',
229 $result = db_query($link, $query);
233 $orig_entry_id = db_fetch_result($result, 0, "id");
234 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
236 if ($orig_feed_id != $feed) {
237 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
241 $entry_is_modified = false;
243 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
244 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
245 $orig_last_read = db_fetch_result($result, 0, "last_read");
246 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
247 $orig_title = db_fetch_result($result, 0, "title");
249 $last_read_qpart = "";
251 if ($orig_content_hash != $content_hash) {
252 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
253 $last_read_qpart = 'last_read = null,';
255 $entry_is_modified = true;
258 if ($orig_title != $entry_title) {
259 $entry_is_modified = true;
262 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
263 $entry_is_modified = true;
266 if ($entry_is_modified) {
268 $entry_comments = db_escape_string($entry_comments);
269 $entry_content = db_escape_string($entry_content);
270 $entry_title = db_escape_string($entry_title);
271 $entry_link = db_escape_string($entry_link);
273 $query = "UPDATE ttrss_entries
276 title = '$entry_title',
277 link = '$entry_link',
278 updated = '$entry_timestamp_fmt',
279 content = '$entry_content',
280 comments = '$entry_comments',
281 content_hash = '$content_hash'
283 id = '$orig_entry_id'";
285 $result = db_query($link, $query);
290 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
294 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
297 $entry_tags = $entry_tags[1];
299 if (count($entry_tags) > 0) {
301 $result = db_query($link, "SELECT id FROM ttrss_entries
302 WHERE guid = '$entry_guid'");
304 if (!$result ||
db_num_rows($result) != 1) {
308 $entry_id = db_fetch_result($result, 0, "id");
310 foreach ($entry_tags as $tag) {
311 $tag = db_escape_string(strtolower($tag));
313 $result = db_query($link, "SELECT id FROM ttrss_tags
314 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
316 if ($result && db_num_rows($result) == 0) {
318 // print "tagging $entry_id as $tag<br>";
320 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
321 VALUES ('$tag', '$entry_id')");
327 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
331 db_query($link, "COMMIT");
335 function print_select($id, $default, $values, $attributes = "") {
336 print "<select id=\"$id\" $attributes>";
337 foreach ($values as $v) {
343 print "<option$sel>$v</option>";
348 function is_filtered($title, $content, $filters) {
350 if ($filters["title"]) {
351 foreach ($filters["title"] as $title_filter) {
352 if (preg_match("/$title_filter/i", $title))
357 if ($filters["content"]) {
358 foreach ($filters["content"] as $content_filter) {
359 if (preg_match("/$content_filter/i", $content))
364 if ($filters["both"]) {
365 foreach ($filters["both"] as $filter) {
366 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
374 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
376 if (file_exists($icon_file) && filesize($icon_file) > 0) {
377 $feed_icon = "<img src=\"$icon_file\">";
379 $feed_icon = "<img src=\"images/blank_icon.gif\">";
382 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
384 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
385 if (ENABLE_FEED_ICONS
) {
389 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
394 $fctr_class = "class=\"invisible\"";
397 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
398 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";