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["content:escaped"];
163 if (!$entry_content) $entry_content = $item["content:encoded"];
164 if (!$entry_content) $entry_content = $item["content"];
165 if (!$entry_content) $entry_content = $item["description"];
167 // if (!$entry_content) continue;
170 if (is_array($entry_content)) {
171 $entry_content = $entry_content["encoded"];
172 if (!$entry_content) $entry_content = $entry_content["escaped"];
176 // print_r($entry_content);
178 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
180 $entry_comments = $item["comments"];
182 $entry_guid = db_escape_string($entry_guid);
184 if (DB_TYPE
== "pgsql") {
185 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
188 $result = db_query($link, "
190 id,last_read,no_orig_date,title,feed_id,content_hash
195 guid = '$entry_guid'");
197 // print db_num_rows($result) . "$entry_guid<br/>";
199 if (db_num_rows($result) == 0) {
202 if (is_filtered($entry_title, $entry_content, $filters)) {
205 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
207 //$entry_guid = db_escape_string($entry_guid);
208 $entry_content = db_escape_string($entry_content);
209 $entry_title = db_escape_string($entry_title);
210 $entry_link = db_escape_string($entry_link);
211 $entry_comments = db_escape_string($entry_comments);
229 '$entry_timestamp_fmt',
237 $result = db_query($link, $query);
241 $orig_entry_id = db_fetch_result($result, 0, "id");
242 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
244 if ($orig_feed_id != $feed) {
245 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
249 $entry_is_modified = false;
251 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
252 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
253 $orig_last_read = db_fetch_result($result, 0, "last_read");
254 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
255 $orig_title = db_fetch_result($result, 0, "title");
257 $last_read_qpart = "";
259 if ($orig_content_hash != $content_hash) {
260 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
261 $last_read_qpart = 'last_read = null,';
263 $entry_is_modified = true;
266 if ($orig_title != $entry_title) {
267 $entry_is_modified = true;
270 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
271 $entry_is_modified = true;
274 if ($entry_is_modified) {
276 $entry_comments = db_escape_string($entry_comments);
277 $entry_content = db_escape_string($entry_content);
278 $entry_title = db_escape_string($entry_title);
279 $entry_link = db_escape_string($entry_link);
281 $query = "UPDATE ttrss_entries
284 title = '$entry_title',
285 link = '$entry_link',
286 updated = '$entry_timestamp_fmt',
287 content = '$entry_content',
288 comments = '$entry_comments',
289 content_hash = '$content_hash'
291 id = '$orig_entry_id'";
293 $result = db_query($link, $query);
298 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
302 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
305 $entry_tags = $entry_tags[1];
307 if (count($entry_tags) > 0) {
309 $result = db_query($link, "SELECT id FROM ttrss_entries
310 WHERE guid = '$entry_guid'");
312 if (!$result ||
db_num_rows($result) != 1) {
316 $entry_id = db_fetch_result($result, 0, "id");
318 foreach ($entry_tags as $tag) {
319 $tag = db_escape_string(strtolower($tag));
321 $result = db_query($link, "SELECT id FROM ttrss_tags
322 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
324 if ($result && db_num_rows($result) == 0) {
326 // print "tagging $entry_id as $tag<br>";
328 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
329 VALUES ('$tag', '$entry_id')");
335 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
339 db_query($link, "COMMIT");
343 function print_select($id, $default, $values, $attributes = "") {
344 print "<select id=\"$id\" $attributes>";
345 foreach ($values as $v) {
351 print "<option$sel>$v</option>";
356 function is_filtered($title, $content, $filters) {
358 if ($filters["title"]) {
359 foreach ($filters["title"] as $title_filter) {
360 if (preg_match("/$title_filter/i", $title))
365 if ($filters["content"]) {
366 foreach ($filters["content"] as $content_filter) {
367 if (preg_match("/$content_filter/i", $content))
372 if ($filters["both"]) {
373 foreach ($filters["both"] as $filter) {
374 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
382 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
384 if (file_exists($icon_file) && filesize($icon_file) > 0) {
385 $feed_icon = "<img src=\"$icon_file\">";
387 $feed_icon = "<img src=\"images/blank_icon.gif\">";
390 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
392 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
393 if (ENABLE_FEED_ICONS
) {
397 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
402 $fctr_class = "class=\"invisible\"";
405 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
406 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";