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);
56 rename($tmpfname, $icon_file);
62 function update_rss_feed($link, $feed_url, $feed) {
64 if (WEB_DEMO_MODE
) return;
67 $rss = fetch_rss($feed_url);
68 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
74 if (ENABLE_FEED_ICONS
) {
75 check_feed_favicon($feed_url, $feed);
78 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
80 $registered_title = pg_fetch_result($result, 0, "title");
82 if (!$registered_title) {
83 $feed_title = $rss->channel
["title"];
84 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
87 foreach ($rss->items
as $item) {
89 $entry_guid = $item["id"];
91 if (!$entry_guid) $entry_guid = $item["guid"];
92 if (!$entry_guid) $entry_guid = $item["link"];
94 if (!$entry_guid) continue;
96 $entry_timestamp = "";
98 $rss_2_date = $item['pubdate'];
99 $rss_1_date = $item['dc']['date'];
100 $atom_date = $item['issued'];
102 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
103 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
104 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
106 if ($entry_timestamp == "") {
107 $entry_timestamp = time();
108 $no_orig_date = 'true';
110 $no_orig_date = 'false';
113 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
115 $entry_title = $item["title"];
116 $entry_link = $item["link"];
118 if (!$entry_title) continue;
119 if (!$entry_link) continue;
121 $entry_content = $item["description"];
122 if (!$entry_content) $entry_content = $item["content:escaped"];
123 if (!$entry_content) $entry_content = $item["content"];
125 if (!$entry_content) continue;
127 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
129 $result = pg_query($link, "
131 id,last_read,no_orig_date,title,feed_id,content_hash,
132 EXTRACT(EPOCH FROM updated) as updated_timestamp
136 guid = '$entry_guid'");
138 if (pg_num_rows($result) == 0) {
140 $entry_content = pg_escape_string($entry_content);
141 $entry_title = pg_escape_string($entry_title);
142 $entry_link = pg_escape_string($entry_link);
158 '$entry_timestamp_fmt',
164 $result = pg_query($link, $query);
168 $orig_entry_id = pg_fetch_result($result, 0, "id");
169 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
171 if ($orig_feed_id != $feed) {
172 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
176 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
177 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
178 $orig_last_read = pg_fetch_result($result, 0, "last_read");
179 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
180 $orig_title = pg_fetch_result($result, 0, "title");
182 if ($orig_title != $entry_title) {
183 $last_read_qpart = 'last_read = null,';
186 if ($orig_content_hash != $content_hash) {
187 $last_read_qpart = 'last_read = null,';
190 // if ($orig_timestamp < $entry_timestamp) {
191 // $last_read_qpart = 'last_read = null,';
194 $entry_content = pg_escape_string($entry_content);
195 $entry_title = pg_escape_string($entry_title);
196 $entry_link = pg_escape_string($entry_link);
198 $query = "UPDATE ttrss_entries
201 title = '$entry_title',
202 link = '$entry_link',
203 updated = '$entry_timestamp_fmt',
204 content = '$entry_content',
205 content_hash = '$content_hash'
207 id = '$orig_entry_id'";
209 $result = pg_query($link, $query);
215 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");