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
);
89 if (ENABLE_FEED_ICONS
) {
90 check_feed_favicon($feed_url, $feed);
93 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
95 $registered_title = pg_fetch_result($result, 0, "title");
97 if (!$registered_title) {
98 $feed_title = $rss->channel
["title"];
99 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
102 foreach ($rss->items
as $item) {
104 $entry_guid = $item["id"];
106 if (!$entry_guid) $entry_guid = $item["guid"];
107 if (!$entry_guid) $entry_guid = $item["link"];
109 if (!$entry_guid) continue;
111 $entry_timestamp = "";
113 $rss_2_date = $item['pubdate'];
114 $rss_1_date = $item['dc']['date'];
115 $atom_date = $item['issued'];
117 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
118 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
119 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
121 if ($entry_timestamp == "") {
122 $entry_timestamp = time();
123 $no_orig_date = 'true';
125 $no_orig_date = 'false';
128 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
130 $entry_title = $item["title"];
131 $entry_link = $item["link"];
133 if (!$entry_title) continue;
134 if (!$entry_link) continue;
136 $entry_content = $item["description"];
137 if (!$entry_content) $entry_content = $item["content:escaped"];
138 if (!$entry_content) $entry_content = $item["content"];
140 if (!$entry_content) continue;
142 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
144 $result = pg_query($link, "
146 id,last_read,no_orig_date,title,feed_id,content_hash,
147 EXTRACT(EPOCH FROM updated) as updated_timestamp
151 guid = '$entry_guid'");
153 if (pg_num_rows($result) == 0) {
155 $entry_content = pg_escape_string($entry_content);
156 $entry_title = pg_escape_string($entry_title);
157 $entry_link = pg_escape_string($entry_link);
173 '$entry_timestamp_fmt',
179 $result = pg_query($link, $query);
183 $orig_entry_id = pg_fetch_result($result, 0, "id");
184 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
186 if ($orig_feed_id != $feed) {
187 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
191 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
192 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
193 $orig_last_read = pg_fetch_result($result, 0, "last_read");
194 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
195 $orig_title = pg_fetch_result($result, 0, "title");
197 $last_read_qpart = "";
199 // if ("$orig_title" != "$entry_title") {
200 // $last_read_qpart = 'last_read = null,';
203 if (UPDATE_POST_ON_CHECKSUM_CHANGE
&&
204 $orig_content_hash != $content_hash) {
206 print "$orig_content_hash : $content_hash";
208 $last_read_qpart = 'last_read = null,';
211 // if ($orig_timestamp < $entry_timestamp) {
212 // $last_read_qpart = 'last_read = null,';
215 $entry_content = pg_escape_string($entry_content);
216 $entry_title = pg_escape_string($entry_title);
217 $entry_link = pg_escape_string($entry_link);
219 $query = "UPDATE ttrss_entries
222 title = '$entry_title',
223 link = '$entry_link',
224 updated = '$entry_timestamp_fmt',
225 content = '$entry_content',
226 content_hash = '$content_hash'
228 id = '$orig_entry_id'";
230 $result = pg_query($link, $query);
236 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");