2 require_once 'config.php';
4 function purge_old_posts() {
6 $result = pg_query("DELETE FROM ttrss_entries WHERE
7 date_entered < NOW() - INTERVAL '30 days'");
11 function update_all_feeds($link, $fetch) {
13 if (WEB_DEMO_MODE
) return;
19 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
20 last_updated is null OR title = '' OR
21 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
26 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
29 while ($line = pg_fetch_assoc($result)) {
30 update_rss_feed($link, $line["feed_url"], $line["id"]);
39 function check_feed_favicon($feed_url, $feed) {
40 $feed_url = str_replace("http://", "", $feed_url);
41 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
43 $icon_url = "http://$feed_url/favicon.ico";
44 $icon_file = ICONS_DIR
. "/$feed.ico";
46 if (!file_exists($icon_file)) {
49 $r = fopen($icon_url, "r");
50 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
53 $tmpfname = tempnam("/tmp", "ttrssicon");
55 $t = fopen($tmpfname, "w");
58 $buf = fread($r, 16384);
66 if (!rename($tmpfname, $icon_file)) {
69 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
75 function update_rss_feed($link, $feed_url, $feed) {
77 if (WEB_DEMO_MODE
) return;
80 $rss = fetch_rss($feed_url);
81 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
87 if (ENABLE_FEED_ICONS
) {
88 check_feed_favicon($feed_url, $feed);
91 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
93 $registered_title = pg_fetch_result($result, 0, "title");
95 if (!$registered_title) {
96 $feed_title = $rss->channel
["title"];
97 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
100 foreach ($rss->items
as $item) {
102 $entry_guid = $item["id"];
104 if (!$entry_guid) $entry_guid = $item["guid"];
105 if (!$entry_guid) $entry_guid = $item["link"];
107 if (!$entry_guid) continue;
109 $entry_timestamp = "";
111 $rss_2_date = $item['pubdate'];
112 $rss_1_date = $item['dc']['date'];
113 $atom_date = $item['issued'];
115 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
116 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
117 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
119 if ($entry_timestamp == "") {
120 $entry_timestamp = time();
121 $no_orig_date = 'true';
123 $no_orig_date = 'false';
126 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
128 $entry_title = $item["title"];
129 $entry_link = $item["link"];
131 if (!$entry_title) continue;
132 if (!$entry_link) continue;
134 $entry_content = $item["description"];
135 if (!$entry_content) $entry_content = $item["content:escaped"];
136 if (!$entry_content) $entry_content = $item["content"];
138 if (!$entry_content) continue;
140 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
142 $result = pg_query($link, "
144 id,last_read,no_orig_date,title,feed_id,content_hash,
145 EXTRACT(EPOCH FROM updated) as updated_timestamp
149 guid = '$entry_guid'");
151 if (pg_num_rows($result) == 0) {
153 $entry_content = pg_escape_string($entry_content);
154 $entry_title = pg_escape_string($entry_title);
155 $entry_link = pg_escape_string($entry_link);
171 '$entry_timestamp_fmt',
177 $result = pg_query($link, $query);
181 $orig_entry_id = pg_fetch_result($result, 0, "id");
182 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
184 if ($orig_feed_id != $feed) {
185 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
189 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
190 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
191 $orig_last_read = pg_fetch_result($result, 0, "last_read");
192 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
193 $orig_title = pg_fetch_result($result, 0, "title");
195 if ($orig_title != $entry_title) {
196 $last_read_qpart = 'last_read = null,';
199 if (UPDATE_POST_ON_CHECKSUM_CHANGE
&&
200 $orig_content_hash != $content_hash) {
202 $last_read_qpart = 'last_read = null,';
205 // if ($orig_timestamp < $entry_timestamp) {
206 // $last_read_qpart = 'last_read = null,';
209 $entry_content = pg_escape_string($entry_content);
210 $entry_title = pg_escape_string($entry_title);
211 $entry_link = pg_escape_string($entry_link);
213 $query = "UPDATE ttrss_entries
216 title = '$entry_title',
217 link = '$entry_link',
218 updated = '$entry_timestamp_fmt',
219 content = '$entry_content',
220 content_hash = '$content_hash'
222 id = '$orig_entry_id'";
224 $result = pg_query($link, $query);
230 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");