2 require_once 'config.php';
4 function update_all_feeds($link, $fetch) {
6 if (WEB_DEMO_MODE
) return;
12 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
13 last_updated is null OR title = '' OR
14 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
19 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
22 while ($line = pg_fetch_assoc($result)) {
23 update_rss_feed($link, $line["feed_url"], $line["id"]);
30 function check_feed_favicon($feed_url, $feed) {
31 $feed_url = str_replace("http://", "", $feed_url);
32 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
34 $icon_url = "http://$feed_url/favicon.ico";
35 $icon_file = ICONS_DIR
. "/$feed.ico";
37 if (!file_exists($icon_file)) {
40 $r = fopen($icon_url, "r");
41 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
44 $tmpfname = tempnam("/tmp", "ttrssicon");
46 $t = fopen($tmpfname, "w");
49 $buf = fread($r, 16384);
57 if (!rename($tmpfname, $icon_file)) {
60 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
66 function update_rss_feed($link, $feed_url, $feed) {
68 if (WEB_DEMO_MODE
) return;
71 $rss = fetch_rss($feed_url);
72 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
78 if (ENABLE_FEED_ICONS
) {
79 check_feed_favicon($feed_url, $feed);
82 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
84 $registered_title = pg_fetch_result($result, 0, "title");
86 if (!$registered_title) {
87 $feed_title = $rss->channel
["title"];
88 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
91 foreach ($rss->items
as $item) {
93 $entry_guid = $item["id"];
95 if (!$entry_guid) $entry_guid = $item["guid"];
96 if (!$entry_guid) $entry_guid = $item["link"];
98 if (!$entry_guid) continue;
100 $entry_timestamp = "";
102 $rss_2_date = $item['pubdate'];
103 $rss_1_date = $item['dc']['date'];
104 $atom_date = $item['issued'];
106 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
107 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
108 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
110 if ($entry_timestamp == "") {
111 $entry_timestamp = time();
112 $no_orig_date = 'true';
114 $no_orig_date = 'false';
117 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
119 $entry_title = $item["title"];
120 $entry_link = $item["link"];
122 if (!$entry_title) continue;
123 if (!$entry_link) continue;
125 $entry_content = $item["description"];
126 if (!$entry_content) $entry_content = $item["content:escaped"];
127 if (!$entry_content) $entry_content = $item["content"];
129 if (!$entry_content) continue;
131 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
133 $result = pg_query($link, "
135 id,last_read,no_orig_date,title,feed_id,content_hash,
136 EXTRACT(EPOCH FROM updated) as updated_timestamp
140 guid = '$entry_guid'");
142 if (pg_num_rows($result) == 0) {
144 $entry_content = pg_escape_string($entry_content);
145 $entry_title = pg_escape_string($entry_title);
146 $entry_link = pg_escape_string($entry_link);
162 '$entry_timestamp_fmt',
168 $result = pg_query($link, $query);
172 $orig_entry_id = pg_fetch_result($result, 0, "id");
173 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
175 if ($orig_feed_id != $feed) {
176 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
180 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
181 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
182 $orig_last_read = pg_fetch_result($result, 0, "last_read");
183 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
184 $orig_title = pg_fetch_result($result, 0, "title");
186 if ($orig_title != $entry_title) {
187 $last_read_qpart = 'last_read = null,';
190 if ($orig_content_hash != $content_hash) {
191 $last_read_qpart = 'last_read = null,';
194 // if ($orig_timestamp < $entry_timestamp) {
195 // $last_read_qpart = 'last_read = null,';
198 $entry_content = pg_escape_string($entry_content);
199 $entry_title = pg_escape_string($entry_title);
200 $entry_link = pg_escape_string($entry_link);
202 $query = "UPDATE ttrss_entries
205 title = '$entry_title',
206 link = '$entry_link',
207 updated = '$entry_timestamp_fmt',
208 content = '$entry_content',
209 content_hash = '$content_hash'
211 id = '$orig_entry_id'";
213 $result = pg_query($link, $query);
219 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");