]>
git.wh0rd.org - tt-rss.git/blob - functions.php
3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
9 require_once 'config.php';
10 require_once 'db-prefs.php';
11 require_once 'compat.php';
12 require_once 'errors.php';
13 require_once 'version.php';
15 if (RSS_BACKEND_TYPE
== "magpie") {
16 require_once "magpierss/rss_fetch.inc";
17 require_once 'magpierss/rss_utils.inc';
18 } else if (RSS_BACKEND_TYPE
== "simplepie") {
19 require_once 'simplepie/simplepie.inc';
22 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
24 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
28 if (DB_TYPE
== "pgsql") {
29 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
30 marked = false AND feed_id = '$feed_id' AND
31 (SELECT date_entered FROM ttrss_entries WHERE
32 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
34 $pg_version = get_pgsql_version($link);
36 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
38 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
39 ttrss_entries.id = ref_id AND
41 feed_id = '$feed_id' AND
42 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
46 $result = db_query($link, "DELETE FROM ttrss_user_entries
48 WHERE ttrss_entries.id = ref_id AND
50 feed_id = '$feed_id' AND
51 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
54 $rows = pg_affected_rows($result);
58 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
59 marked = false AND feed_id = '$feed_id' AND
60 (SELECT date_entered FROM ttrss_entries WHERE
61 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
63 $result = db_query($link, "DELETE FROM ttrss_user_entries
64 USING ttrss_user_entries, ttrss_entries
65 WHERE ttrss_entries.id = ref_id AND
67 feed_id = '$feed_id' AND
68 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
70 $rows = mysql_affected_rows($link);
75 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
79 function global_purge_old_posts($link, $do_output = false, $limit = false) {
81 $random_qpart = sql_random_function();
84 $limit_qpart = "LIMIT $limit";
89 $result = db_query($link,
90 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
91 ORDER BY $random_qpart $limit_qpart");
93 while ($line = db_fetch_assoc($result)) {
95 $feed_id = $line["id"];
96 $purge_interval = $line["purge_interval"];
97 $owner_uid = $line["owner_uid"];
99 if ($purge_interval == 0) {
101 $tmp_result = db_query($link,
102 "SELECT value FROM ttrss_user_prefs WHERE
103 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
105 if (db_num_rows($tmp_result) != 0) {
106 $purge_interval = db_fetch_result($tmp_result, 0, "value");
111 // print "Feed $feed_id: purge interval = $purge_interval\n";
114 if ($purge_interval > 0) {
115 purge_feed($link, $feed_id, $purge_interval, $do_output);
119 // purge orphaned posts in main content table
120 db_query($link, "DELETE FROM ttrss_entries WHERE
121 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
125 function purge_old_posts($link) {
127 $user_id = $_SESSION["uid"];
129 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
130 WHERE owner_uid = '$user_id'");
132 while ($line = db_fetch_assoc($result)) {
134 $feed_id = $line["id"];
135 $purge_interval = $line["purge_interval"];
137 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
139 if ($purge_interval > 0) {
140 purge_feed($link, $feed_id, $purge_interval);
144 // purge orphaned posts in main content table
145 db_query($link, "DELETE FROM ttrss_entries WHERE
146 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
149 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
151 if (WEB_DEMO_MODE
) return;
154 $user_id = $_SESSION["uid"];
155 purge_old_posts($link);
158 // db_query($link, "BEGIN");
160 if (MAX_UPDATE_TIME
> 0) {
161 if (DB_TYPE
== "mysql") {
164 $q_order = "RANDOM()";
167 $q_order = "last_updated DESC";
170 $result = db_query($link, "SELECT feed_url,id,
171 SUBSTRING(last_updated,1,19) AS last_updated,
172 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
177 while ($line = db_fetch_assoc($result)) {
178 $upd_intl = $line["update_interval"];
180 if (!$upd_intl ||
$upd_intl == 0) {
181 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
185 // Updates for this feed are disabled
189 if ($fetch ||
(!$line["last_updated"] ||
190 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
192 // print "<!-- feed: ".$line["feed_url"]." -->";
194 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
196 $upd_elapsed = time() - $upd_start;
198 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
204 // db_query($link, "COMMIT");
208 function check_feed_favicon($feed_url, $feed, $link) {
209 $feed_url = str_replace("http://", "", $feed_url);
210 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
212 $icon_url = "http://$feed_url/favicon.ico";
213 $icon_file = ICONS_DIR
. "/$feed.ico";
215 if (!file_exists($icon_file)) {
218 $r = fopen($icon_url, "r");
219 error_reporting (DEFAULT_ERROR_LEVEL
);
222 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
224 $t = fopen($tmpfname, "w");
227 $buf = fread($r, 16384);
235 if (!rename($tmpfname, $icon_file)) {
239 chmod($icon_file, 0644);
241 error_reporting (DEFAULT_ERROR_LEVEL
);
247 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
249 if (WEB_DEMO_MODE
) return;
251 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
255 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
256 FROM ttrss_feeds WHERE id = '$feed'");
258 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
259 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
261 $update_interval = db_fetch_result($result, 0, "update_interval");
263 if ($update_interval < 0) { return; }
265 $feed = db_escape_string($feed);
267 $fetch_url = $feed_url;
269 if ($auth_login && $auth_pass) {
270 $url_parts = array();
271 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
273 if ($url_parts[1] && $url_parts[2]) {
274 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
279 if (RSS_BACKEND_TYPE
== "magpie") {
281 $rss = fetch_rss($fetch_url);
282 error_reporting (DEFAULT_ERROR_LEVEL
);
283 } else if (RSS_BACKEND_TYPE
== "simplepie") {
285 if (!file_exists(SIMPLEPIE_CACHE_DIR
)) {
286 mkdir(SIMPLEPIE_CACHE_DIR
);
289 $rss = new SimplePie();
290 $rss->feed_url($fetch_url);
291 $rss->cache_location(SIMPLEPIE_CACHE_DIR
);
295 $feed = db_escape_string($feed);
299 if (RSS_BACKEND_TYPE
== "simplepie") {
300 $rss_check = $rss->data
;
305 // db_query($link, "BEGIN");
307 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
308 FROM ttrss_feeds WHERE id = '$feed'");
310 $registered_title = db_fetch_result($result, 0, "title");
311 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
312 $orig_site_url = db_fetch_result($result, 0, "site_url");
314 $owner_uid = db_fetch_result($result, 0, "owner_uid");
316 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
317 check_feed_favicon($feed_url, $feed, $link);
320 if (!$registered_title ||
$registered_title == "[Unknown]") {
322 if (RSS_BACKEND_TYPE
== "magpie") {
323 $feed_title = db_escape_string($rss->channel
["title"]);
325 $feed_title = $rss->get_feed_title();
328 db_query($link, "UPDATE ttrss_feeds SET
329 title = '$feed_title' WHERE id = '$feed'");
332 if (RSS_BACKEND_TYPE
== "magpie") {
333 $site_url = $rss->channel
["link"];
334 // weird, weird Magpie
335 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
337 $site_url = $rss->get_feed_link();
340 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
341 db_query($link, "UPDATE ttrss_feeds SET
342 site_url = '$site_url' WHERE id = '$feed'");
345 // print "I: " . $rss->channel["image"]["url"];
347 if (RSS_BACKEND_TYPE
== "magpie") {
348 $icon_url = $rss->image
["url"];
350 $icon_url = $rss->get_image_url(); # FIXME
353 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
354 $icon_url = db_escape_string($icon_url);
355 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
361 $result = db_query($link, "SELECT reg_exp,
362 ttrss_filter_types.name AS name,
363 ttrss_filter_actions.name AS action
364 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
365 owner_uid = $owner_uid AND
366 ttrss_filter_types.id = filter_type AND
367 ttrss_filter_actions.id = action_id AND
368 (feed_id IS NULL OR feed_id = '$feed')");
370 while ($line = db_fetch_assoc($result)) {
371 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
373 $filter["reg_exp"] = $line["reg_exp"];
374 $filter["action"] = $line["action"];
376 array_push($filters[$line["name"]], $filter);
379 if (RSS_BACKEND_TYPE
== "magpie") {
380 $iterator = $rss->items
;
382 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
383 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
386 $iterator = $rss->get_items();
389 if (!is_array($iterator)) {
390 /* db_query($link, "UPDATE ttrss_feeds
391 SET last_error = 'Parse error: can\'t find any articles.'
392 WHERE id = '$feed'"); */
396 foreach ($iterator as $item) {
398 if (RSS_BACKEND_TYPE
== "magpie") {
400 $entry_guid = $item["id"];
402 if (!$entry_guid) $entry_guid = $item["guid"];
403 if (!$entry_guid) $entry_guid = $item["link"];
405 if (!$entry_guid) continue;
407 $entry_timestamp = "";
409 $rss_2_date = $item['pubdate'];
410 $rss_1_date = $item['dc']['date'];
411 $atom_date = $item['issued'];
412 if (!$atom_date) $atom_date = $item['updated'];
414 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
415 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
416 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
418 if ($entry_timestamp == "") {
419 $entry_timestamp = time();
420 $no_orig_date = 'true';
422 $no_orig_date = 'false';
425 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
427 $entry_title = $item["title"];
429 // strange Magpie workaround
430 $entry_link = $item["link_"];
431 if (!$entry_link) $entry_link = $item["link"];
433 if (!$entry_title) continue;
434 if (!$entry_link) continue;
436 $entry_content = $item["content:escaped"];
438 if (!$entry_content) $entry_content = $item["content:encoded"];
439 if (!$entry_content) $entry_content = $item["content"];
440 if (!$entry_content) $entry_content = $item["atom_content"];
441 if (!$entry_content) $entry_content = $item["summary"];
442 if (!$entry_content) $entry_content = $item["description"];
444 // if (!$entry_content) continue;
447 if (is_array($entry_content)) {
448 $entry_content = $entry_content["encoded"];
449 if (!$entry_content) $entry_content = $entry_content["escaped"];
453 // print_r(htmlspecialchars($entry_content));
456 $entry_content_unescaped = $entry_content;
457 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
459 $entry_comments = $item["comments"];
461 $entry_author = db_escape_string($item['dc']['creator']);
463 $entry_guid = db_escape_string($entry_guid);
465 $result = db_query($link, "SELECT id FROM ttrss_entries
466 WHERE guid = '$entry_guid'");
468 $entry_content = db_escape_string($entry_content);
469 $entry_title = db_escape_string($entry_title);
470 $entry_link = db_escape_string($entry_link);
471 $entry_comments = db_escape_string($entry_comments);
473 $num_comments = db_escape_string($item["slash"]["comments"]);
475 if (!$num_comments) $num_comments = 0;
477 } else if (RSS_BACKEND_TYPE
== "simplepie") {
479 $entry_guid = $item->get_id();
482 $entry_guid = $item->get_permalink();
485 if (!$entry_guid) continue;
487 $entry_timestamp = $item->get_date("U");
489 if ($entry_timestamp == "") {
490 $entry_timestamp = time();
491 $no_orig_date = 'true';
493 $no_orig_date = 'false';
496 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
498 $entry_title = $item->get_title();
499 $entry_link = $item->get_permalink();
501 if (!$entry_title) continue;
502 if (!$entry_link) continue;
504 $entry_content = $item->get_description();
506 // print_r(htmlspecialchars($entry_content));
509 $entry_content_unescaped = $entry_content;
510 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
512 $entry_comments = ""; # FIXME
514 $entry_author = $item->get_author(0);
516 $entry_author = db_escape_string($entry_author->name
);
518 $entry_guid = db_escape_string($entry_guid);
520 $result = db_query($link, "SELECT id FROM ttrss_entries
521 WHERE guid = '$entry_guid'");
523 $entry_content = db_escape_string($entry_content);
524 $entry_title = db_escape_string($entry_title);
525 $entry_link = db_escape_string($entry_link);
526 $entry_comments = db_escape_string($entry_comments);
528 $num_comments = 0; # FIXME
530 if (!$num_comments) $num_comments = 0;
536 $entry_content = sanitize_rss($entry_content);
537 $entry_title = sanitize_rss($entry_title);
538 $entry_link = sanitize_rss($entry_link);
539 $entry_comments = sanitize_rss($entry_comments);
541 db_query($link, "BEGIN");
543 if (db_num_rows($result) == 0) {
545 // base post entry does not exist, create it
547 $result = db_query($link,
548 "INSERT INTO ttrss_entries
564 '$entry_timestamp_fmt',
573 // we keep encountering the entry in feeds, so we need to
574 // update date_entered column so that we don't get horrible
575 // dupes when the entry gets purged and reinserted again e.g.
576 // in the case of SLOW SLOW OMG SLOW updating feeds
578 $base_entry_id = db_fetch_result($result, 0, "id");
580 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
581 WHERE id = '$base_entry_id'");
584 // now it should exist, if not - bad luck then
586 $result = db_query($link, "SELECT
587 id,content_hash,no_orig_date,title,
588 substring(date_entered,1,19) as date_entered,
589 substring(updated,1,19) as updated,
593 WHERE guid = '$entry_guid'");
595 if (db_num_rows($result) == 1) {
597 // this will be used below in update handler
598 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
599 $orig_title = db_fetch_result($result, 0, "title");
600 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
601 $orig_date_entered = strtotime(db_fetch_result($result,
604 $ref_id = db_fetch_result($result, 0, "id");
606 // check for user post link to main table
608 // do we allow duplicate posts with same GUID in different feeds?
609 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
610 $dupcheck_qpart = "AND feed_id = '$feed'";
612 $dupcheck_qpart = "";
615 // error_reporting(0);
617 $filter_name = get_filter_name($entry_title, $entry_content,
618 $entry_link, $filters);
620 if ($filter_name == "filter") {
624 // error_reporting (DEFAULT_ERROR_LEVEL);
626 $result = db_query($link,
627 "SELECT ref_id FROM ttrss_user_entries WHERE
628 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
631 // okay it doesn't exist - create user entry
632 if (db_num_rows($result) == 0) {
634 if ($filter_name != 'catchup') {
636 $last_read_qpart = 'NULL';
639 $last_read_qpart = 'NOW()';
642 if ($filter_name == 'mark') {
648 $result = db_query($link,
649 "INSERT INTO ttrss_user_entries
650 (ref_id, owner_uid, feed_id, unread, last_read, marked)
651 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
652 $last_read_qpart, $marked)");
655 $post_needs_update = false;
657 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
658 ($content_hash != $orig_content_hash)) {
659 $post_needs_update = true;
662 if ($orig_title != $entry_title) {
663 $post_needs_update = true;
666 if ($orig_num_comments != $num_comments) {
667 $post_needs_update = true;
670 // this doesn't seem to be very reliable
672 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
673 // $post_needs_update = true;
676 // if post needs update, update it and mark all user entries
677 // linking to this post as updated
678 if ($post_needs_update) {
680 // print "<!-- post $orig_title needs update : $post_needs_update -->";
682 db_query($link, "UPDATE ttrss_entries
683 SET title = '$entry_title', content = '$entry_content',
684 num_comments = '$num_comments'
685 WHERE id = '$ref_id'");
687 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
688 db_query($link, "UPDATE ttrss_user_entries
689 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
691 db_query($link, "UPDATE ttrss_user_entries
692 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
698 db_query($link, "COMMIT");
701 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
705 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
706 $entry_content_unescaped, $entry_tags);
708 // print "<br>$entry_title : $entry_content_unescaped<br>";
709 // print_r($entry_tags);
712 $entry_tags = $entry_tags[1];
714 if (count($entry_tags) > 0) {
716 db_query($link, "BEGIN");
718 $result = db_query($link, "SELECT id,int_id
719 FROM ttrss_entries,ttrss_user_entries
720 WHERE guid = '$entry_guid'
721 AND feed_id = '$feed' AND ref_id = id
722 AND owner_uid = '$owner_uid'");
724 if (db_num_rows($result) == 1) {
726 $entry_id = db_fetch_result($result, 0, "id");
727 $entry_int_id = db_fetch_result($result, 0, "int_id");
729 foreach ($entry_tags as $tag) {
730 $tag = db_escape_string(strtolower($tag));
732 $tag = str_replace("+", " ", $tag);
733 $tag = str_replace("technorati tag: ", "", $tag);
735 $result = db_query($link, "SELECT id FROM ttrss_tags
736 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
737 owner_uid = '$owner_uid' LIMIT 1");
739 // print db_fetch_result($result, 0, "id");
741 if ($result && db_num_rows($result) == 0) {
743 // print "tagging $entry_id as $tag<br>";
745 db_query($link, "INSERT INTO ttrss_tags
746 (owner_uid,tag_name,post_int_id)
747 VALUES ('$owner_uid','$tag', '$entry_int_id')");
751 db_query($link, "COMMIT");
755 db_query($link, "UPDATE ttrss_feeds
756 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
758 // db_query($link, "COMMIT");
761 $error_msg = db_escape_string(magpie_error());
763 "UPDATE ttrss_feeds SET last_error = '$error_msg',
764 last_updated = NOW() WHERE id = '$feed'");
769 function print_select($id, $default, $values, $attributes = "") {
770 print "<select name=\"$id\" id=\"$id\" $attributes>";
771 foreach ($values as $v) {
777 print "<option$sel>$v</option>";
782 function print_select_hash($id, $default, $values, $attributes = "") {
783 print "<select name=\"$id\" id='$id' $attributes>";
784 foreach (array_keys($values) as $v) {
790 print "<option $sel value=\"$v\">".$values[$v]."</option>";
796 function get_filter_name($title, $content, $link, $filters) {
798 if ($filters["title"]) {
799 foreach ($filters["title"] as $filter) {
800 $reg_exp = $filter["reg_exp"];
801 if (preg_match("/$reg_exp/i", $title)) {
802 return $filter["action"];
807 if ($filters["content"]) {
808 foreach ($filters["content"] as $filter) {
809 $reg_exp = $filter["reg_exp"];
810 if (preg_match("/$reg_exp/i", $content)) {
811 return $filter["action"];
816 if ($filters["both"]) {
817 foreach ($filters["both"] as $filter) {
818 $reg_exp = $filter["reg_exp"];
819 if (preg_match("/$reg_exp/i", $title) ||
820 preg_match("/$reg_exp/i", $content)) {
821 return $filter["action"];
826 if ($filters["link"]) {
827 $reg_exp = $filter["reg_exp"];
828 foreach ($filters["link"] as $filter) {
829 $reg_exp = $filter["reg_exp"];
830 if (preg_match("/$reg_exp/i", $link)) {
831 return $filter["action"];
839 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
840 $rtl_content = false, $last_updated = false, $last_error = false) {
842 if (file_exists($icon_file) && filesize($icon_file) > 0) {
843 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
845 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
849 $rtl_tag = "dir=\"rtl\"";
851 $rtl_tag = "dir=\"ltr\"";
854 $error_notify_msg = "";
857 $link_title = "Error: $last_error ($last_updated)";
858 $error_notify_msg = "(Error)";
859 } else if ($last_updated) {
860 $link_title = "Updated: $last_updated";
863 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
865 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
866 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
870 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
875 $fctr_class = "class=\"invisible\"";
878 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
879 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
881 if (get_pref($link, "EXTENDED_FEEDLIST")) {
882 print "<div class=\"feedExtInfo\">
883 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
890 function getmicrotime() {
891 list($usec, $sec) = explode(" ",microtime());
892 return ((float)$usec +
(float)$sec);
895 function print_radio($id, $default, $values, $attributes = "") {
896 foreach ($values as $v) {
904 $sel .= " value=\"1\"";
906 $sel .= " value=\"0\"";
909 print "<input class=\"noborder\"
910 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
915 function initialize_user_prefs($link, $uid) {
917 $uid = db_escape_string($uid);
919 db_query($link, "BEGIN");
921 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
923 $u_result = db_query($link, "SELECT pref_name
924 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
926 $active_prefs = array();
928 while ($line = db_fetch_assoc($u_result)) {
929 array_push($active_prefs, $line["pref_name"]);
932 while ($line = db_fetch_assoc($result)) {
933 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
934 // print "adding " . $line["pref_name"] . "<br>";
936 db_query($link, "INSERT INTO ttrss_user_prefs
937 (owner_uid,pref_name,value) VALUES
938 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
943 db_query($link, "COMMIT");
947 function lookup_user_id($link, $user) {
949 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
952 if (db_num_rows($result) == 1) {
953 return db_fetch_result($result, 0, "id");
959 function http_authenticate_user($link) {
961 if (!$_SERVER["PHP_AUTH_USER"]) {
963 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
964 header('HTTP/1.0 401 Unauthorized');
968 $auth_result = authenticate_user($link,
969 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
972 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
973 header('HTTP/1.0 401 Unauthorized');
981 function authenticate_user($link, $login, $password) {
983 if (!SINGLE_USER_MODE
) {
985 $pwd_hash = 'SHA1:' . sha1($password);
987 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
988 login = '$login' AND pwd_hash = '$pwd_hash'");
990 if (db_num_rows($result) == 1) {
991 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
992 $_SESSION["name"] = db_fetch_result($result, 0, "login");
993 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
995 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
998 $user_theme = get_user_theme_path($link);
1000 $_SESSION["theme"] = $user_theme;
1001 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1003 initialize_user_prefs($link, $_SESSION["uid"]);
1012 $_SESSION["uid"] = 1;
1013 $_SESSION["name"] = "admin";
1015 $user_theme = get_user_theme_path($link);
1017 $_SESSION["theme"] = $user_theme;
1018 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1020 initialize_user_prefs($link, $_SESSION["uid"]);
1026 function make_password($length = 8) {
1029 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1033 while ($i < $length) {
1034 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1036 if (!strstr($password, $char)) {
1044 // this is called after user is created to initialize default feeds, labels
1047 // user preferences are checked on every login, not here
1049 function initialize_user($link, $uid) {
1051 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1052 values ('$uid','unread = true', 'Unread articles')");
1054 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1055 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1057 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1058 values ('$uid', 'Tiny Tiny RSS: New Releases',
1059 'http://tt-rss.spb.ru/releases.rss')");
1063 function logout_user() {
1065 if (isset($_COOKIE[session_name()])) {
1066 setcookie(session_name(), '', time()-42000, '/');
1070 function get_script_urlpath() {
1071 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1074 function get_login_redirect() {
1075 $server = $_SERVER["SERVER_NAME"];
1077 if (ENABLE_LOGIN_SSL
) {
1078 $protocol = "https";
1083 $url_path = get_script_urlpath();
1085 $redirect_uri = "$protocol://$server$url_path/login.php";
1087 return $redirect_uri;
1090 function validate_session($link) {
1091 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1092 if ($_SESSION["ip_address"]) {
1093 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1101 function basic_nosid_redirect_check() {
1102 if (!SINGLE_USER_MODE
) {
1103 if (!$_COOKIE[get_session_cookie_name()]) {
1104 $redirect_uri = get_login_redirect();
1105 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1106 header("Location: $redirect_uri?rt=$return_to");
1112 function login_sequence($link) {
1113 if (!SINGLE_USER_MODE
) {
1115 if (!validate_session($link)) {
1117 $redirect_uri = get_login_redirect();
1118 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1119 header("Location: $redirect_uri?rt=$return_to");
1123 if (!USE_HTTP_AUTH
) {
1124 if (!$_SESSION["uid"]) {
1125 $redirect_uri = get_login_redirect();
1126 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1127 header("Location: $redirect_uri?rt=$return_to");
1131 if (!$_SESSION["uid"]) {
1132 if (!$_SERVER["PHP_AUTH_USER"]) {
1134 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1135 header('HTTP/1.0 401 Unauthorized');
1139 $auth_result = authenticate_user($link,
1140 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1142 if (!$auth_result) {
1143 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1144 header('HTTP/1.0 401 Unauthorized');
1151 return authenticate_user($link, "admin", null);
1155 function truncate_string($str, $max_len) {
1156 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1157 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1163 function get_user_theme_path($link) {
1164 $result = db_query($link, "SELECT theme_path
1166 ttrss_themes,ttrss_users
1167 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1168 if (db_num_rows($result) != 0) {
1169 return db_fetch_result($result, 0, "theme_path");
1175 function smart_date_time($timestamp) {
1176 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1177 return date("G:i", $timestamp);
1178 } else if (date("Y", $timestamp) == date("Y")) {
1179 return date("M d, G:i", $timestamp);
1181 return date("Y/m/d G:i", $timestamp);
1185 function smart_date($timestamp) {
1186 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1188 } else if (date("Y", $timestamp) == date("Y")) {
1189 return date("D m", $timestamp);
1191 return date("Y/m/d", $timestamp);
1195 function sql_bool_to_string($s) {
1196 if ($s == "t" ||
$s == "1") {
1203 function sql_bool_to_bool($s) {
1204 if ($s == "t" ||
$s == "1") {
1212 function toggleEvenOdd($a) {
1219 function sanity_check($link) {
1224 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1225 $schema_version = db_fetch_result($result, 0, "schema_version");
1227 if ($schema_version != SCHEMA_VERSION
) {
1231 if (DB_TYPE
== "mysql") {
1232 $result = db_query($link, "SELECT true", false);
1233 if (db_num_rows($result) != 1) {
1238 error_reporting (DEFAULT_ERROR_LEVEL
);
1240 if ($error_code != 0) {
1241 print_error_xml($error_code);
1248 function file_is_locked($filename) {
1250 $fp = fopen($filename, "r");
1251 error_reporting(DEFAULT_ERROR_LEVEL
);
1253 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1254 flock($fp, LOCK_UN
);
1264 function make_lockfile($filename) {
1265 $fp = fopen($filename, "w");
1267 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1274 function sql_random_function() {
1275 if (DB_TYPE
== "mysql") {
1282 function catchup_feed($link, $feed, $cat_view) {
1284 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1289 $cat_qpart = "cat_id = '$feed'";
1291 $cat_qpart = "cat_id IS NULL";
1294 $tmp_result = db_query($link, "SELECT id
1295 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1298 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1300 $tmp_feed = $tmp_line["id"];
1302 db_query($link, "UPDATE ttrss_user_entries
1303 SET unread = false,last_read = NOW()
1304 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1307 } else if ($feed > 0) {
1309 $tmp_result = db_query($link, "SELECT id
1310 FROM ttrss_feeds WHERE parent_feed = '$feed'
1311 ORDER BY cat_id,title");
1313 $parent_ids = array();
1315 if (db_num_rows($tmp_result) > 0) {
1316 while ($p = db_fetch_assoc($tmp_result)) {
1317 array_push($parent_ids, "feed_id = " . $p["id"]);
1320 $children_qpart = implode(" OR ", $parent_ids);
1322 db_query($link, "UPDATE ttrss_user_entries
1323 SET unread = false,last_read = NOW()
1324 WHERE (feed_id = '$feed' OR $children_qpart)
1325 AND owner_uid = " . $_SESSION["uid"]);
1328 db_query($link, "UPDATE ttrss_user_entries
1329 SET unread = false,last_read = NOW()
1330 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1333 } else if ($feed < 0 && $feed > -10) { // special, like starred
1336 db_query($link, "UPDATE ttrss_user_entries
1337 SET unread = false,last_read = NOW()
1338 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1341 } else if ($feed < -10) { // label
1343 // TODO make this more efficient
1345 $label_id = -$feed - 11;
1347 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1348 WHERE id = '$label_id'");
1351 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1353 db_query($link, "BEGIN");
1355 $tmp2_result = db_query($link,
1359 ttrss_user_entries,ttrss_entries,ttrss_feeds
1361 ref_id = ttrss_entries.id AND
1362 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1364 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1366 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1367 db_query($link, "UPDATE
1370 unread = false, last_read = NOW()
1372 int_id = " . $tmp_line["int_id"]);
1375 db_query($link, "COMMIT");
1377 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1378 SET unread = false,last_read = NOW()
1381 AND owner_uid = ".$_SESSION["uid"]); */
1385 db_query($link, "BEGIN");
1387 $tag_name = db_escape_string($feed);
1389 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1390 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1392 while ($line = db_fetch_assoc($result)) {
1393 db_query($link, "UPDATE ttrss_user_entries SET
1394 unread = false, last_read = NOW()
1395 WHERE int_id = " . $line["post_int_id"]);
1397 db_query($link, "COMMIT");
1401 function update_generic_feed($link, $feed, $cat_view) {
1405 $cat_qpart = "cat_id = '$feed'";
1407 $cat_qpart = "cat_id IS NULL";
1410 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1411 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1413 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1414 $feed_url = $tmp_line["feed_url"];
1415 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1419 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1420 WHERE id = '$feed'");
1421 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1422 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1426 function getAllCounters($link) {
1427 getLabelCounters($link);
1428 getFeedCounters($link);
1429 getTagCounters($link);
1430 getGlobalCounters($link);
1431 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1432 getCategoryCounters($link);
1436 function getCategoryCounters($link) {
1437 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1438 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1439 AND unread = true)) AS unread FROM ttrss_feeds
1441 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1443 while ($line = db_fetch_assoc($result)) {
1444 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1445 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1446 $line["unread"]."\"/>";
1450 function getCategoryUnread($link, $cat) {
1453 $cat_query = "cat_id = '$cat'";
1455 $cat_query = "cat_id IS NULL";
1458 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1460 AND owner_uid = " . $_SESSION["uid"]);
1462 $cat_feeds = array();
1463 while ($line = db_fetch_assoc($result)) {
1464 array_push($cat_feeds, "feed_id = " . $line["id"]);
1467 if (count($cat_feeds) == 0) return 0;
1469 $match_part = implode(" OR ", $cat_feeds);
1471 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1472 FROM ttrss_user_entries
1473 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1477 # this needs to be rewritten
1478 while ($line = db_fetch_assoc($result)) {
1479 $unread +
= $line["unread"];
1486 function getFeedUnread($link, $feed, $is_cat = false) {
1487 $n_feed = sprintf("%d", $feed);
1490 return getCategoryUnread($link, $n_feed);
1491 } else if ($n_feed == -1) {
1492 $match_part = "marked = true";
1493 } else if ($n_feed > 0) {
1495 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1497 AND owner_uid = " . $_SESSION["uid"]);
1499 if (db_num_rows($result) > 0) {
1501 $linked_feeds = array();
1502 while ($line = db_fetch_assoc($result)) {
1503 array_push($linked_feeds, "feed_id = " . $line["id"]);
1506 $match_part = implode(" OR ", $linked_feeds);
1508 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1509 FROM ttrss_user_entries
1510 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1514 # this needs to be rewritten
1515 while ($line = db_fetch_assoc($result)) {
1516 $unread +
= $line["unread"];
1522 $match_part = "feed_id = '$n_feed'";
1524 } else if ($feed < -10) {
1526 $label_id = -$feed - 11;
1528 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1529 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1531 $match_part = db_fetch_result($result, 0, "sql_exp");
1536 $result = db_query($link, "SELECT count(int_id) AS unread
1537 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1538 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1539 ttrss_user_entries.ref_id = ttrss_entries.id AND
1540 ttrss_feeds.hidden = false AND
1541 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1545 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1546 FROM ttrss_tags,ttrss_user_entries
1547 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1548 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1551 $unread = db_fetch_result($result, 0, "unread");
1556 /* FIXME this needs reworking */
1558 function getGlobalUnread($link, $user_id = false) {
1561 $user_id = $_SESSION["uid"];
1564 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1565 WHERE unread = true AND
1566 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1567 ttrss_user_entries.ref_id = ttrss_entries.id AND
1569 ttrss_user_entries.owner_uid = '$user_id'");
1570 $c_id = db_fetch_result($result, 0, "c_id");
1574 function getGlobalCounters($link, $global_unread = -1) {
1575 if ($global_unread == -1) {
1576 $global_unread = getGlobalUnread($link);
1578 print "<counter type=\"global\" id='global-unread'
1579 counter='$global_unread'/>";
1581 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1582 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1584 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1586 print "<counter type=\"global\" id='subscribed-feeds'
1587 counter='$subscribed_feeds'/>";
1591 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1594 if (!$_SESSION["tctr_last_value"]) {
1595 $_SESSION["tctr_last_value"] = array();
1599 $old_counters = $_SESSION["tctr_last_value"];
1601 $tctrs_modified = false;
1603 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1604 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1605 ttrss_user_entries.ref_id = ttrss_entries.id AND
1606 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1607 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1609 select tag_name,0 as count FROM ttrss_tags
1610 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1612 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1613 FROM ttrss_user_entries WHERE int_id = post_int_id
1614 AND unread = true)) AS count FROM ttrss_tags
1615 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1619 while ($line = db_fetch_assoc($result)) {
1620 $tags[$line["tag_name"]] +
= $line["count"];
1623 foreach (array_keys($tags) as $tag) {
1624 $unread = $tags[$tag];
1626 $tag = htmlspecialchars($tag);
1628 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1629 $old_counters[$tag] = $unread;
1630 $tctrs_modified = true;
1631 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1636 if ($smart_mode && $tctrs_modified) {
1637 $_SESSION["tctr_last_value"] = $old_counters;
1642 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1645 if (!$_SESSION["lctr_last_value"]) {
1646 $_SESSION["lctr_last_value"] = array();
1652 $old_counters = $_SESSION["lctr_last_value"];
1653 $lctrs_modified = false;
1655 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1656 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1657 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1658 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1660 $count = db_fetch_result($result, 0, "count");
1663 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1665 $ret_arr["-1"]["counter"] = $count;
1666 $ret_arr["-1"]["description"] = "Starred";
1669 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1670 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1672 while ($line = db_fetch_assoc($result)) {
1674 $id = -$line["id"] - 11;
1676 $label_name = $line["description"];
1678 error_reporting (0);
1680 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1681 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1682 ttrss_feeds.hidden = false AND
1683 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1684 ttrss_user_entries.ref_id = ttrss_entries.id AND
1685 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1687 $count = db_fetch_result($tmp_result, 0, "count");
1689 if (!$smart_mode ||
$old_counters[$id] != $count) {
1690 $old_counters[$id] = $count;
1691 $lctrs_modified = true;
1693 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1695 $ret_arr[$id]["counter"] = $count;
1696 $ret_arr[$id]["description"] = $label_name;
1700 error_reporting (DEFAULT_ERROR_LEVEL
);
1703 if ($smart_mode && $lctrs_modified) {
1704 $_SESSION["lctr_last_value"] = $old_counters;
1710 /* function getFeedCounter($link, $id) {
1712 $result = db_query($link, "SELECT
1713 count(id) as count,last_error
1714 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1715 WHERE feed_id = '$id' AND unread = true
1716 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1717 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1719 $count = db_fetch_result($result, 0, "count");
1720 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1722 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1725 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1728 if (!$_SESSION["fctr_last_value"]) {
1729 $_SESSION["fctr_last_value"] = array();
1733 $old_counters = $_SESSION["fctr_last_value"];
1735 $result = db_query($link, "SELECT id,last_error,parent_feed,
1736 SUBSTRING(last_updated,1,19) AS last_updated,
1738 FROM ttrss_entries,ttrss_user_entries
1739 WHERE feed_id = ttrss_feeds.id AND
1740 ttrss_user_entries.ref_id = ttrss_entries.id
1741 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1742 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1743 AND parent_feed IS NULL");
1745 $fctrs_modified = false;
1747 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1749 while ($line = db_fetch_assoc($result)) {
1752 $count = $line["count"];
1753 $last_error = htmlspecialchars($line["last_error"]);
1755 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1756 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1758 $last_updated = date($short_date, strtotime($line["last_updated"]));
1761 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1763 $tmp_result = db_query($link,
1764 "SELECT id,COUNT(unread) AS unread
1765 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1766 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1767 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1769 if (db_num_rows($tmp_result) > 0) {
1770 while ($l = db_fetch_assoc($tmp_result)) {
1771 $count +
= $l["unread"];
1775 if (!$smart_mode ||
$old_counters[$id] != $count) {
1776 $old_counters[$id] = $count;
1777 $fctrs_modified = true;
1780 $error_part = "error=\"$last_error\"";
1786 $has_img_part = "hi=\"$has_img\"";
1791 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1795 if ($smart_mode && $fctrs_modified) {
1796 $_SESSION["fctr_last_value"] = $old_counters;
1800 function get_script_dt_add() {
1801 if (strpos(VERSION
, "99") === false) {
1808 function get_pgsql_version($link) {
1809 $result = db_query($link, "SELECT version() AS version");
1810 $version = split(" ", db_fetch_result($result, 0, "version"));
1814 function print_error_xml($code, $add_msg = "") {
1817 $error_msg = $ERRORS[$code];
1820 $error_msg = "$error_msg; $add_msg";
1823 print "<rpc-reply>";
1824 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1825 print "</rpc-reply>";
1828 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1830 if ($cat_id == "0" ||
!$cat_id) {
1831 $cat_qpart = "NULL";
1833 $cat_qpart = "'$cat_id'";
1836 $result = db_query($link,
1837 "SELECT id FROM ttrss_feeds
1838 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1840 if (db_num_rows($result) == 0) {
1842 $result = db_query($link,
1843 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1844 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1845 '[Unknown]', $cat_qpart)");
1847 $result = db_query($link,
1848 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1849 AND owner_uid = " . $_SESSION["uid"]);
1851 $feed_id = db_fetch_result($result, 0, "id");
1854 update_rss_feed($link, $feed_link, $feed_id, true);
1863 function print_feed_select($link, $id, $default_id = "",
1864 $attributes = "", $include_all_feeds = true) {
1866 print "<select id=\"$id\" name=\"$id\" $attributes>";
1867 if ($include_all_feeds) {
1868 print "<option value=\"0\">All feeds</option>";
1871 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1872 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1874 if (db_num_rows($result) > 0 && $include_all_feeds) {
1875 print "<option disabled>--------</option>";
1878 while ($line = db_fetch_assoc($result)) {
1879 if ($line["id"] == $default_id) {
1880 $is_selected = "selected";
1884 printf("<option $is_selected value='%d'>%s</option>",
1885 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1891 function print_feed_cat_select($link, $id, $default_id = "",
1892 $attributes = "", $include_all_cats = true) {
1894 print "<select id=\"$id\" name=\"$id\" $attributes>";
1896 if ($include_all_cats) {
1897 print "<option value=\"0\">Uncategorized</option>";
1900 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1901 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1903 if (db_num_rows($result) > 0 && $include_all_cats) {
1904 print "<option disabled>--------</option>";
1907 while ($line = db_fetch_assoc($result)) {
1908 if ($line["id"] == $default_id) {
1909 $is_selected = "selected";
1913 printf("<option $is_selected value='%d'>%s</option>",
1914 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1920 function checkbox_to_sql_bool($val) {
1921 return ($val == "on") ?
"true" : "false";
1924 function getFeedCatTitle($link, $id) {
1927 } else if ($id < -10) {
1929 } else if ($id > 0) {
1930 $result = db_query($link, "SELECT ttrss_feed_categories.title
1931 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1932 cat_id = ttrss_feed_categories.id");
1933 if (db_num_rows($result) == 1) {
1934 return db_fetch_result($result, 0, "title");
1936 return "Uncategorized";
1939 return "getFeedCatTitle($id) failed";
1944 function getFeedTitle($link, $id) {
1946 return "Starred articles";
1947 } else if ($id < -10) {
1948 $label_id = -10 - $id;
1949 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1950 if (db_num_rows($result) == 1) {
1951 return db_fetch_result($result, 0, "description");
1953 return "Unknown label ($label_id)";
1956 } else if ($id > 0) {
1957 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1958 if (db_num_rows($result) == 1) {
1959 return db_fetch_result($result, 0, "title");
1961 return "Unknown feed ($id)";
1964 return "getFeedTitle($id) failed";
1969 function get_session_cookie_name() {
1970 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
1973 function print_init_params($link) {
1974 print "<init-params>";
1975 if ($_SESSION["stored-params"]) {
1976 foreach (array_keys($_SESSION["stored-params"]) as $key) {
1978 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
1979 print "<param key=\"$key\" value=\"$value\"/>";
1984 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
1985 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
1986 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
1988 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
1989 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
1991 print "<param key=\"hide_read_feeds\" value=\"" .
1992 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
1994 print "<param key=\"feeds_sort_by_unread\" value=\"" .
1995 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
1997 print "</init-params>";
2000 function print_runtime_info($link) {
2001 print "<runtime-info>";
2002 if (ENABLE_UPDATE_DAEMON
) {
2003 print "<param key=\"daemon_is_running\" value=\"".
2004 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2006 print "</runtime-info>";
2009 function getSearchSql($search, $match_on) {
2011 $search_query_part = "";
2013 $keywords = split(" ", $search);
2014 $query_keywords = array();
2016 if ($match_on == "both") {
2018 foreach ($keywords as $k) {
2019 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2020 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2023 $search_query_part = implode("AND", $query_keywords) . " AND ";
2025 } else if ($match_on == "title") {
2027 foreach ($keywords as $k) {
2028 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2031 $search_query_part = implode("AND", $query_keywords) . " AND ";
2033 } else if ($match_on == "content") {
2035 foreach ($keywords as $k) {
2036 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2040 $search_query_part = implode("AND", $query_keywords);
2042 return $search_query_part;
2045 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2048 $search_query_part = getSearchSql($search, $match_on);
2049 $search_query_part .= " AND ";
2052 $search_query_part = "";
2055 $view_query_part = "";
2057 if ($view_mode == "adaptive") {
2059 $view_query_part = " ";
2060 } else if ($feed != -1) {
2061 $unread = getFeedUnread($link, $feed, $cat_view);
2063 $view_query_part = " unread = true AND ";
2068 if ($view_mode == "marked") {
2069 $view_query_part = " marked = true AND ";
2072 if ($view_mode == "unread") {
2073 $view_query_part = " unread = true AND ";
2077 $limit_query_part = "LIMIT " . $limit;
2080 $vfeed_query_part = "";
2082 // override query strategy and enable feed display when searching globally
2083 if ($search && $search_mode == "all_feeds") {
2084 $query_strategy_part = "ttrss_entries.id > 0";
2085 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2086 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2087 $query_strategy_part = "ttrss_entries.id > 0";
2088 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2089 id = feed_id) as feed_title,";
2090 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2092 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2094 $tmp_result = false;
2097 $tmp_result = db_query($link, "SELECT id
2098 FROM ttrss_feeds WHERE cat_id = '$feed'");
2100 $tmp_result = db_query($link, "SELECT id
2101 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2102 WHERE id = '$feed') AND id != '$feed'");
2105 $cat_siblings = array();
2107 if (db_num_rows($tmp_result) > 0) {
2108 while ($p = db_fetch_assoc($tmp_result)) {
2109 array_push($cat_siblings, "feed_id = " . $p["id"]);
2112 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2113 $feed, implode(" OR ", $cat_siblings));
2116 $query_strategy_part = "ttrss_entries.id > 0";
2119 } else if ($feed >= 0) {
2124 $query_strategy_part = "cat_id = '$feed'";
2126 $query_strategy_part = "cat_id IS NULL";
2129 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2132 $tmp_result = db_query($link, "SELECT id
2133 FROM ttrss_feeds WHERE parent_feed = '$feed'
2134 ORDER BY cat_id,title");
2136 $parent_ids = array();
2138 if (db_num_rows($tmp_result) > 0) {
2139 while ($p = db_fetch_assoc($tmp_result)) {
2140 array_push($parent_ids, "feed_id = " . $p["id"]);
2143 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2144 $feed, implode(" OR ", $parent_ids));
2146 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2148 $query_strategy_part = "feed_id = '$feed'";
2151 } else if ($feed == -1) { // starred virtual feed
2152 $query_strategy_part = "marked = true";
2153 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2154 } else if ($feed <= -10) { // labels
2155 $label_id = -$feed - 11;
2157 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2158 WHERE id = '$label_id'");
2160 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2162 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2164 $query_strategy_part = "id > 0"; // dumb
2167 if (get_pref($link, 'REVERSE_HEADLINES')) {
2168 $order_by = "updated";
2170 $order_by = "updated DESC";
2173 if ($override_order) {
2174 $order_by = $override_order;
2179 if ($search && $search_mode == "all_feeds") {
2180 $feed_title = "Global search results ($search)";
2181 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2182 $feed_title = "Tag search results ($search, $feed)";
2183 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2184 $feed_title = $feed;
2185 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2190 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2191 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2192 $feed_title = db_fetch_result($result, 0, "title");
2194 $feed_title = "Uncategorized";
2198 $feed_title = "Category search results ($search, $feed_title)";
2203 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2204 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2206 $feed_title = db_fetch_result($result, 0, "title");
2207 $feed_site_url = db_fetch_result($result, 0, "site_url");
2208 $last_error = db_fetch_result($result, 0, "last_error");
2211 $feed_title = "Feed search results ($search, $feed_title)";
2215 } else if ($feed == -1) {
2216 $feed_title = "Starred articles";
2217 } else if ($feed < -10) {
2218 $label_id = -$feed - 11;
2219 $result = db_query($link, "SELECT description FROM ttrss_labels
2220 WHERE id = '$label_id'");
2221 $feed_title = db_fetch_result($result, 0, "description");
2224 $feed_title = "Label search results ($search, $feed_title)";
2230 $feed_title = db_unescape_string($feed_title);
2232 if ($feed < -10) error_reporting (0);
2234 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2237 $feed_kind = "Feeds";
2239 $feed_kind = "Labels";
2242 $content_query_part = "content as content_preview,";
2245 ttrss_entries.id,ttrss_entries.title,
2246 SUBSTRING(updated,1,16) as updated,
2247 unread,feed_id,marked,link,last_read,
2248 SUBSTRING(last_read,1,19) as last_read_noms,
2251 SUBSTRING(updated,1,19) as updated_noms
2253 ttrss_entries,ttrss_user_entries,ttrss_feeds
2255 ttrss_feeds.hidden = false AND
2256 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2257 ttrss_user_entries.ref_id = ttrss_entries.id AND
2258 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2261 $query_strategy_part ORDER BY $order_by
2264 $result = db_query($link, $query);
2266 if ($_GET["debug"]) print $query;
2271 $feed_kind = "Tags";
2273 $result = db_query($link, "SELECT
2274 ttrss_entries.id as id,title,
2275 SUBSTRING(updated,1,16) as updated,
2277 marked,link,last_read,
2278 SUBSTRING(last_read,1,19) as last_read_noms,
2281 SUBSTRING(updated,1,19) as updated_noms
2283 ttrss_entries,ttrss_user_entries,ttrss_tags
2285 ref_id = ttrss_entries.id AND
2286 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2287 post_int_id = int_id AND tag_name = '$feed' AND
2290 $query_strategy_part ORDER BY $order_by
2291 $limit_query_part");
2294 return array($result, $feed_title, $feed_site_url, $last_error);
2298 function generate_syndicated_feed($link, $feed, $is_cat,
2299 $search, $search_mode, $match_on) {
2301 $qfh_ret = queryFeedHeadlines($link, $feed,
2302 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2304 $result = $qfh_ret[0];
2305 $feed_title = htmlspecialchars($qfh_ret[1]);
2306 $feed_site_url = $qfh_ret[2];
2307 $last_error = $qfh_ret[3];
2309 print "<rss version=\"2.0\">
2311 <title>$feed_title</title>
2312 <link>$feed_site_url</link>
2313 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2315 while ($line = db_fetch_assoc($result)) {
2317 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2319 $rfc822_date = date('r', strtotime($line["updated"]));
2321 print "<pubDate>$rfc822_date</pubDate>";
2324 htmlspecialchars($line["title"]) . "</title>";
2326 print "<description>" .
2327 htmlspecialchars($line["content_preview"]) . "</description>";
2332 print "</channel></rss>";
2336 function getCategoryTitle($link, $cat_id) {
2338 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2341 if (db_num_rows($result) == 1) {
2342 return db_fetch_result($result, 0, "title");
2344 return "Uncategorized";
2348 function sanitize_rss($str) {
2351 $res = preg_replace('/<script.*?>/i',
2352 "<p class=\"scriptWarn\">", $str);
2354 $res = preg_replace('/<\/script>/i',
2360 function check_for_update($link) {
2361 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2363 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2368 $rss = fetch_rss($releases_feed);
2369 error_reporting (DEFAULT_ERROR_LEVEL
);
2373 $items = $rss->items
;
2375 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2376 if (!$items ||
!is_array($items)) $items = $rss;
2378 if (!is_array($items)) {
2382 $last_item = $items[0];
2384 $last_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_item["title"]));
2385 $cur_version = preg_replace("/\.99/", "", VERSION
);
2387 $release_url = $last_item["link"];
2389 if ($cur_version != $last_version) {
2390 return "<div class=\"notice\"><a target=\"_new\" href=\"$release_url\">
2391 New version of Tiny-Tiny RSS is available.</a></div>";