2 require_once 'config.php';
4 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
6 function purge_old_posts() {
8 $result = pg_query("DELETE FROM ttrss_entries WHERE
9 date_entered < NOW() - INTERVAL '30 days'");
13 function update_all_feeds($link, $fetch) {
15 if (WEB_DEMO_MODE
) return;
21 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
22 last_updated is null OR title = '' OR
23 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
28 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
31 while ($line = pg_fetch_assoc($result)) {
32 update_rss_feed($link, $line["feed_url"], $line["id"]);
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
);
87 $feed = pg_escape_string($feed);
91 if (ENABLE_FEED_ICONS
) {
92 check_feed_favicon($feed_url, $feed);
95 $result = pg_query("SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
97 $registered_title = pg_fetch_result($result, 0, "title");
98 $orig_icon_url = pg_fetch_result($result, 0, "icon_url");
100 if (!$registered_title) {
101 $feed_title = $rss->channel
["title"];
102 pg_query("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 = pg_escape_string($icon_url);
111 pg_query("UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
114 foreach ($rss->items
as $item) {
116 $entry_guid = $item["id"];
118 if (!$entry_guid) $entry_guid = $item["guid"];
119 if (!$entry_guid) $entry_guid = $item["link"];
121 if (!$entry_guid) continue;
123 $entry_timestamp = "";
125 $rss_2_date = $item['pubdate'];
126 $rss_1_date = $item['dc']['date'];
127 $atom_date = $item['issued'];
129 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
130 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
131 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
133 if ($entry_timestamp == "") {
134 $entry_timestamp = time();
135 $no_orig_date = 'true';
137 $no_orig_date = 'false';
140 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
142 $entry_title = $item["title"];
143 $entry_link = $item["link"];
145 if (!$entry_title) continue;
146 if (!$entry_link) continue;
148 $entry_content = $item["description"];
149 if (!$entry_content) $entry_content = $item["content:escaped"];
150 if (!$entry_content) $entry_content = $item["content"];
152 // if (!$entry_content) continue;
154 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
156 $entry_comments = $item["comments"];
158 $result = pg_query($link, "
160 id,last_read,no_orig_date,title,feed_id,content_hash,
161 EXTRACT(EPOCH FROM updated) as updated_timestamp
165 guid = '$entry_guid'");
167 if (pg_num_rows($result) == 0) {
169 $entry_guid = pg_escape_string($entry_guid);
170 $entry_content = pg_escape_string($entry_content);
171 $entry_title = pg_escape_string($entry_title);
172 $entry_link = pg_escape_string($entry_link);
173 $entry_comments = pg_escape_string($entry_comments);
190 '$entry_timestamp_fmt',
197 $result = pg_query($link, $query);
201 $orig_entry_id = pg_fetch_result($result, 0, "id");
202 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
204 if ($orig_feed_id != $feed) {
205 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
209 $entry_is_modified = false;
211 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
212 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
213 $orig_last_read = pg_fetch_result($result, 0, "last_read");
214 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
215 $orig_title = pg_fetch_result($result, 0, "title");
217 $last_read_qpart = "";
219 // if ("$orig_title" != "$entry_title") {
220 // $last_read_qpart = 'last_read = null,';
223 if ($orig_content_hash != $content_hash) {
224 if (UPDATE_POST_ON_CHECKSUM_CHANGE
) {
225 $last_read_qpart = 'last_read = null,';
227 $entry_is_modified = true;
230 if ($orig_title != $entry_title) {
231 $entry_is_modified = true;
234 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
235 $entry_is_modified = true;
238 // if (!$no_orig_date && $orig_timestamp < $entry_timestamp) {
239 // $last_read_qpart = 'last_read = null,';
242 if ($entry_is_modified) {
244 $entry_comments = pg_escape_string($entry_comments);
245 $entry_content = pg_escape_string($entry_content);
246 $entry_title = pg_escape_string($entry_title);
247 $entry_link = pg_escape_string($entry_link);
249 // print "update object $entry_guid<br>";
251 $query = "UPDATE ttrss_entries
254 title = '$entry_title',
255 link = '$entry_link',
256 updated = '$entry_timestamp_fmt',
257 content = '$entry_content',
258 comments = '$entry_comments',
259 content_hash = '$content_hash'
261 id = '$orig_entry_id'";
263 $result = pg_query($link, $query);
269 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");