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,
28 substring(last_updated,1,19) as last_updated,
29 update_interval FROM ttrss_feeds");
31 while ($line = db_fetch_assoc($result)) {
32 $upd_intl = $line["update_interval"];
34 if (!$upd_intl) $upd_intl = MIN_UPDATE_INTERVAL
;
36 if (!$line["last_updated"] ||
37 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
39 update_rss_feed($link, $line["feed_url"], $line["id"]);
43 purge_old_posts($link);
45 db_query($link, "COMMIT");
49 function check_feed_favicon($feed_url, $feed) {
50 $feed_url = str_replace("http://", "", $feed_url);
51 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
53 $icon_url = "http://$feed_url/favicon.ico";
54 $icon_file = ICONS_DIR
. "/$feed.ico";
56 if (!file_exists($icon_file)) {
59 $r = fopen($icon_url, "r");
60 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
63 $tmpfname = tempnam("/tmp", "ttrssicon");
65 $t = fopen($tmpfname, "w");
68 $buf = fread($r, 16384);
76 if (!rename($tmpfname, $icon_file)) {
79 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
85 function update_rss_feed($link, $feed_url, $feed) {
87 if (WEB_DEMO_MODE
) return;
90 $rss = fetch_rss($feed_url);
91 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
93 db_query($link, "BEGIN");
95 $feed = db_escape_string($feed);
99 if (ENABLE_FEED_ICONS
) {
100 check_feed_favicon($feed_url, $feed);
103 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
105 $registered_title = db_fetch_result($result, 0, "title");
106 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
108 if (!$registered_title) {
109 $feed_title = db_escape_string($rss->channel
["title"]);
110 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
113 // print "I: " . $rss->channel["image"]["url"];
115 $icon_url = $rss->image
["url"];
117 if ($icon_url && !$orig_icon_url) {
118 $icon_url = db_escape_string($icon_url);
119 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
125 $result = db_query($link, "SELECT reg_exp,
126 (SELECT name FROM ttrss_filter_types
127 WHERE id = filter_type) as name
128 FROM ttrss_filters");
130 while ($line = db_fetch_assoc($result)) {
131 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
132 array_push($filters[$line["name"]], $line["reg_exp"]);
135 foreach ($rss->items
as $item) {
137 $entry_guid = $item["id"];
139 if (!$entry_guid) $entry_guid = $item["guid"];
140 if (!$entry_guid) $entry_guid = $item["link"];
142 if (!$entry_guid) continue;
144 $entry_timestamp = "";
146 $rss_2_date = $item['pubdate'];
147 $rss_1_date = $item['dc']['date'];
148 $atom_date = $item['issued'];
150 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
151 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
152 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
154 if ($entry_timestamp == "") {
155 $entry_timestamp = time();
156 $no_orig_date = 'true';
158 $no_orig_date = 'false';
161 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
163 $entry_title = $item["title"];
164 $entry_link = $item["link"];
166 if (!$entry_title) continue;
167 if (!$entry_link) continue;
169 $entry_content = $item["content:escaped"];
171 if (!$entry_content) $entry_content = $item["content:encoded"];
172 if (!$entry_content) $entry_content = $item["content"];
173 if (!$entry_content) $entry_content = $item["description"];
175 // if (!$entry_content) continue;
178 if (is_array($entry_content)) {
179 $entry_content = $entry_content["encoded"];
180 if (!$entry_content) $entry_content = $entry_content["escaped"];
184 // print_r($entry_content);
186 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
188 $entry_comments = $item["comments"];
190 $entry_guid = db_escape_string($entry_guid);
192 if (DB_TYPE
== "pgsql") {
193 $extract_ts_qpart = ",EXTRACT(EPOCH FROM updated) as updated_timestamp";
196 $result = db_query($link, "
198 id,last_read,no_orig_date,title,feed_id,content_hash
203 guid = '$entry_guid'");
205 // print db_num_rows($result) . "$entry_guid<br/>";
207 if (db_num_rows($result) == 0) {
210 if (is_filtered($entry_title, $entry_content, $filters)) {
213 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
215 //$entry_guid = db_escape_string($entry_guid);
216 $entry_content = db_escape_string($entry_content);
217 $entry_title = db_escape_string($entry_title);
218 $entry_link = db_escape_string($entry_link);
219 $entry_comments = db_escape_string($entry_comments);
237 '$entry_timestamp_fmt',
245 $result = db_query($link, $query);
249 $orig_entry_id = db_fetch_result($result, 0, "id");
250 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
252 if ($orig_feed_id != $feed) {
253 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
257 $entry_is_modified = false;
259 $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp");
260 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
261 $orig_last_read = db_fetch_result($result, 0, "last_read");
262 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
263 $orig_title = db_fetch_result($result, 0, "title");
265 $last_read_qpart = "";
267 if ($orig_content_hash != $content_hash) {
268 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
269 $last_read_qpart = 'last_read = null,';
271 $entry_is_modified = true;
274 if ($orig_title != $entry_title) {
275 $entry_is_modified = true;
278 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
279 $entry_is_modified = true;
282 if ($entry_is_modified) {
284 $entry_comments = db_escape_string($entry_comments);
285 $entry_content = db_escape_string($entry_content);
286 $entry_title = db_escape_string($entry_title);
287 $entry_link = db_escape_string($entry_link);
289 $query = "UPDATE ttrss_entries
292 title = '$entry_title',
293 link = '$entry_link',
294 updated = '$entry_timestamp_fmt',
295 content = '$entry_content',
296 comments = '$entry_comments',
297 content_hash = '$content_hash'
299 id = '$orig_entry_id'";
301 $result = db_query($link, $query);
306 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
310 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
313 $entry_tags = $entry_tags[1];
315 if (count($entry_tags) > 0) {
317 $result = db_query($link, "SELECT id FROM ttrss_entries
318 WHERE guid = '$entry_guid'");
320 if (!$result ||
db_num_rows($result) != 1) {
324 $entry_id = db_fetch_result($result, 0, "id");
326 foreach ($entry_tags as $tag) {
327 $tag = db_escape_string(strtolower($tag));
329 $result = db_query($link, "SELECT id FROM ttrss_tags
330 WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1");
332 if ($result && db_num_rows($result) == 0) {
334 // print "tagging $entry_id as $tag<br>";
336 db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id)
337 VALUES ('$tag', '$entry_id')");
343 db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
347 db_query($link, "COMMIT");
351 function print_select($id, $default, $values, $attributes = "") {
352 print "<select id=\"$id\" $attributes>";
353 foreach ($values as $v) {
359 print "<option$sel>$v</option>";
364 function is_filtered($title, $content, $filters) {
366 if ($filters["title"]) {
367 foreach ($filters["title"] as $title_filter) {
368 if (preg_match("/$title_filter/i", $title))
373 if ($filters["content"]) {
374 foreach ($filters["content"] as $content_filter) {
375 if (preg_match("/$content_filter/i", $content))
380 if ($filters["both"]) {
381 foreach ($filters["both"] as $filter) {
382 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
390 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) {
392 if (file_exists($icon_file) && filesize($icon_file) > 0) {
393 $feed_icon = "<img src=\"$icon_file\">";
395 $feed_icon = "<img src=\"images/blank_icon.gif\">";
398 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
400 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
401 if (ENABLE_FEED_ICONS
) {
405 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
410 $fctr_class = "class=\"invisible\"";
413 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
414 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";