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