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