]>
Commit | Line | Data |
---|---|---|
1 | <? | |
2 | require_once 'config.php'; | |
3 | ||
4 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); | |
5 | ||
6 | function purge_old_posts($link) { | |
7 | if (PURGE_OLD_DAYS > 0) { | |
8 | ||
9 | if (DB_TYPE == "pgsql") { | |
10 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
11 | marked = false AND | |
12 | date_entered < NOW() - INTERVAL '".PURGE_OLD_DAYS." days'"); | |
13 | } else { | |
14 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
15 | marked = false AND | |
16 | date_entered < DATE_SUB(NOW(), INTERVAL ".PURGE_OLD_DAYS." DAY)"); | |
17 | } | |
18 | } | |
19 | } | |
20 | ||
21 | function update_all_feeds($link, $fetch) { | |
22 | ||
23 | if (WEB_DEMO_MODE) return; | |
24 | ||
25 | if (DAEMON_REFRESH_ONLY) { | |
26 | if (!$_GET["daemon"]) { | |
27 | return; | |
28 | } | |
29 | } | |
30 | ||
31 | db_query($link, "BEGIN"); | |
32 | ||
33 | $result = db_query($link, "SELECT feed_url,id, | |
34 | substring(last_updated,1,19) as last_updated, | |
35 | update_interval FROM ttrss_feeds"); | |
36 | ||
37 | while ($line = db_fetch_assoc($result)) { | |
38 | $upd_intl = $line["update_interval"]; | |
39 | ||
40 | if (!$upd_intl || $upd_intl == 0) $upd_intl = DEFAULT_UPDATE_INTERVAL; | |
41 | ||
42 | if (!$line["last_updated"] || | |
43 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) { | |
44 | ||
45 | update_rss_feed($link, $line["feed_url"], $line["id"]); | |
46 | } | |
47 | } | |
48 | ||
49 | purge_old_posts($link); | |
50 | ||
51 | db_query($link, "COMMIT"); | |
52 | ||
53 | } | |
54 | ||
55 | function check_feed_favicon($feed_url, $feed) { | |
56 | $feed_url = str_replace("http://", "", $feed_url); | |
57 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
58 | ||
59 | $icon_url = "http://$feed_url/favicon.ico"; | |
60 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
61 | ||
62 | if (!file_exists($icon_file)) { | |
63 | ||
64 | error_reporting(0); | |
65 | $r = fopen($icon_url, "r"); | |
66 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
67 | ||
68 | if ($r) { | |
69 | $tmpfname = tempnam("/tmp", "ttrssicon"); | |
70 | ||
71 | $t = fopen($tmpfname, "w"); | |
72 | ||
73 | while (!feof($r)) { | |
74 | $buf = fread($r, 16384); | |
75 | fwrite($t, $buf); | |
76 | } | |
77 | ||
78 | fclose($r); | |
79 | fclose($t); | |
80 | ||
81 | error_reporting(0); | |
82 | if (!rename($tmpfname, $icon_file)) { | |
83 | unlink($tmpfname); | |
84 | } | |
85 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
86 | ||
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | function update_rss_feed($link, $feed_url, $feed) { | |
92 | ||
93 | if (WEB_DEMO_MODE) return; | |
94 | ||
95 | $feed = db_escape_string($feed); | |
96 | ||
97 | error_reporting(0); | |
98 | $rss = fetch_rss($feed_url); | |
99 | ||
100 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
101 | ||
102 | db_query($link, "BEGIN"); | |
103 | ||
104 | $feed = db_escape_string($feed); | |
105 | ||
106 | if ($rss) { | |
107 | ||
108 | if (ENABLE_FEED_ICONS) { | |
109 | check_feed_favicon($feed_url, $feed); | |
110 | } | |
111 | ||
112 | $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'"); | |
113 | ||
114 | $registered_title = db_fetch_result($result, 0, "title"); | |
115 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
116 | ||
117 | if (!$registered_title) { | |
118 | $feed_title = db_escape_string($rss->channel["title"]); | |
119 | db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'"); | |
120 | } | |
121 | ||
122 | // print "I: " . $rss->channel["image"]["url"]; | |
123 | ||
124 | $icon_url = $rss->image["url"]; | |
125 | ||
126 | if ($icon_url && !$orig_icon_url) { | |
127 | $icon_url = db_escape_string($icon_url); | |
128 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
129 | } | |
130 | ||
131 | ||
132 | $filters = array(); | |
133 | ||
134 | $result = db_query($link, "SELECT reg_exp, | |
135 | (SELECT name FROM ttrss_filter_types | |
136 | WHERE id = filter_type) as name | |
137 | FROM ttrss_filters"); | |
138 | ||
139 | while ($line = db_fetch_assoc($result)) { | |
140 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); | |
141 | array_push($filters[$line["name"]], $line["reg_exp"]); | |
142 | } | |
143 | ||
144 | foreach ($rss->items as $item) { | |
145 | ||
146 | $entry_guid = $item["id"]; | |
147 | ||
148 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
149 | if (!$entry_guid) $entry_guid = $item["link"]; | |
150 | ||
151 | if (!$entry_guid) continue; | |
152 | ||
153 | $entry_timestamp = ""; | |
154 | ||
155 | $rss_2_date = $item['pubdate']; | |
156 | $rss_1_date = $item['dc']['date']; | |
157 | $atom_date = $item['issued']; | |
158 | ||
159 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); | |
160 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
161 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
162 | ||
163 | if ($entry_timestamp == "") { | |
164 | $entry_timestamp = time(); | |
165 | $no_orig_date = 'true'; | |
166 | } else { | |
167 | $no_orig_date = 'false'; | |
168 | } | |
169 | ||
170 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
171 | ||
172 | $entry_title = $item["title"]; | |
173 | $entry_link = $item["link"]; | |
174 | ||
175 | if (!$entry_title) continue; | |
176 | if (!$entry_link) continue; | |
177 | ||
178 | $entry_content = $item["content:escaped"]; | |
179 | ||
180 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
181 | if (!$entry_content) $entry_content = $item["content"]; | |
182 | if (!$entry_content) $entry_content = $item["description"]; | |
183 | ||
184 | // if (!$entry_content) continue; | |
185 | ||
186 | // WTF | |
187 | if (is_array($entry_content)) { | |
188 | $entry_content = $entry_content["encoded"]; | |
189 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
190 | } | |
191 | ||
192 | // print_r($item); | |
193 | // print_r($entry_content); | |
194 | ||
195 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
196 | ||
197 | $entry_comments = $item["comments"]; | |
198 | ||
199 | $entry_guid = db_escape_string($entry_guid); | |
200 | ||
201 | $result = db_query($link, " | |
202 | SELECT | |
203 | id,last_read,no_orig_date,title,feed_id,content_hash, | |
204 | substring(updated,1,19) as updated | |
205 | FROM | |
206 | ttrss_entries | |
207 | WHERE | |
208 | guid = '$entry_guid'"); | |
209 | ||
210 | // print db_num_rows($result) . "$entry_guid<br/>"; | |
211 | ||
212 | if (db_num_rows($result) == 0) { | |
213 | ||
214 | error_reporting(0); | |
215 | if (is_filtered($entry_title, $entry_content, $filters)) { | |
216 | continue; | |
217 | } | |
218 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
219 | ||
220 | //$entry_guid = db_escape_string($entry_guid); | |
221 | $entry_content = db_escape_string($entry_content); | |
222 | $entry_title = db_escape_string($entry_title); | |
223 | $entry_link = db_escape_string($entry_link); | |
224 | $entry_comments = db_escape_string($entry_comments); | |
225 | ||
226 | $query = "INSERT | |
227 | INTO ttrss_entries | |
228 | (title, | |
229 | guid, | |
230 | link, | |
231 | updated, | |
232 | content, | |
233 | content_hash, | |
234 | feed_id, | |
235 | comments, | |
236 | no_orig_date, | |
237 | date_entered) | |
238 | VALUES | |
239 | ('$entry_title', | |
240 | '$entry_guid', | |
241 | '$entry_link', | |
242 | '$entry_timestamp_fmt', | |
243 | '$entry_content', | |
244 | '$content_hash', | |
245 | '$feed', | |
246 | '$entry_comments', | |
247 | $no_orig_date, | |
248 | NOW())"; | |
249 | ||
250 | $result = db_query($link, $query); | |
251 | ||
252 | } else { | |
253 | ||
254 | $orig_entry_id = db_fetch_result($result, 0, "id"); | |
255 | $orig_feed_id = db_fetch_result($result, 0, "feed_id"); | |
256 | ||
257 | // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>"; | |
258 | ||
259 | if ($orig_feed_id != $feed) { | |
260 | // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]"; | |
261 | continue; | |
262 | } | |
263 | ||
264 | $entry_is_modified = false; | |
265 | ||
266 | $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated")); | |
267 | ||
268 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
269 | $orig_last_read = db_fetch_result($result, 0, "last_read"); | |
270 | $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date"); | |
271 | $orig_title = db_fetch_result($result, 0, "title"); | |
272 | ||
273 | $last_read_qpart = ""; | |
274 | ||
275 | if ($orig_content_hash != $content_hash) { | |
276 | // print "$orig_content_hash :: $content_hash<br>"; | |
277 | ||
278 | if (UPDATE_POST_ON_CHECKSUM_CHANGE) { | |
279 | $last_read_qpart = 'last_read = null,'; | |
280 | } | |
281 | $entry_is_modified = true; | |
282 | } | |
283 | ||
284 | if ($orig_title != $entry_title) { | |
285 | $entry_is_modified = true; | |
286 | } | |
287 | ||
288 | if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
289 | $entry_is_modified = true; | |
290 | } | |
291 | ||
292 | if ($entry_is_modified) { | |
293 | ||
294 | // print "$entry_guid Modified!<br>"; | |
295 | ||
296 | $entry_comments = db_escape_string($entry_comments); | |
297 | $entry_content = db_escape_string($entry_content); | |
298 | $entry_title = db_escape_string($entry_title); | |
299 | $entry_link = db_escape_string($entry_link); | |
300 | ||
301 | $query = "UPDATE ttrss_entries | |
302 | SET | |
303 | $last_read_qpart | |
304 | title = '$entry_title', | |
305 | link = '$entry_link', | |
306 | updated = '$entry_timestamp_fmt', | |
307 | content = '$entry_content', | |
308 | comments = '$entry_comments', | |
309 | content_hash = '$content_hash' | |
310 | WHERE | |
311 | id = '$orig_entry_id'"; | |
312 | ||
313 | $result = db_query($link, $query); | |
314 | } | |
315 | } | |
316 | ||
317 | /* taaaags */ | |
318 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
319 | ||
320 | $entry_tags = null; | |
321 | ||
322 | preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content, | |
323 | $entry_tags); | |
324 | ||
325 | $entry_tags = $entry_tags[1]; | |
326 | ||
327 | if (count($entry_tags) > 0) { | |
328 | ||
329 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
330 | WHERE guid = '$entry_guid'"); | |
331 | ||
332 | if (!$result || db_num_rows($result) != 1) { | |
333 | return; | |
334 | } | |
335 | ||
336 | $entry_id = db_fetch_result($result, 0, "id"); | |
337 | ||
338 | foreach ($entry_tags as $tag) { | |
339 | $tag = db_escape_string(strtolower($tag)); | |
340 | ||
341 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
342 | WHERE tag_name = '$tag' AND post_id = '$entry_id' LIMIT 1"); | |
343 | ||
344 | if ($result && db_num_rows($result) == 0) { | |
345 | ||
346 | // print "tagging $entry_id as $tag<br>"; | |
347 | ||
348 | db_query($link, "INSERT INTO ttrss_tags (tag_name,post_id) | |
349 | VALUES ('$tag', '$entry_id')"); | |
350 | } | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | db_query($link, "UPDATE ttrss_feeds | |
356 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
357 | ||
358 | } else { | |
359 | $error_msg = db_escape_string(magpie_error()); | |
360 | db_query($link, | |
361 | "UPDATE ttrss_feeds SET last_error = '$error_msg', | |
362 | last_updated = NOW() WHERE id = '$feed'"); | |
363 | } | |
364 | ||
365 | db_query($link, "COMMIT"); | |
366 | ||
367 | } | |
368 | ||
369 | function print_select($id, $default, $values, $attributes = "") { | |
370 | print "<select id=\"$id\" $attributes>"; | |
371 | foreach ($values as $v) { | |
372 | if ($v == $default) | |
373 | $sel = " selected"; | |
374 | else | |
375 | $sel = ""; | |
376 | ||
377 | print "<option$sel>$v</option>"; | |
378 | } | |
379 | print "</select>"; | |
380 | } | |
381 | ||
382 | function is_filtered($title, $content, $filters) { | |
383 | ||
384 | if ($filters["title"]) { | |
385 | foreach ($filters["title"] as $title_filter) { | |
386 | if (preg_match("/$title_filter/i", $title)) | |
387 | return true; | |
388 | } | |
389 | } | |
390 | ||
391 | if ($filters["content"]) { | |
392 | foreach ($filters["content"] as $content_filter) { | |
393 | if (preg_match("/$content_filter/i", $content)) | |
394 | return true; | |
395 | } | |
396 | } | |
397 | ||
398 | if ($filters["both"]) { | |
399 | foreach ($filters["both"] as $filter) { | |
400 | if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content)) | |
401 | return true; | |
402 | } | |
403 | } | |
404 | ||
405 | return false; | |
406 | } | |
407 | ||
408 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file) { | |
409 | ||
410 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
411 | $feed_icon = "<img src=\"$icon_file\">"; | |
412 | } else { | |
413 | $feed_icon = "<img src=\"images/blank_icon.gif\">"; | |
414 | } | |
415 | ||
416 | $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; | |
417 | ||
418 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
419 | if (ENABLE_FEED_ICONS) { | |
420 | print "$feed_icon"; | |
421 | } | |
422 | ||
423 | print "<span id=\"FEEDN-$feed_id\">$feed</span>"; | |
424 | ||
425 | if ($unread != 0) { | |
426 | $fctr_class = ""; | |
427 | } else { | |
428 | $fctr_class = "class=\"invisible\""; | |
429 | } | |
430 | ||
431 | print "<span $fctr_class id=\"FEEDCTR-$feed_id\"> | |
432 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
433 | ||
434 | print "</li>"; | |
435 | ||
436 | } | |
437 | ||
438 | function getmicrotime() { | |
439 | list($usec, $sec) = explode(" ",microtime()); | |
440 | return ((float)$usec + (float)$sec); | |
441 | } | |
442 | ||
443 | ?> |