]>
Commit | Line | Data |
---|---|---|
40d13c28 AD |
1 | <? |
2 | require_once 'config.php'; | |
3 | ||
c3a8d71a AD |
4 | function purge_old_posts() { |
5 | if (PURGE_OLD_DAYS) { | |
6 | $result = pg_query("DELETE FROM ttrss_entries WHERE | |
7 | date_entered < NOW() - INTERVAL '30 days'"); | |
8 | } | |
9 | } | |
10 | ||
9c9c7e6b | 11 | function update_all_feeds($link, $fetch) { |
40d13c28 | 12 | |
b0b4abcf AD |
13 | if (WEB_DEMO_MODE) return; |
14 | ||
b82af8c3 AD |
15 | pg_query("BEGIN"); |
16 | ||
9c9c7e6b AD |
17 | if (!$fetch) { |
18 | ||
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) > " . | |
22 | MIN_UPDATE_TIME); | |
23 | ||
24 | } else { | |
25 | ||
26 | $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds"); | |
27 | } | |
40d13c28 AD |
28 | |
29 | while ($line = pg_fetch_assoc($result)) { | |
466001c4 | 30 | update_rss_feed($link, $line["feed_url"], $line["id"]); |
40d13c28 AD |
31 | } |
32 | ||
c3a8d71a AD |
33 | purge_old_posts(); |
34 | ||
b82af8c3 AD |
35 | pg_query("COMMIT"); |
36 | ||
40d13c28 AD |
37 | } |
38 | ||
78800912 AD |
39 | function check_feed_favicon($feed_url, $feed) { |
40 | $feed_url = str_replace("http://", "", $feed_url); | |
41 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
42 | ||
43 | $icon_url = "http://$feed_url/favicon.ico"; | |
44 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
45 | ||
46 | if (!file_exists($icon_file)) { | |
e695fdc8 | 47 | |
78800912 AD |
48 | error_reporting(0); |
49 | $r = fopen($icon_url, "r"); | |
50 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
51 | ||
52 | if ($r) { | |
53 | $tmpfname = tempnam("/tmp", "ttrssicon"); | |
54 | ||
55 | $t = fopen($tmpfname, "w"); | |
56 | ||
57 | while (!feof($r)) { | |
58 | $buf = fread($r, 16384); | |
59 | fwrite($t, $buf); | |
60 | } | |
61 | ||
62 | fclose($r); | |
63 | fclose($t); | |
64 | ||
e695fdc8 AD |
65 | error_reporting(0); |
66 | if (!rename($tmpfname, $icon_file)) { | |
67 | unlink($tmpfname); | |
68 | } | |
69 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
78800912 AD |
70 | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
40d13c28 AD |
75 | function update_rss_feed($link, $feed_url, $feed) { |
76 | ||
b0b4abcf AD |
77 | if (WEB_DEMO_MODE) return; |
78 | ||
3ad5aa85 | 79 | error_reporting(0); |
40d13c28 | 80 | $rss = fetch_rss($feed_url); |
3ad5aa85 | 81 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
76798ff3 | 82 | |
f48ba3c9 | 83 | pg_query("BEGIN"); |
40d13c28 AD |
84 | |
85 | if ($rss) { | |
b82af8c3 | 86 | |
78800912 AD |
87 | if (ENABLE_FEED_ICONS) { |
88 | check_feed_favicon($feed_url, $feed); | |
89 | } | |
90 | ||
331900c6 AD |
91 | $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'"); |
92 | ||
93 | $registered_title = pg_fetch_result($result, 0, "title"); | |
94 | ||
95 | if (!$registered_title) { | |
331900c6 AD |
96 | $feed_title = $rss->channel["title"]; |
97 | pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'"); | |
98 | } | |
40d13c28 AD |
99 | |
100 | foreach ($rss->items as $item) { | |
101 | ||
102 | $entry_guid = $item["id"]; | |
103 | ||
104 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
105 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
106 | |
107 | if (!$entry_guid) continue; | |
40d13c28 | 108 | |
9c9c7e6b | 109 | $entry_timestamp = ""; |
b82af8c3 | 110 | |
9c9c7e6b AD |
111 | $rss_2_date = $item['pubdate']; |
112 | $rss_1_date = $item['dc']['date']; | |
113 | $atom_date = $item['issued']; | |
b197f117 | 114 | |
9c9c7e6b AD |
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); | |
b82af8c3 AD |
118 | |
119 | if ($entry_timestamp == "") { | |
120 | $entry_timestamp = time(); | |
121 | $no_orig_date = 'true'; | |
466001c4 AD |
122 | } else { |
123 | $no_orig_date = 'false'; | |
b82af8c3 | 124 | } |
b197f117 | 125 | |
466001c4 | 126 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 127 | |
40d13c28 AD |
128 | $entry_title = $item["title"]; |
129 | $entry_link = $item["link"]; | |
71ad3959 AD |
130 | |
131 | if (!$entry_title) continue; | |
132 | if (!$entry_link) continue; | |
133 | ||
40d13c28 | 134 | $entry_content = $item["description"]; |
466001c4 | 135 | if (!$entry_content) $entry_content = $item["content:escaped"]; |
40d13c28 | 136 | if (!$entry_content) $entry_content = $item["content"]; |
a2015351 AD |
137 | |
138 | if (!$entry_content) continue; | |
139 | ||
466001c4 | 140 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 141 | |
40d13c28 AD |
142 | $result = pg_query($link, " |
143 | SELECT | |
466001c4 | 144 | id,last_read,no_orig_date,title,feed_id,content_hash, |
b197f117 | 145 | EXTRACT(EPOCH FROM updated) as updated_timestamp |
40d13c28 AD |
146 | FROM |
147 | ttrss_entries | |
148 | WHERE | |
a2015351 | 149 | guid = '$entry_guid'"); |
466001c4 | 150 | |
40d13c28 | 151 | if (pg_num_rows($result) == 0) { |
466001c4 AD |
152 | |
153 | $entry_content = pg_escape_string($entry_content); | |
154 | $entry_title = pg_escape_string($entry_title); | |
155 | $entry_link = pg_escape_string($entry_link); | |
156 | ||
157 | $query = "INSERT | |
158 | INTO ttrss_entries | |
159 | (title, | |
160 | guid, | |
161 | link, | |
162 | updated, | |
163 | content, | |
164 | content_hash, | |
165 | feed_id, | |
166 | no_orig_date) | |
40d13c28 | 167 | VALUES |
466001c4 AD |
168 | ('$entry_title', |
169 | '$entry_guid', | |
170 | '$entry_link', | |
171 | '$entry_timestamp_fmt', | |
172 | '$entry_content', | |
173 | '$content_hash', | |
174 | '$feed', | |
175 | $no_orig_date)"; | |
176 | ||
76798ff3 AD |
177 | $result = pg_query($link, $query); |
178 | ||
40d13c28 | 179 | } else { |
466001c4 AD |
180 | |
181 | $orig_entry_id = pg_fetch_result($result, 0, "id"); | |
182 | $orig_feed_id = pg_fetch_result($result, 0, "feed_id"); | |
183 | ||
184 | if ($orig_feed_id != $feed) { | |
185 | // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]"; | |
186 | continue; | |
187 | } | |
188 | ||
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"); | |
b82af8c3 | 193 | $orig_title = pg_fetch_result($result, 0, "title"); |
cac95b8d | 194 | |
466001c4 AD |
195 | if ($orig_title != $entry_title) { |
196 | $last_read_qpart = 'last_read = null,'; | |
197 | } | |
36bf7496 AD |
198 | |
199 | if (UPDATE_POST_ON_CHECKSUM_CHANGE && | |
200 | $orig_content_hash != $content_hash) { | |
466001c4 | 201 | |
b82af8c3 | 202 | $last_read_qpart = 'last_read = null,'; |
cac95b8d AD |
203 | } |
204 | ||
f48ba3c9 AD |
205 | // if ($orig_timestamp < $entry_timestamp) { |
206 | // $last_read_qpart = 'last_read = null,'; | |
207 | // } | |
a2015351 | 208 | |
466001c4 AD |
209 | $entry_content = pg_escape_string($entry_content); |
210 | $entry_title = pg_escape_string($entry_title); | |
211 | $entry_link = pg_escape_string($entry_link); | |
212 | ||
40d13c28 AD |
213 | $query = "UPDATE ttrss_entries |
214 | SET | |
466001c4 AD |
215 | $last_read_qpart |
216 | title = '$entry_title', | |
40d13c28 | 217 | link = '$entry_link', |
466001c4 AD |
218 | updated = '$entry_timestamp_fmt', |
219 | content = '$entry_content', | |
220 | content_hash = '$content_hash' | |
40d13c28 | 221 | WHERE |
466001c4 | 222 | id = '$orig_entry_id'"; |
a2015351 | 223 | |
40d13c28 | 224 | $result = pg_query($link, $query); |
a2015351 | 225 | |
466001c4 | 226 | } |
40d13c28 AD |
227 | } |
228 | ||
76798ff3 AD |
229 | if ($result) { |
230 | $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()"); | |
231 | } | |
40d13c28 AD |
232 | |
233 | } | |
234 | ||
f48ba3c9 AD |
235 | pg_query("COMMIT"); |
236 | ||
40d13c28 AD |
237 | } |
238 | ||
239 | ||
40d13c28 | 240 | ?> |