]>
Commit | Line | Data |
---|---|---|
40d13c28 AD |
1 | <? |
2 | require_once 'config.php'; | |
3 | ||
a3ee2a38 AD |
4 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
5 | ||
b6eefba5 | 6 | function purge_old_posts($link) { |
f92db4f5 | 7 | if (PURGE_OLD_DAYS > 0) { |
b6eefba5 | 8 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE |
f4c10d44 | 9 | marked = false AND |
f92db4f5 | 10 | date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'"); |
c3a8d71a AD |
11 | } |
12 | } | |
13 | ||
9c9c7e6b | 14 | function update_all_feeds($link, $fetch) { |
40d13c28 | 15 | |
b0b4abcf AD |
16 | if (WEB_DEMO_MODE) return; |
17 | ||
b6eefba5 | 18 | db_query($link, "BEGIN"); |
b82af8c3 | 19 | |
9c9c7e6b AD |
20 | if (!$fetch) { |
21 | ||
b6eefba5 | 22 | $result = db_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE |
9c9c7e6b AD |
23 | last_updated is null OR title = '' OR |
24 | EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " . | |
25 | MIN_UPDATE_TIME); | |
26 | ||
27 | } else { | |
28 | ||
b6eefba5 | 29 | $result = db_query($link, "SELECT feed_url,id FROM ttrss_feeds"); |
9c9c7e6b | 30 | } |
40d13c28 | 31 | |
b6eefba5 | 32 | while ($line = db_fetch_assoc($result)) { |
466001c4 | 33 | update_rss_feed($link, $line["feed_url"], $line["id"]); |
40d13c28 AD |
34 | } |
35 | ||
b6eefba5 | 36 | purge_old_posts($link); |
c3a8d71a | 37 | |
b6eefba5 | 38 | db_query($link, "COMMIT"); |
b82af8c3 | 39 | |
40d13c28 AD |
40 | } |
41 | ||
78800912 AD |
42 | function check_feed_favicon($feed_url, $feed) { |
43 | $feed_url = str_replace("http://", "", $feed_url); | |
44 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
45 | ||
46 | $icon_url = "http://$feed_url/favicon.ico"; | |
47 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
48 | ||
49 | if (!file_exists($icon_file)) { | |
e695fdc8 | 50 | |
78800912 AD |
51 | error_reporting(0); |
52 | $r = fopen($icon_url, "r"); | |
53 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
54 | ||
55 | if ($r) { | |
56 | $tmpfname = tempnam("/tmp", "ttrssicon"); | |
57 | ||
58 | $t = fopen($tmpfname, "w"); | |
59 | ||
60 | while (!feof($r)) { | |
61 | $buf = fread($r, 16384); | |
62 | fwrite($t, $buf); | |
63 | } | |
64 | ||
65 | fclose($r); | |
66 | fclose($t); | |
67 | ||
e695fdc8 AD |
68 | error_reporting(0); |
69 | if (!rename($tmpfname, $icon_file)) { | |
70 | unlink($tmpfname); | |
71 | } | |
72 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
78800912 AD |
73 | |
74 | } | |
75 | } | |
76 | } | |
77 | ||
40d13c28 AD |
78 | function update_rss_feed($link, $feed_url, $feed) { |
79 | ||
b0b4abcf AD |
80 | if (WEB_DEMO_MODE) return; |
81 | ||
3ad5aa85 | 82 | error_reporting(0); |
40d13c28 | 83 | $rss = fetch_rss($feed_url); |
3ad5aa85 | 84 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
76798ff3 | 85 | |
b6eefba5 | 86 | db_query($link, "BEGIN"); |
b7f4bda2 | 87 | |
b6eefba5 | 88 | $feed = db_escape_string($feed); |
40d13c28 AD |
89 | |
90 | if ($rss) { | |
b82af8c3 | 91 | |
78800912 AD |
92 | if (ENABLE_FEED_ICONS) { |
93 | check_feed_favicon($feed_url, $feed); | |
94 | } | |
95 | ||
b6eefba5 | 96 | $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 97 | |
b6eefba5 AD |
98 | $registered_title = db_fetch_result($result, 0, "title"); |
99 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
331900c6 AD |
100 | |
101 | if (!$registered_title) { | |
331900c6 | 102 | $feed_title = $rss->channel["title"]; |
b6eefba5 | 103 | db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'"); |
331900c6 | 104 | } |
40d13c28 | 105 | |
b7f4bda2 AD |
106 | // print "I: " . $rss->channel["image"]["url"]; |
107 | ||
108 | $icon_url = $rss->image["url"]; | |
109 | ||
110 | if ($icon_url && !$orig_icon_url) { | |
b6eefba5 AD |
111 | $icon_url = db_escape_string($icon_url); |
112 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
113 | } |
114 | ||
e6155a06 AD |
115 | |
116 | $filters = array(); | |
117 | ||
b6eefba5 | 118 | $result = db_query($link, "SELECT regexp, |
e6155a06 AD |
119 | (SELECT name FROM ttrss_filter_types |
120 | WHERE id = filter_type) as name | |
121 | FROM ttrss_filters"); | |
122 | ||
b6eefba5 | 123 | while ($line = db_fetch_assoc($result)) { |
e6155a06 AD |
124 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
125 | array_push($filters[$line["name"]], $line["regexp"]); | |
126 | } | |
127 | ||
40d13c28 AD |
128 | foreach ($rss->items as $item) { |
129 | ||
130 | $entry_guid = $item["id"]; | |
131 | ||
132 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
133 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
134 | |
135 | if (!$entry_guid) continue; | |
a116f569 | 136 | |
9c9c7e6b | 137 | $entry_timestamp = ""; |
b82af8c3 | 138 | |
9c9c7e6b AD |
139 | $rss_2_date = $item['pubdate']; |
140 | $rss_1_date = $item['dc']['date']; | |
141 | $atom_date = $item['issued']; | |
b197f117 | 142 | |
9c9c7e6b AD |
143 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
144 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
145 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
146 | |
147 | if ($entry_timestamp == "") { | |
148 | $entry_timestamp = time(); | |
149 | $no_orig_date = 'true'; | |
466001c4 AD |
150 | } else { |
151 | $no_orig_date = 'false'; | |
b82af8c3 | 152 | } |
b197f117 | 153 | |
466001c4 | 154 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 155 | |
40d13c28 AD |
156 | $entry_title = $item["title"]; |
157 | $entry_link = $item["link"]; | |
71ad3959 AD |
158 | |
159 | if (!$entry_title) continue; | |
160 | if (!$entry_link) continue; | |
161 | ||
40d13c28 | 162 | $entry_content = $item["description"]; |
466001c4 | 163 | if (!$entry_content) $entry_content = $item["content:escaped"]; |
40d13c28 | 164 | if (!$entry_content) $entry_content = $item["content"]; |
a2015351 | 165 | |
a116f569 | 166 | // if (!$entry_content) continue; |
a2015351 | 167 | |
466001c4 | 168 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 169 | |
a1ea1e12 AD |
170 | $entry_comments = $item["comments"]; |
171 | ||
b6eefba5 | 172 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 173 | |
b6eefba5 | 174 | $result = db_query($link, " |
40d13c28 | 175 | SELECT |
466001c4 | 176 | id,last_read,no_orig_date,title,feed_id,content_hash, |
b197f117 | 177 | EXTRACT(EPOCH FROM updated) as updated_timestamp |
40d13c28 AD |
178 | FROM |
179 | ttrss_entries | |
180 | WHERE | |
a2015351 | 181 | guid = '$entry_guid'"); |
466001c4 | 182 | |
b6eefba5 | 183 | if (db_num_rows($result) == 0) { |
466001c4 | 184 | |
e6155a06 AD |
185 | error_reporting(0); |
186 | if (is_filtered($entry_title, $entry_content, $filters)) { | |
187 | continue; | |
188 | } | |
189 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
190 | ||
b6eefba5 AD |
191 | //$entry_guid = db_escape_string($entry_guid); |
192 | $entry_content = db_escape_string($entry_content); | |
193 | $entry_title = db_escape_string($entry_title); | |
194 | $entry_link = db_escape_string($entry_link); | |
195 | $entry_comments = db_escape_string($entry_comments); | |
466001c4 AD |
196 | |
197 | $query = "INSERT | |
198 | INTO ttrss_entries | |
199 | (title, | |
200 | guid, | |
201 | link, | |
202 | updated, | |
203 | content, | |
204 | content_hash, | |
205 | feed_id, | |
a1ea1e12 | 206 | comments, |
466001c4 | 207 | no_orig_date) |
40d13c28 | 208 | VALUES |
466001c4 AD |
209 | ('$entry_title', |
210 | '$entry_guid', | |
211 | '$entry_link', | |
212 | '$entry_timestamp_fmt', | |
213 | '$entry_content', | |
214 | '$content_hash', | |
215 | '$feed', | |
a1ea1e12 | 216 | '$entry_comments', |
466001c4 AD |
217 | $no_orig_date)"; |
218 | ||
b6eefba5 | 219 | $result = db_query($link, $query); |
76798ff3 | 220 | |
40d13c28 | 221 | } else { |
466001c4 | 222 | |
b6eefba5 AD |
223 | $orig_entry_id = db_fetch_result($result, 0, "id"); |
224 | $orig_feed_id = db_fetch_result($result, 0, "feed_id"); | |
466001c4 AD |
225 | |
226 | if ($orig_feed_id != $feed) { | |
227 | // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]"; | |
228 | continue; | |
229 | } | |
ad3024fc AD |
230 | |
231 | $entry_is_modified = false; | |
466001c4 | 232 | |
b6eefba5 AD |
233 | $orig_timestamp = db_fetch_result($result, 0, "updated_timestamp"); |
234 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
235 | $orig_last_read = db_fetch_result($result, 0, "last_read"); | |
236 | $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date"); | |
237 | $orig_title = db_fetch_result($result, 0, "title"); | |
cac95b8d | 238 | |
2d84262b AD |
239 | $last_read_qpart = ""; |
240 | ||
ad3024fc AD |
241 | if ($orig_content_hash != $content_hash) { |
242 | if (UPDATE_POST_ON_CHECKSUM_CHANGE) { | |
243 | $last_read_qpart = 'last_read = null,'; | |
244 | } | |
245 | $entry_is_modified = true; | |
246 | } | |
247 | ||
248 | if ($orig_title != $entry_title) { | |
249 | $entry_is_modified = true; | |
250 | } | |
251 | ||
252 | if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
253 | $entry_is_modified = true; | |
cac95b8d AD |
254 | } |
255 | ||
ad3024fc | 256 | if ($entry_is_modified) { |
a2015351 | 257 | |
b6eefba5 AD |
258 | $entry_comments = db_escape_string($entry_comments); |
259 | $entry_content = db_escape_string($entry_content); | |
260 | $entry_title = db_escape_string($entry_title); | |
261 | $entry_link = db_escape_string($entry_link); | |
a2015351 | 262 | |
ad3024fc AD |
263 | $query = "UPDATE ttrss_entries |
264 | SET | |
265 | $last_read_qpart | |
266 | title = '$entry_title', | |
267 | link = '$entry_link', | |
268 | updated = '$entry_timestamp_fmt', | |
269 | content = '$entry_content', | |
270 | comments = '$entry_comments', | |
271 | content_hash = '$content_hash' | |
272 | WHERE | |
273 | id = '$orig_entry_id'"; | |
274 | ||
b6eefba5 | 275 | $result = db_query($link, $query); |
ad3024fc | 276 | } |
466001c4 | 277 | } |
40d13c28 AD |
278 | } |
279 | ||
76798ff3 | 280 | if ($result) { |
b6eefba5 | 281 | $result = db_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()"); |
76798ff3 | 282 | } |
40d13c28 AD |
283 | |
284 | } | |
285 | ||
b6eefba5 | 286 | db_query($link, "COMMIT"); |
f48ba3c9 | 287 | |
40d13c28 AD |
288 | } |
289 | ||
f175937c AD |
290 | function print_select($id, $default, $values, $attributes = "") { |
291 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
292 | foreach ($values as $v) { |
293 | if ($v == $default) | |
294 | $sel = " selected"; | |
295 | else | |
296 | $sel = ""; | |
297 | ||
298 | print "<option$sel>$v</option>"; | |
299 | } | |
300 | print "</select>"; | |
301 | } | |
40d13c28 | 302 | |
e6155a06 AD |
303 | function is_filtered($title, $content, $filters) { |
304 | ||
305 | if ($filters["title"]) { | |
306 | foreach ($filters["title"] as $title_filter) { | |
307 | if (preg_match("/$title_filter/i", $title)) | |
308 | return true; | |
309 | } | |
310 | } | |
311 | ||
312 | if ($filters["content"]) { | |
313 | foreach ($filters["content"] as $content_filter) { | |
314 | if (preg_match("/$content_filter/i", $content)) | |
315 | return true; | |
316 | } | |
317 | } | |
318 | ||
319 | if ($filters["both"]) { | |
320 | foreach ($filters["both"] as $filter) { | |
321 | if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content)) | |
322 | return true; | |
323 | } | |
324 | } | |
325 | ||
326 | return false; | |
327 | } | |
328 | ||
40d13c28 | 329 | ?> |