]> git.wh0rd.org - tt-rss.git/blob - functions.php
import rss_utils.inc for parse_w3cdtf()
[tt-rss.git] / functions.php
1 <?
2 session_start();
3
4 if ($_GET["debug"]) {
5 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 } else {
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8 }
9
10 require_once 'config.php';
11 require_once 'db-prefs.php';
12
13 require_once 'magpierss/rss_utils.inc';
14
15 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
16
17 function purge_feed($link, $feed_id, $purge_interval) {
18
19 if (DB_TYPE == "pgsql") {
20 db_query($link, "DELETE FROM ttrss_user_entries WHERE
21 marked = false AND feed_id = '$feed_id' AND
22 (SELECT date_entered FROM ttrss_entries WHERE
23 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
24 } else {
25 db_query($link, "DELETE FROM ttrss_user_entries WHERE
26 marked = false AND feed_id = '$feed_id' AND
27 (SELECT date_entered FROM ttrss_entries WHERE
28 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
29 }
30 }
31
32 function global_purge_old_posts($link, $do_output = false) {
33
34 $result = db_query($link,
35 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
36
37 while ($line = db_fetch_assoc($result)) {
38
39 $feed_id = $line["id"];
40 $purge_interval = $line["purge_interval"];
41 $owner_uid = $line["owner_uid"];
42
43 if ($purge_interval == 0) {
44
45 $tmp_result = db_query($link,
46 "SELECT value FROM ttrss_user_prefs WHERE
47 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
48
49 if (db_num_rows($tmp_result) != 0) {
50 $purge_interval = db_fetch_result($tmp_result, 0, "value");
51 }
52 }
53
54 if ($do_output) {
55 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
56 }
57
58 if ($purge_interval > 0) {
59 purge_feed($link, $feed_id, $purge_interval);
60 }
61 }
62
63 // purge orphaned posts in main content table
64 db_query($link, "DELETE FROM ttrss_entries WHERE
65 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
66
67 }
68
69 function purge_old_posts($link) {
70
71 $user_id = $_SESSION["uid"];
72
73 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
74 WHERE owner_uid = '$user_id'");
75
76 while ($line = db_fetch_assoc($result)) {
77
78 $feed_id = $line["id"];
79 $purge_interval = $line["purge_interval"];
80
81 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
82
83 if ($purge_interval > 0) {
84 purge_feed($link, $feed_id, $purge_interval);
85 }
86 }
87
88 // purge orphaned posts in main content table
89 db_query($link, "DELETE FROM ttrss_entries WHERE
90 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
91 }
92
93 function update_all_feeds($link, $fetch, $user_id = false) {
94
95 if (WEB_DEMO_MODE) return;
96
97 if (!$user_id) {
98 $user_id = $_SESSION["uid"];
99 purge_old_posts($link);
100 }
101
102 // db_query($link, "BEGIN");
103
104 $result = db_query($link, "SELECT feed_url,id,
105 substring(last_updated,1,19) as last_updated,
106 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
107
108 while ($line = db_fetch_assoc($result)) {
109 $upd_intl = $line["update_interval"];
110
111 if (!$upd_intl || $upd_intl == 0) {
112 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
113 }
114
115 if ($fetch || (!$line["last_updated"] ||
116 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
117
118 update_rss_feed($link, $line["feed_url"], $line["id"]);
119 }
120 }
121
122 // db_query($link, "COMMIT");
123
124 }
125
126 function check_feed_favicon($feed_url, $feed, $link) {
127 $feed_url = str_replace("http://", "", $feed_url);
128 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
129
130 $icon_url = "http://$feed_url/favicon.ico";
131 $icon_file = ICONS_DIR . "/$feed.ico";
132
133 if (!file_exists($icon_file)) {
134
135 error_reporting(0);
136 $r = fopen($icon_url, "r");
137 error_reporting (DEFAULT_ERROR_LEVEL);
138
139 if ($r) {
140 $tmpfname = tempnam("/tmp", "ttrssicon");
141
142 $t = fopen($tmpfname, "w");
143
144 while (!feof($r)) {
145 $buf = fread($r, 16384);
146 fwrite($t, $buf);
147 }
148
149 fclose($r);
150 fclose($t);
151
152 error_reporting(0);
153 if (!rename($tmpfname, $icon_file)) {
154 unlink($tmpfname);
155 }
156
157 chmod($icon_file, 0644);
158
159 error_reporting (DEFAULT_ERROR_LEVEL);
160
161 }
162 }
163 }
164
165 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
166
167 if (WEB_DEMO_MODE) return;
168
169 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
170 return;
171 }
172
173 $feed = db_escape_string($feed);
174
175 error_reporting(0);
176 $rss = fetch_rss($feed_url);
177
178 error_reporting (DEFAULT_ERROR_LEVEL);
179
180 $feed = db_escape_string($feed);
181
182 if ($rss) {
183
184 db_query($link, "BEGIN");
185
186 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
187 FROM ttrss_feeds WHERE id = '$feed'");
188
189 $registered_title = db_fetch_result($result, 0, "title");
190 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
191 $orig_site_url = db_fetch_result($result, 0, "site_url");
192
193 $owner_uid = db_fetch_result($result, 0, "owner_uid");
194
195 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
196 check_feed_favicon($feed_url, $feed, $link);
197 }
198
199 if (!$registered_title) {
200 $feed_title = db_escape_string($rss->channel["title"]);
201 db_query($link, "UPDATE ttrss_feeds SET
202 title = '$feed_title' WHERE id = '$feed'");
203 }
204
205 $site_url = $rss->channel["link"];
206
207 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
208 $site_url = db_escape_string($rss->channel["link"]);
209 db_query($link, "UPDATE ttrss_feeds SET
210 site_url = '$site_url' WHERE id = '$feed'");
211 }
212
213 // print "I: " . $rss->channel["image"]["url"];
214
215 $icon_url = $rss->image["url"];
216
217 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
218 $icon_url = db_escape_string($icon_url);
219 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
220 }
221
222
223 $filters = array();
224
225 $result = db_query($link, "SELECT reg_exp,
226 (SELECT name FROM ttrss_filter_types
227 WHERE id = filter_type) as name
228 FROM ttrss_filters WHERE
229 owner_uid = $owner_uid AND
230 (feed_id IS NULL OR feed_id = '$feed')");
231
232 while ($line = db_fetch_assoc($result)) {
233 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
234 array_push($filters[$line["name"]], $line["reg_exp"]);
235 }
236
237 $iterator = $rss->items;
238
239 if (!$iterator) $iterator = $rss->entries;
240 if (!$iterator) $iterator = $rss;
241
242 foreach ($iterator as $item) {
243
244 $entry_guid = $item["id"];
245
246 if (!$entry_guid) $entry_guid = $item["guid"];
247 if (!$entry_guid) $entry_guid = $item["link"];
248
249 if (!$entry_guid) continue;
250
251 $entry_timestamp = "";
252
253 $rss_2_date = $item['pubdate'];
254 $rss_1_date = $item['dc']['date'];
255 $atom_date = $item['issued'];
256
257 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
258 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
259 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
260
261 if ($entry_timestamp == "") {
262 $entry_timestamp = time();
263 $no_orig_date = 'true';
264 } else {
265 $no_orig_date = 'false';
266 }
267
268 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
269
270 $entry_title = $item["title"];
271
272 // strange Magpie workaround
273 $entry_link = $item["link_"];
274 if (!$entry_link) $entry_link = $item["link"];
275
276 if (!$entry_title) continue;
277 if (!$entry_link) continue;
278
279 $entry_content = $item["content:escaped"];
280
281 if (!$entry_content) $entry_content = $item["summary"];
282 if (!$entry_content) $entry_content = $item["content:encoded"];
283 if (!$entry_content) $entry_content = $item["content"];
284 if (!$entry_content) $entry_content = $item["description"];
285
286 $entry_content_unescaped = $entry_content;
287
288 // if (!$entry_content) continue;
289
290 // WTF
291 if (is_array($entry_content)) {
292 $entry_content = $entry_content["encoded"];
293 if (!$entry_content) $entry_content = $entry_content["escaped"];
294 }
295
296 // print_r($item);
297 // print_r($entry_content);
298
299 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
300
301 $entry_comments = $item["comments"];
302
303 $entry_guid = db_escape_string($entry_guid);
304
305 $result = db_query($link, "SELECT id FROM ttrss_entries
306 WHERE guid = '$entry_guid'");
307
308 $entry_content = db_escape_string($entry_content);
309 $entry_title = db_escape_string($entry_title);
310 $entry_link = db_escape_string($entry_link);
311 $entry_comments = db_escape_string($entry_comments);
312
313 if (db_num_rows($result) == 0) {
314
315 // base post entry does not exist, create it
316
317 $result = db_query($link,
318 "INSERT INTO ttrss_entries
319 (title,
320 guid,
321 link,
322 updated,
323 content,
324 content_hash,
325 no_orig_date,
326 date_entered,
327 comments)
328 VALUES
329 ('$entry_title',
330 '$entry_guid',
331 '$entry_link',
332 '$entry_timestamp_fmt',
333 '$entry_content',
334 '$content_hash',
335 $no_orig_date,
336 NOW(),
337 '$entry_comments')");
338 }
339
340 // now it should exist, if not - bad luck then
341
342 $result = db_query($link, "SELECT
343 id,content_hash,no_orig_date,title,
344 substring(updated,1,19) as updated
345 FROM
346 ttrss_entries
347 WHERE guid = '$entry_guid'");
348
349 if (db_num_rows($result) == 1) {
350
351 // this will be used below in update handler
352 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
353 // $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
354 // $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
355 $orig_title = db_fetch_result($result, 0, "title");
356
357 $ref_id = db_fetch_result($result, 0, "id");
358
359 // check for user post link to main table
360
361 // do we allow duplicate posts with same GUID in different feeds?
362 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
363 $dupcheck_qpart = "AND feed_id = '$feed'";
364 } else {
365 $dupcheck_qpart = "";
366 }
367
368 error_reporting(0);
369 if (is_filtered($entry_title, $entry_content, $entry_link, $filters)) {
370 continue;
371 }
372 error_reporting (DEFAULT_ERROR_LEVEL);
373
374 $result = db_query($link,
375 "SELECT ref_id FROM ttrss_user_entries WHERE
376 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
377 $dupcheck_qpart");
378
379 // okay it doesn't exist - create user entry
380 if (db_num_rows($result) == 0) {
381 $result = db_query($link,
382 "INSERT INTO ttrss_user_entries
383 (ref_id, owner_uid, feed_id)
384 VALUES ('$ref_id', '$owner_uid', '$feed')");
385 }
386
387 $post_needs_update = false;
388
389 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
390 ($content_hash != $orig_content_hash)) {
391 $post_needs_update = true;
392 }
393
394 if ($orig_title != $entry_title) {
395 $post_needs_update = true;
396 }
397
398 // this doesn't seem to be very reliable
399 //
400 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
401 // $post_needs_update = true;
402 // }
403
404 // if post needs update, update it and mark all user entries
405 // linking to this post as updated
406 if ($post_needs_update) {
407
408 // print "<!-- post $orig_title needs update : $post_needs_update -->";
409
410 db_query($link, "UPDATE ttrss_entries
411 SET title = '$entry_title', content = '$entry_content'
412 WHERE id = '$ref_id'");
413
414 db_query($link, "UPDATE ttrss_user_entries
415 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
416
417 }
418 }
419
420 /* taaaags */
421 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
422
423 $entry_tags = null;
424
425 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i",
426 $entry_content_unescaped, $entry_tags);
427
428 // print "<br>$entry_title : $entry_content_unescaped<br>";
429 // print_r($entry_tags);
430
431 $entry_tags = $entry_tags[1];
432
433 if (count($entry_tags) > 0) {
434
435 $result = db_query($link, "SELECT id,int_id
436 FROM ttrss_entries,ttrss_user_entries
437 WHERE guid = '$entry_guid'
438 AND feed_id = '$feed' AND ref_id = id
439 AND owner_uid = '$owner_uid'");
440
441 if (db_num_rows($result) == 1) {
442
443 $entry_id = db_fetch_result($result, 0, "id");
444 $entry_int_id = db_fetch_result($result, 0, "int_id");
445
446 foreach ($entry_tags as $tag) {
447 $tag = db_escape_string(strtolower($tag));
448
449 $tag = str_replace("technorati tag: ", "", $tag);
450
451 $result = db_query($link, "SELECT id FROM ttrss_tags
452 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
453 owner_uid = '$owner_uid' LIMIT 1");
454
455 // print db_fetch_result($result, 0, "id");
456
457 if ($result && db_num_rows($result) == 0) {
458
459 // print "tagging $entry_id as $tag<br>";
460
461 db_query($link, "INSERT INTO ttrss_tags
462 (owner_uid,tag_name,post_int_id)
463 VALUES ('$owner_uid','$tag', '$entry_int_id')");
464 }
465 }
466 }
467 }
468 }
469
470 db_query($link, "UPDATE ttrss_feeds
471 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
472
473 db_query($link, "COMMIT");
474
475 } else {
476 $error_msg = db_escape_string(magpie_error());
477 db_query($link,
478 "UPDATE ttrss_feeds SET last_error = '$error_msg',
479 last_updated = NOW() WHERE id = '$feed'");
480 }
481
482 }
483
484 function print_select($id, $default, $values, $attributes = "") {
485 print "<select id=\"$id\" $attributes>";
486 foreach ($values as $v) {
487 if ($v == $default)
488 $sel = " selected";
489 else
490 $sel = "";
491
492 print "<option$sel>$v</option>";
493 }
494 print "</select>";
495 }
496
497 function is_filtered($title, $content, $link, $filters) {
498
499 if ($filters["title"]) {
500 foreach ($filters["title"] as $title_filter) {
501 if (preg_match("/$title_filter/i", $title))
502 return true;
503 }
504 }
505
506 if ($filters["content"]) {
507 foreach ($filters["content"] as $content_filter) {
508 if (preg_match("/$content_filter/i", $content))
509 return true;
510 }
511 }
512
513 if ($filters["both"]) {
514 foreach ($filters["both"] as $filter) {
515 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
516 return true;
517 }
518 }
519
520 if ($filters["link"]) {
521 foreach ($filters["link"] as $link_filter) {
522 if (preg_match("/$link_filter/i", $link))
523 return true;
524 }
525 }
526
527 return false;
528 }
529
530 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
531
532 if (file_exists($icon_file) && filesize($icon_file) > 0) {
533 $feed_icon = "<img src=\"$icon_file\">";
534 } else {
535 $feed_icon = "<img src=\"images/blank_icon.gif\">";
536 }
537
538 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
539
540 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
541 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
542 print "$feed_icon";
543 }
544
545 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
546
547 if ($unread != 0) {
548 $fctr_class = "";
549 } else {
550 $fctr_class = "class=\"invisible\"";
551 }
552
553 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
554 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
555
556 print "</li>";
557
558 }
559
560 function getmicrotime() {
561 list($usec, $sec) = explode(" ",microtime());
562 return ((float)$usec + (float)$sec);
563 }
564
565 function print_radio($id, $default, $values, $attributes = "") {
566 foreach ($values as $v) {
567
568 if ($v == $default)
569 $sel = "checked";
570 else
571 $sel = "";
572
573 if ($v == "Yes") {
574 $sel .= " value=\"1\"";
575 } else {
576 $sel .= " value=\"0\"";
577 }
578
579 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
580
581 }
582 }
583
584 function initialize_user_prefs($link, $uid) {
585
586 $uid = db_escape_string($uid);
587
588 db_query($link, "BEGIN");
589
590 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
591
592 $u_result = db_query($link, "SELECT pref_name
593 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
594
595 $active_prefs = array();
596
597 while ($line = db_fetch_assoc($u_result)) {
598 array_push($active_prefs, $line["pref_name"]);
599 }
600
601 while ($line = db_fetch_assoc($result)) {
602 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
603 // print "adding " . $line["pref_name"] . "<br>";
604
605 db_query($link, "INSERT INTO ttrss_user_prefs
606 (owner_uid,pref_name,value) VALUES
607 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
608
609 }
610 }
611
612 db_query($link, "COMMIT");
613
614 }
615
616 function authenticate_user($link, $login, $password) {
617
618 $pwd_hash = 'SHA1:' . sha1($password);
619
620 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
621 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
622
623 if (db_num_rows($result) == 1) {
624 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
625 $_SESSION["name"] = db_fetch_result($result, 0, "login");
626 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
627
628 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
629 $_SESSION["uid"]);
630
631 $user_theme = get_user_theme_path($link);
632
633 $_SESSION["theme"] = $user_theme;
634
635 initialize_user_prefs($link, $_SESSION["uid"]);
636
637 return true;
638 }
639
640 return false;
641
642 }
643
644 function make_password($length = 8) {
645
646 $password = "";
647 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
648
649 $i = 0;
650
651 while ($i < $length) {
652 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
653
654 if (!strstr($password, $char)) {
655 $password .= $char;
656 $i++;
657 }
658 }
659 return $password;
660 }
661
662 // this is called after user is created to initialize default feeds, labels
663 // or whatever else
664
665 // user preferences are checked on every login, not here
666
667 function initialize_user($link, $uid) {
668
669 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
670 values ('$uid','unread = true', 'Unread articles')");
671
672 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
673 values ('$uid','last_read is null and unread = false', 'Updated articles')");
674
675 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
676 values ('$uid', 'Tiny Tiny RSS: New Releases',
677 'http://tt-rss.spb.ru/releases.rss')");
678
679 }
680
681 function logout_user() {
682 session_destroy();
683 }
684
685 function login_sequence($link) {
686 if (!SINGLE_USER_MODE) {
687
688 if (!USE_HTTP_AUTH) {
689 if (!$_SESSION["uid"]) {
690 header("Location: login.php?rt=tt-rss.php");
691 exit;
692 }
693 } else {
694 if (!$_SESSION["uid"]) {
695 if (!$_SERVER["PHP_AUTH_USER"]) {
696
697 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
698 header('HTTP/1.0 401 Unauthorized');
699 exit;
700
701 } else {
702 $auth_result = authenticate_user($link,
703 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
704
705 if (!$auth_result) {
706 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
707 header('HTTP/1.0 401 Unauthorized');
708 exit;
709 }
710 }
711 }
712 }
713 } else {
714 $_SESSION["uid"] = 1;
715 $_SESSION["name"] = "admin";
716 initialize_user_prefs($link, 1);
717 }
718 }
719
720 function truncate_string($str, $max_len) {
721 if (strlen($str) > $max_len) {
722 return substr($str, 0, $max_len) . "...";
723 } else {
724 return $str;
725 }
726 }
727
728 function get_user_theme_path($link) {
729 $result = db_query($link, "SELECT theme_path FROM ttrss_themes
730 WHERE id = (SELECT theme_id FROM ttrss_users
731 WHERE id = " . $_SESSION["uid"] . ")");
732 if (db_num_rows($result) != 0) {
733 return db_fetch_result($result, 0, "theme_path");
734 } else {
735 return null;
736 }
737 }
738 ?>