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