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