]>
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, false);
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 fetch_file_contents($url) {
209 if (USE_CURL_FOR_ICONS
) {
210 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
212 $ch = curl_init($url);
213 $fp = fopen($tmpfile, "w");
216 curl_setopt($ch, CURLOPT_FILE
, $fp);
222 $contents = file_get_contents($tmpfile);
228 return file_get_contents($url);
233 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
234 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
236 function get_favicon_url($url) {
238 if ($html = @fetch_file_contents
($url)) {
240 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
241 // Attempt to grab a favicon link from their webpage url
242 $linkUrl = html_entity_decode($matches[1]);
244 if (substr($linkUrl, 0, 1) == '/') {
245 $urlParts = parse_url($url);
246 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
247 } else if (substr($linkUrl, 0, 7) == 'http://') {
248 $faviconURL = $linkUrl;
249 } else if (substr($url, -1, 1) == '/') {
250 $faviconURL = $url.$linkUrl;
252 $faviconURL = $url.'/'.$linkUrl;
256 // If unsuccessful, attempt to "guess" the favicon location
257 $urlParts = parse_url($url);
258 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
262 // Run a test to see if what we have attempted to get actually exists.
263 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
270 function url_validate($link) {
272 $url_parts = @parse_url
($link);
274 if ( empty( $url_parts["host"] ) )
277 if ( !empty( $url_parts["path"] ) ) {
278 $documentpath = $url_parts["path"];
283 if ( !empty( $url_parts["query"] ) )
284 $documentpath .= "?" . $url_parts["query"];
286 $host = $url_parts["host"];
287 $port = $url_parts["port"];
292 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
297 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
299 $http_response = fgets( $socket, 22 );
301 $responses = "/(200 OK)|(30[0-9] Moved)/";
302 if ( preg_match($responses, $http_response) ) {
311 function check_feed_favicon($site_url, $feed, $link) {
312 $favicon_url = get_favicon_url($site_url);
314 # print "FAVICON [$site_url]: $favicon_url\n";
318 $icon_file = ICONS_DIR
. "/$feed.ico";
320 if ($favicon_url && !file_exists($icon_file)) {
321 $contents = fetch_file_contents($favicon_url);
323 $fp = fopen($icon_file, "w");
326 fwrite($fp, $contents);
328 chmod($icon_file, 0644);
332 error_reporting(DEFAULT_ERROR_LEVEL
);
336 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
338 if (WEB_DEMO_MODE
) return;
340 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
344 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
345 FROM ttrss_feeds WHERE id = '$feed'");
347 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
348 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
350 $update_interval = db_fetch_result($result, 0, "update_interval");
352 if ($update_interval < 0) { return; }
354 $feed = db_escape_string($feed);
356 $fetch_url = $feed_url;
358 if ($auth_login && $auth_pass) {
359 $url_parts = array();
360 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
362 if ($url_parts[1] && $url_parts[2]) {
363 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
368 if (RSS_BACKEND_TYPE
== "magpie") {
370 $rss = fetch_rss($fetch_url);
371 error_reporting (DEFAULT_ERROR_LEVEL
);
372 } else if (RSS_BACKEND_TYPE
== "simplepie") {
374 if (!file_exists(SIMPLEPIE_CACHE_DIR
)) {
375 mkdir(SIMPLEPIE_CACHE_DIR
);
378 $rss = new SimplePie();
379 $rss->feed_url($fetch_url);
380 $rss->cache_location(SIMPLEPIE_CACHE_DIR
);
384 $feed = db_escape_string($feed);
388 if (RSS_BACKEND_TYPE
== "simplepie") {
389 $rss_check = $rss->data
;
394 // db_query($link, "BEGIN");
396 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
397 FROM ttrss_feeds WHERE id = '$feed'");
399 $registered_title = db_fetch_result($result, 0, "title");
400 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
401 $orig_site_url = db_fetch_result($result, 0, "site_url");
403 $owner_uid = db_fetch_result($result, 0, "owner_uid");
405 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
406 check_feed_favicon($rss->channel
["link"], $feed, $link);
409 if (!$registered_title ||
$registered_title == "[Unknown]") {
411 if (RSS_BACKEND_TYPE
== "magpie") {
412 $feed_title = db_escape_string($rss->channel
["title"]);
414 $feed_title = $rss->get_feed_title();
417 db_query($link, "UPDATE ttrss_feeds SET
418 title = '$feed_title' WHERE id = '$feed'");
421 if (RSS_BACKEND_TYPE
== "magpie") {
422 $site_url = $rss->channel
["link"];
423 // weird, weird Magpie
424 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
426 $site_url = $rss->get_feed_link();
429 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
430 db_query($link, "UPDATE ttrss_feeds SET
431 site_url = '$site_url' WHERE id = '$feed'");
434 // print "I: " . $rss->channel["image"]["url"];
436 if (RSS_BACKEND_TYPE
== "magpie") {
437 $icon_url = $rss->image
["url"];
439 $icon_url = $rss->get_image_url(); # FIXME
442 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
443 $icon_url = db_escape_string($icon_url);
444 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
450 $result = db_query($link, "SELECT reg_exp,
451 ttrss_filter_types.name AS name,
452 ttrss_filter_actions.name AS action
453 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
455 owner_uid = $owner_uid AND
456 ttrss_filter_types.id = filter_type AND
457 ttrss_filter_actions.id = action_id AND
458 (feed_id IS NULL OR feed_id = '$feed')");
460 while ($line = db_fetch_assoc($result)) {
461 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
463 $filter["reg_exp"] = $line["reg_exp"];
464 $filter["action"] = $line["action"];
466 array_push($filters[$line["name"]], $filter);
469 if (RSS_BACKEND_TYPE
== "magpie") {
470 $iterator = $rss->items
;
472 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
473 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
476 $iterator = $rss->get_items();
479 if (!is_array($iterator)) {
480 /* db_query($link, "UPDATE ttrss_feeds
481 SET last_error = 'Parse error: can\'t find any articles.'
482 WHERE id = '$feed'"); */
486 foreach ($iterator as $item) {
488 if (RSS_BACKEND_TYPE
== "magpie") {
490 $entry_guid = $item["id"];
492 if (!$entry_guid) $entry_guid = $item["guid"];
493 if (!$entry_guid) $entry_guid = $item["link"];
494 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
496 if (!$entry_guid) continue;
498 $entry_timestamp = "";
500 $rss_2_date = $item['pubdate'];
501 $rss_1_date = $item['dc']['date'];
502 $atom_date = $item['issued'];
503 if (!$atom_date) $atom_date = $item['updated'];
505 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
506 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
507 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
509 if ($entry_timestamp == "") {
510 $entry_timestamp = time();
511 $no_orig_date = 'true';
513 $no_orig_date = 'false';
516 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
518 $entry_title = trim(strip_tags($item["title"]));
520 // strange Magpie workaround
521 $entry_link = $item["link_"];
522 if (!$entry_link) $entry_link = $item["link"];
524 if (!$entry_title) continue;
525 # if (!$entry_link) continue;
527 $entry_link = strip_tags($entry_link);
529 $entry_content = $item["content:escaped"];
531 if (!$entry_content) $entry_content = $item["content:encoded"];
532 if (!$entry_content) $entry_content = $item["content"];
533 if (!$entry_content) $entry_content = $item["atom_content"];
534 if (!$entry_content) $entry_content = $item["summary"];
535 if (!$entry_content) $entry_content = $item["description"];
537 // if (!$entry_content) continue;
540 if (is_array($entry_content)) {
541 $entry_content = $entry_content["encoded"];
542 if (!$entry_content) $entry_content = $entry_content["escaped"];
546 // print_r(htmlspecialchars($entry_content));
549 $entry_content_unescaped = $entry_content;
550 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
552 $entry_comments = strip_tags($item["comments"]);
554 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
555 $entry_guid = db_escape_string(strip_tags($entry_guid));
557 $result = db_query($link, "SELECT id FROM ttrss_entries
558 WHERE guid = '$entry_guid'");
560 $entry_content = db_escape_string($entry_content);
561 $entry_title = db_escape_string($entry_title);
562 $entry_link = db_escape_string($entry_link);
563 $entry_comments = db_escape_string($entry_comments);
565 $num_comments = db_escape_string($item["slash"]["comments"]);
567 if (!$num_comments) $num_comments = 0;
572 $entry_content = sanitize_rss($entry_content);
574 db_query($link, "BEGIN");
576 if (db_num_rows($result) == 0) {
578 // base post entry does not exist, create it
580 $result = db_query($link,
581 "INSERT INTO ttrss_entries
597 '$entry_timestamp_fmt',
606 // we keep encountering the entry in feeds, so we need to
607 // update date_entered column so that we don't get horrible
608 // dupes when the entry gets purged and reinserted again e.g.
609 // in the case of SLOW SLOW OMG SLOW updating feeds
611 $base_entry_id = db_fetch_result($result, 0, "id");
613 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
614 WHERE id = '$base_entry_id'");
617 // now it should exist, if not - bad luck then
619 $result = db_query($link, "SELECT
620 id,content_hash,no_orig_date,title,
621 substring(date_entered,1,19) as date_entered,
622 substring(updated,1,19) as updated,
626 WHERE guid = '$entry_guid'");
628 if (db_num_rows($result) == 1) {
630 // this will be used below in update handler
631 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
632 $orig_title = db_fetch_result($result, 0, "title");
633 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
634 $orig_date_entered = strtotime(db_fetch_result($result,
637 $ref_id = db_fetch_result($result, 0, "id");
639 // check for user post link to main table
641 // do we allow duplicate posts with same GUID in different feeds?
642 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
643 $dupcheck_qpart = "AND feed_id = '$feed'";
645 $dupcheck_qpart = "";
648 // error_reporting(0);
650 $filter_name = get_filter_name($entry_title, $entry_content,
651 $entry_link, $filters);
653 if ($filter_name == "filter") {
657 // error_reporting (DEFAULT_ERROR_LEVEL);
659 $result = db_query($link,
660 "SELECT ref_id FROM ttrss_user_entries WHERE
661 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
664 // okay it doesn't exist - create user entry
665 if (db_num_rows($result) == 0) {
667 if ($filter_name != 'catchup') {
669 $last_read_qpart = 'NULL';
672 $last_read_qpart = 'NOW()';
675 if ($filter_name == 'mark') {
681 $result = db_query($link,
682 "INSERT INTO ttrss_user_entries
683 (ref_id, owner_uid, feed_id, unread, last_read, marked)
684 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
685 $last_read_qpart, $marked)");
688 $post_needs_update = false;
690 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
691 ($content_hash != $orig_content_hash)) {
692 $post_needs_update = true;
695 if ($orig_title != $entry_title) {
696 $post_needs_update = true;
699 if ($orig_num_comments != $num_comments) {
700 $post_needs_update = true;
703 // this doesn't seem to be very reliable
705 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
706 // $post_needs_update = true;
709 // if post needs update, update it and mark all user entries
710 // linking to this post as updated
711 if ($post_needs_update) {
713 // print "<!-- post $orig_title needs update : $post_needs_update -->";
715 db_query($link, "UPDATE ttrss_entries
716 SET title = '$entry_title', content = '$entry_content',
717 num_comments = '$num_comments'
718 WHERE id = '$ref_id'");
720 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
721 db_query($link, "UPDATE ttrss_user_entries
722 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
724 db_query($link, "UPDATE ttrss_user_entries
725 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
731 db_query($link, "COMMIT");
734 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
738 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
739 $entry_content_unescaped, $entry_tags);
741 // print "<br>$entry_title : $entry_content_unescaped<br>";
742 // print_r($entry_tags);
745 $entry_tags = $entry_tags[1];
747 if (count($entry_tags) > 0) {
749 db_query($link, "BEGIN");
751 $result = db_query($link, "SELECT id,int_id
752 FROM ttrss_entries,ttrss_user_entries
753 WHERE guid = '$entry_guid'
754 AND feed_id = '$feed' AND ref_id = id
755 AND owner_uid = '$owner_uid'");
757 if (db_num_rows($result) == 1) {
759 $entry_id = db_fetch_result($result, 0, "id");
760 $entry_int_id = db_fetch_result($result, 0, "int_id");
762 foreach ($entry_tags as $tag) {
763 $tag = db_escape_string(strtolower($tag));
765 $tag = str_replace("+", " ", $tag);
766 $tag = str_replace("technorati tag: ", "", $tag);
768 $result = db_query($link, "SELECT id FROM ttrss_tags
769 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
770 owner_uid = '$owner_uid' LIMIT 1");
772 // print db_fetch_result($result, 0, "id");
774 if ($result && db_num_rows($result) == 0) {
776 // print "tagging $entry_id as $tag<br>";
778 db_query($link, "INSERT INTO ttrss_tags
779 (owner_uid,tag_name,post_int_id)
780 VALUES ('$owner_uid','$tag', '$entry_int_id')");
784 db_query($link, "COMMIT");
788 db_query($link, "UPDATE ttrss_feeds
789 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
791 // db_query($link, "COMMIT");
794 $error_msg = db_escape_string(magpie_error());
796 "UPDATE ttrss_feeds SET last_error = '$error_msg',
797 last_updated = NOW() WHERE id = '$feed'");
802 function print_select($id, $default, $values, $attributes = "") {
803 print "<select name=\"$id\" id=\"$id\" $attributes>";
804 foreach ($values as $v) {
810 print "<option$sel>$v</option>";
815 function print_select_hash($id, $default, $values, $attributes = "") {
816 print "<select name=\"$id\" id='$id' $attributes>";
817 foreach (array_keys($values) as $v) {
823 print "<option $sel value=\"$v\">".$values[$v]."</option>";
829 function get_filter_name($title, $content, $link, $filters) {
831 if ($filters["title"]) {
832 foreach ($filters["title"] as $filter) {
833 $reg_exp = $filter["reg_exp"];
834 if (preg_match("/$reg_exp/i", $title)) {
835 return $filter["action"];
840 if ($filters["content"]) {
841 foreach ($filters["content"] as $filter) {
842 $reg_exp = $filter["reg_exp"];
843 if (preg_match("/$reg_exp/i", $content)) {
844 return $filter["action"];
849 if ($filters["both"]) {
850 foreach ($filters["both"] as $filter) {
851 $reg_exp = $filter["reg_exp"];
852 if (preg_match("/$reg_exp/i", $title) ||
853 preg_match("/$reg_exp/i", $content)) {
854 return $filter["action"];
859 if ($filters["link"]) {
860 $reg_exp = $filter["reg_exp"];
861 foreach ($filters["link"] as $filter) {
862 $reg_exp = $filter["reg_exp"];
863 if (preg_match("/$reg_exp/i", $link)) {
864 return $filter["action"];
872 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
873 $rtl_content = false, $last_updated = false, $last_error = false) {
875 if (file_exists($icon_file) && filesize($icon_file) > 0) {
876 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
878 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
882 $rtl_tag = "dir=\"rtl\"";
884 $rtl_tag = "dir=\"ltr\"";
887 $error_notify_msg = "";
890 $link_title = "Error: $last_error ($last_updated)";
891 $error_notify_msg = "(Error)";
892 } else if ($last_updated) {
893 $link_title = "Updated: $last_updated";
896 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
898 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
899 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
903 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
908 $fctr_class = "class=\"invisible\"";
911 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
912 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
914 if (get_pref($link, "EXTENDED_FEEDLIST")) {
915 print "<div class=\"feedExtInfo\">
916 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
923 function getmicrotime() {
924 list($usec, $sec) = explode(" ",microtime());
925 return ((float)$usec +
(float)$sec);
928 function print_radio($id, $default, $values, $attributes = "") {
929 foreach ($values as $v) {
937 $sel .= " value=\"1\"";
939 $sel .= " value=\"0\"";
942 print "<input class=\"noborder\"
943 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
948 function initialize_user_prefs($link, $uid) {
950 $uid = db_escape_string($uid);
952 db_query($link, "BEGIN");
954 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
956 $u_result = db_query($link, "SELECT pref_name
957 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
959 $active_prefs = array();
961 while ($line = db_fetch_assoc($u_result)) {
962 array_push($active_prefs, $line["pref_name"]);
965 while ($line = db_fetch_assoc($result)) {
966 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
967 // print "adding " . $line["pref_name"] . "<br>";
969 db_query($link, "INSERT INTO ttrss_user_prefs
970 (owner_uid,pref_name,value) VALUES
971 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
976 db_query($link, "COMMIT");
980 function lookup_user_id($link, $user) {
982 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
985 if (db_num_rows($result) == 1) {
986 return db_fetch_result($result, 0, "id");
992 function http_authenticate_user($link) {
994 if (!$_SERVER["PHP_AUTH_USER"]) {
996 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
997 header('HTTP/1.0 401 Unauthorized');
1001 $auth_result = authenticate_user($link,
1002 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1004 if (!$auth_result) {
1005 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1006 header('HTTP/1.0 401 Unauthorized');
1014 function authenticate_user($link, $login, $password, $force_auth = false) {
1016 if (!SINGLE_USER_MODE
) {
1018 $pwd_hash = 'SHA1:' . sha1($password);
1020 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1021 $query = "SELECT id,login,access_level
1022 FROM ttrss_users WHERE
1025 $query = "SELECT id,login,access_level
1026 FROM ttrss_users WHERE
1027 login = '$login' AND pwd_hash = '$pwd_hash'";
1030 $result = db_query($link, $query);
1032 if (db_num_rows($result) == 1) {
1033 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1034 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1035 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1037 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1040 $user_theme = get_user_theme_path($link);
1042 $_SESSION["theme"] = $user_theme;
1043 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1045 initialize_user_prefs($link, $_SESSION["uid"]);
1054 $_SESSION["uid"] = 1;
1055 $_SESSION["name"] = "admin";
1057 $user_theme = get_user_theme_path($link);
1059 $_SESSION["theme"] = $user_theme;
1060 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1062 initialize_user_prefs($link, $_SESSION["uid"]);
1068 function make_password($length = 8) {
1071 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1075 while ($i < $length) {
1076 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1078 if (!strstr($password, $char)) {
1086 // this is called after user is created to initialize default feeds, labels
1089 // user preferences are checked on every login, not here
1091 function initialize_user($link, $uid) {
1093 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1094 values ('$uid','unread = true', 'Unread articles')");
1096 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1097 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1099 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1100 values ('$uid', 'Tiny Tiny RSS: New Releases',
1101 'http://tt-rss.spb.ru/releases.rss')");
1105 function logout_user() {
1107 if (isset($_COOKIE[session_name()])) {
1108 setcookie(session_name(), '', time()-42000, '/');
1112 function get_script_urlpath() {
1113 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1116 function get_login_redirect() {
1117 $server = $_SERVER["SERVER_NAME"];
1119 if (ENABLE_LOGIN_SSL
) {
1120 $protocol = "https";
1125 $url_path = get_script_urlpath();
1127 $redirect_uri = "$protocol://$server$url_path/login.php";
1129 return $redirect_uri;
1132 function validate_session($link) {
1133 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1134 if ($_SESSION["ip_address"]) {
1135 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1143 function basic_nosid_redirect_check() {
1144 if (!SINGLE_USER_MODE
) {
1145 if (!$_COOKIE[get_session_cookie_name()]) {
1146 $redirect_uri = get_login_redirect();
1147 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1148 header("Location: $redirect_uri?rt=$return_to");
1154 function login_sequence($link) {
1155 if (!SINGLE_USER_MODE
) {
1157 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1158 $swu = db_escape_string($_REQUEST["swu"]);
1160 $_SESSION["prefs_cache"] = false;
1161 return authenticate_user($link, $swu, null, true);
1165 if (!validate_session($link)) {
1167 $redirect_uri = get_login_redirect();
1168 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1169 header("Location: $redirect_uri?rt=$return_to");
1173 if (!USE_HTTP_AUTH
) {
1174 if (!$_SESSION["uid"]) {
1175 $redirect_uri = get_login_redirect();
1176 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1177 header("Location: $redirect_uri?rt=$return_to");
1181 if (!$_SESSION["uid"]) {
1182 if (!$_SERVER["PHP_AUTH_USER"]) {
1184 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1185 header('HTTP/1.0 401 Unauthorized');
1189 $auth_result = authenticate_user($link,
1190 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1192 if (!$auth_result) {
1193 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1194 header('HTTP/1.0 401 Unauthorized');
1201 return authenticate_user($link, "admin", null);
1205 function truncate_string($str, $max_len) {
1206 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1207 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1213 function get_user_theme_path($link) {
1214 $result = db_query($link, "SELECT theme_path
1216 ttrss_themes,ttrss_users
1217 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1218 if (db_num_rows($result) != 0) {
1219 return db_fetch_result($result, 0, "theme_path");
1225 function smart_date_time($timestamp) {
1226 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1227 return date("G:i", $timestamp);
1228 } else if (date("Y", $timestamp) == date("Y")) {
1229 return date("M d, G:i", $timestamp);
1231 return date("Y/m/d G:i", $timestamp);
1235 function smart_date($timestamp) {
1236 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1238 } else if (date("Y", $timestamp) == date("Y")) {
1239 return date("D m", $timestamp);
1241 return date("Y/m/d", $timestamp);
1245 function sql_bool_to_string($s) {
1246 if ($s == "t" ||
$s == "1") {
1253 function sql_bool_to_bool($s) {
1254 if ($s == "t" ||
$s == "1") {
1262 function toggleEvenOdd($a) {
1269 function sanity_check($link) {
1274 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1275 $schema_version = db_fetch_result($result, 0, "schema_version");
1277 if ($schema_version != SCHEMA_VERSION
) {
1281 if (DB_TYPE
== "mysql") {
1282 $result = db_query($link, "SELECT true", false);
1283 if (db_num_rows($result) != 1) {
1288 error_reporting (DEFAULT_ERROR_LEVEL
);
1290 if ($error_code != 0) {
1291 print_error_xml($error_code);
1298 function file_is_locked($filename) {
1300 $fp = fopen($filename, "r");
1301 error_reporting(DEFAULT_ERROR_LEVEL
);
1303 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1304 flock($fp, LOCK_UN
);
1314 function make_lockfile($filename) {
1315 $fp = fopen($filename, "w");
1317 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1324 function sql_random_function() {
1325 if (DB_TYPE
== "mysql") {
1332 function catchup_feed($link, $feed, $cat_view) {
1334 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1339 $cat_qpart = "cat_id = '$feed'";
1341 $cat_qpart = "cat_id IS NULL";
1344 $tmp_result = db_query($link, "SELECT id
1345 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1348 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1350 $tmp_feed = $tmp_line["id"];
1352 db_query($link, "UPDATE ttrss_user_entries
1353 SET unread = false,last_read = NOW()
1354 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1357 } else if ($feed > 0) {
1359 $tmp_result = db_query($link, "SELECT id
1360 FROM ttrss_feeds WHERE parent_feed = '$feed'
1361 ORDER BY cat_id,title");
1363 $parent_ids = array();
1365 if (db_num_rows($tmp_result) > 0) {
1366 while ($p = db_fetch_assoc($tmp_result)) {
1367 array_push($parent_ids, "feed_id = " . $p["id"]);
1370 $children_qpart = implode(" OR ", $parent_ids);
1372 db_query($link, "UPDATE ttrss_user_entries
1373 SET unread = false,last_read = NOW()
1374 WHERE (feed_id = '$feed' OR $children_qpart)
1375 AND owner_uid = " . $_SESSION["uid"]);
1378 db_query($link, "UPDATE ttrss_user_entries
1379 SET unread = false,last_read = NOW()
1380 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1383 } else if ($feed < 0 && $feed > -10) { // special, like starred
1386 db_query($link, "UPDATE ttrss_user_entries
1387 SET unread = false,last_read = NOW()
1388 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1391 } else if ($feed < -10) { // label
1393 // TODO make this more efficient
1395 $label_id = -$feed - 11;
1397 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1398 WHERE id = '$label_id'");
1401 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1403 db_query($link, "BEGIN");
1405 $tmp2_result = db_query($link,
1409 ttrss_user_entries,ttrss_entries,ttrss_feeds
1411 ref_id = ttrss_entries.id AND
1412 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1414 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1416 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1417 db_query($link, "UPDATE
1420 unread = false, last_read = NOW()
1422 int_id = " . $tmp_line["int_id"]);
1425 db_query($link, "COMMIT");
1427 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1428 SET unread = false,last_read = NOW()
1431 AND owner_uid = ".$_SESSION["uid"]); */
1435 db_query($link, "BEGIN");
1437 $tag_name = db_escape_string($feed);
1439 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1440 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1442 while ($line = db_fetch_assoc($result)) {
1443 db_query($link, "UPDATE ttrss_user_entries SET
1444 unread = false, last_read = NOW()
1445 WHERE int_id = " . $line["post_int_id"]);
1447 db_query($link, "COMMIT");
1451 function update_generic_feed($link, $feed, $cat_view) {
1455 $cat_qpart = "cat_id = '$feed'";
1457 $cat_qpart = "cat_id IS NULL";
1460 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1461 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1463 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1464 $feed_url = $tmp_line["feed_url"];
1465 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1469 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1470 WHERE id = '$feed'");
1471 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1472 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1476 function getAllCounters($link) {
1477 getLabelCounters($link);
1478 getFeedCounters($link);
1479 getTagCounters($link);
1480 getGlobalCounters($link);
1481 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1482 getCategoryCounters($link);
1486 function getCategoryCounters($link) {
1487 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1488 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1489 AND unread = true)) AS unread FROM ttrss_feeds
1491 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1493 while ($line = db_fetch_assoc($result)) {
1494 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1495 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1496 $line["unread"]."\"/>";
1500 function getCategoryUnread($link, $cat) {
1503 $cat_query = "cat_id = '$cat'";
1505 $cat_query = "cat_id IS NULL";
1508 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1510 AND owner_uid = " . $_SESSION["uid"]);
1512 $cat_feeds = array();
1513 while ($line = db_fetch_assoc($result)) {
1514 array_push($cat_feeds, "feed_id = " . $line["id"]);
1517 if (count($cat_feeds) == 0) return 0;
1519 $match_part = implode(" OR ", $cat_feeds);
1521 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1522 FROM ttrss_user_entries
1523 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1527 # this needs to be rewritten
1528 while ($line = db_fetch_assoc($result)) {
1529 $unread +
= $line["unread"];
1536 function getFeedUnread($link, $feed, $is_cat = false) {
1537 $n_feed = sprintf("%d", $feed);
1540 return getCategoryUnread($link, $n_feed);
1541 } else if ($n_feed == -1) {
1542 $match_part = "marked = true";
1543 } else if ($n_feed > 0) {
1545 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1547 AND owner_uid = " . $_SESSION["uid"]);
1549 if (db_num_rows($result) > 0) {
1551 $linked_feeds = array();
1552 while ($line = db_fetch_assoc($result)) {
1553 array_push($linked_feeds, "feed_id = " . $line["id"]);
1556 $match_part = implode(" OR ", $linked_feeds);
1558 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1559 FROM ttrss_user_entries
1560 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1564 # this needs to be rewritten
1565 while ($line = db_fetch_assoc($result)) {
1566 $unread +
= $line["unread"];
1572 $match_part = "feed_id = '$n_feed'";
1574 } else if ($feed < -10) {
1576 $label_id = -$feed - 11;
1578 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1579 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1581 $match_part = db_fetch_result($result, 0, "sql_exp");
1586 $result = db_query($link, "SELECT count(int_id) AS unread
1587 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1588 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1589 ttrss_user_entries.ref_id = ttrss_entries.id AND
1590 ttrss_feeds.hidden = false AND
1591 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1595 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1596 FROM ttrss_tags,ttrss_user_entries
1597 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1598 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1601 $unread = db_fetch_result($result, 0, "unread");
1606 /* FIXME this needs reworking */
1608 function getGlobalUnread($link, $user_id = false) {
1611 $user_id = $_SESSION["uid"];
1614 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1615 WHERE unread = true AND
1616 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1617 ttrss_user_entries.ref_id = ttrss_entries.id AND
1619 ttrss_user_entries.owner_uid = '$user_id'");
1620 $c_id = db_fetch_result($result, 0, "c_id");
1624 function getGlobalCounters($link, $global_unread = -1) {
1625 if ($global_unread == -1) {
1626 $global_unread = getGlobalUnread($link);
1628 print "<counter type=\"global\" id='global-unread'
1629 counter='$global_unread'/>";
1631 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1632 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1634 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1636 print "<counter type=\"global\" id='subscribed-feeds'
1637 counter='$subscribed_feeds'/>";
1641 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1644 if (!$_SESSION["tctr_last_value"]) {
1645 $_SESSION["tctr_last_value"] = array();
1649 $old_counters = $_SESSION["tctr_last_value"];
1651 $tctrs_modified = false;
1653 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1654 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1655 ttrss_user_entries.ref_id = ttrss_entries.id AND
1656 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1657 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1659 select tag_name,0 as count FROM ttrss_tags
1660 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1662 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1663 FROM ttrss_user_entries WHERE int_id = post_int_id
1664 AND unread = true)) AS count FROM ttrss_tags
1665 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1669 while ($line = db_fetch_assoc($result)) {
1670 $tags[$line["tag_name"]] +
= $line["count"];
1673 foreach (array_keys($tags) as $tag) {
1674 $unread = $tags[$tag];
1676 $tag = htmlspecialchars($tag);
1678 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1679 $old_counters[$tag] = $unread;
1680 $tctrs_modified = true;
1681 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1686 if ($smart_mode && $tctrs_modified) {
1687 $_SESSION["tctr_last_value"] = $old_counters;
1692 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1695 if (!$_SESSION["lctr_last_value"]) {
1696 $_SESSION["lctr_last_value"] = array();
1702 $old_counters = $_SESSION["lctr_last_value"];
1703 $lctrs_modified = false;
1705 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1706 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1707 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1708 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1710 $count = db_fetch_result($result, 0, "count");
1713 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1715 $ret_arr["-1"]["counter"] = $count;
1716 $ret_arr["-1"]["description"] = "Starred";
1719 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1720 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1722 while ($line = db_fetch_assoc($result)) {
1724 $id = -$line["id"] - 11;
1726 $label_name = $line["description"];
1728 error_reporting (0);
1730 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1731 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1732 ttrss_feeds.hidden = false AND
1733 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1734 ttrss_user_entries.ref_id = ttrss_entries.id AND
1735 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1737 $count = db_fetch_result($tmp_result, 0, "count");
1739 if (!$smart_mode ||
$old_counters[$id] != $count) {
1740 $old_counters[$id] = $count;
1741 $lctrs_modified = true;
1743 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1745 $ret_arr[$id]["counter"] = $count;
1746 $ret_arr[$id]["description"] = $label_name;
1750 error_reporting (DEFAULT_ERROR_LEVEL
);
1753 if ($smart_mode && $lctrs_modified) {
1754 $_SESSION["lctr_last_value"] = $old_counters;
1760 /* function getFeedCounter($link, $id) {
1762 $result = db_query($link, "SELECT
1763 count(id) as count,last_error
1764 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1765 WHERE feed_id = '$id' AND unread = true
1766 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1767 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1769 $count = db_fetch_result($result, 0, "count");
1770 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1772 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1775 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1778 if (!$_SESSION["fctr_last_value"]) {
1779 $_SESSION["fctr_last_value"] = array();
1783 $old_counters = $_SESSION["fctr_last_value"];
1785 $result = db_query($link, "SELECT id,last_error,parent_feed,
1786 SUBSTRING(last_updated,1,19) AS last_updated,
1788 FROM ttrss_entries,ttrss_user_entries
1789 WHERE feed_id = ttrss_feeds.id AND
1790 ttrss_user_entries.ref_id = ttrss_entries.id
1791 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1792 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1793 AND parent_feed IS NULL");
1795 $fctrs_modified = false;
1797 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1799 while ($line = db_fetch_assoc($result)) {
1802 $count = $line["count"];
1803 $last_error = htmlspecialchars($line["last_error"]);
1805 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1806 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1808 $last_updated = date($short_date, strtotime($line["last_updated"]));
1811 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1813 $tmp_result = db_query($link,
1814 "SELECT id,COUNT(unread) AS unread
1815 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1816 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1817 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1819 if (db_num_rows($tmp_result) > 0) {
1820 while ($l = db_fetch_assoc($tmp_result)) {
1821 $count +
= $l["unread"];
1825 if (!$smart_mode ||
$old_counters[$id] != $count) {
1826 $old_counters[$id] = $count;
1827 $fctrs_modified = true;
1830 $error_part = "error=\"$last_error\"";
1836 $has_img_part = "hi=\"$has_img\"";
1841 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1845 if ($smart_mode && $fctrs_modified) {
1846 $_SESSION["fctr_last_value"] = $old_counters;
1850 function get_script_dt_add() {
1851 if (strpos(VERSION
, "99") === false) {
1858 function get_pgsql_version($link) {
1859 $result = db_query($link, "SELECT version() AS version");
1860 $version = split(" ", db_fetch_result($result, 0, "version"));
1864 function print_error_xml($code, $add_msg = "") {
1867 $error_msg = $ERRORS[$code];
1870 $error_msg = "$error_msg; $add_msg";
1873 print "<rpc-reply>";
1874 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1875 print "</rpc-reply>";
1878 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1880 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
1882 if ($feed_link == "") return;
1884 if ($cat_id == "0" ||
!$cat_id) {
1885 $cat_qpart = "NULL";
1887 $cat_qpart = "'$cat_id'";
1890 $result = db_query($link,
1891 "SELECT id FROM ttrss_feeds
1892 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1894 if (db_num_rows($result) == 0) {
1896 $result = db_query($link,
1897 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1898 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1899 '[Unknown]', $cat_qpart)");
1901 $result = db_query($link,
1902 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1903 AND owner_uid = " . $_SESSION["uid"]);
1905 $feed_id = db_fetch_result($result, 0, "id");
1908 update_rss_feed($link, $feed_link, $feed_id, true);
1917 function print_feed_select($link, $id, $default_id = "",
1918 $attributes = "", $include_all_feeds = true) {
1920 print "<select id=\"$id\" name=\"$id\" $attributes>";
1921 if ($include_all_feeds) {
1922 print "<option value=\"0\">All feeds</option>";
1925 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1926 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1928 if (db_num_rows($result) > 0 && $include_all_feeds) {
1929 print "<option disabled>--------</option>";
1932 while ($line = db_fetch_assoc($result)) {
1933 if ($line["id"] == $default_id) {
1934 $is_selected = "selected";
1938 printf("<option $is_selected value='%d'>%s</option>",
1939 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1945 function print_feed_cat_select($link, $id, $default_id = "",
1946 $attributes = "", $include_all_cats = true) {
1948 print "<select id=\"$id\" name=\"$id\" $attributes>";
1950 if ($include_all_cats) {
1951 print "<option value=\"0\">Uncategorized</option>";
1954 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1955 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1957 if (db_num_rows($result) > 0 && $include_all_cats) {
1958 print "<option disabled>--------</option>";
1961 while ($line = db_fetch_assoc($result)) {
1962 if ($line["id"] == $default_id) {
1963 $is_selected = "selected";
1967 printf("<option $is_selected value='%d'>%s</option>",
1968 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1974 function checkbox_to_sql_bool($val) {
1975 return ($val == "on") ?
"true" : "false";
1978 function getFeedCatTitle($link, $id) {
1981 } else if ($id < -10) {
1983 } else if ($id > 0) {
1984 $result = db_query($link, "SELECT ttrss_feed_categories.title
1985 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1986 cat_id = ttrss_feed_categories.id");
1987 if (db_num_rows($result) == 1) {
1988 return db_fetch_result($result, 0, "title");
1990 return "Uncategorized";
1993 return "getFeedCatTitle($id) failed";
1998 function getFeedTitle($link, $id) {
2000 return "Starred articles";
2001 } else if ($id < -10) {
2002 $label_id = -10 - $id;
2003 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2004 if (db_num_rows($result) == 1) {
2005 return db_fetch_result($result, 0, "description");
2007 return "Unknown label ($label_id)";
2010 } else if ($id > 0) {
2011 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2012 if (db_num_rows($result) == 1) {
2013 return db_fetch_result($result, 0, "title");
2015 return "Unknown feed ($id)";
2018 return "getFeedTitle($id) failed";
2023 function get_session_cookie_name() {
2024 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2027 function print_init_params($link) {
2028 print "<init-params>";
2029 if ($_SESSION["stored-params"]) {
2030 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2032 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2033 print "<param key=\"$key\" value=\"$value\"/>";
2038 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2039 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2040 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2042 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2043 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2045 print "<param key=\"hide_read_feeds\" value=\"" .
2046 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2048 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2049 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2051 print "<param key=\"confirm_feed_catchup\" value=\"" .
2052 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2054 print "</init-params>";
2057 function print_runtime_info($link) {
2058 print "<runtime-info>";
2059 if (ENABLE_UPDATE_DAEMON
) {
2060 print "<param key=\"daemon_is_running\" value=\"".
2061 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2063 print "</runtime-info>";
2066 function getSearchSql($search, $match_on) {
2068 $search_query_part = "";
2070 $keywords = split(" ", $search);
2071 $query_keywords = array();
2073 if ($match_on == "both") {
2075 foreach ($keywords as $k) {
2076 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2077 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2080 $search_query_part = implode("AND", $query_keywords) . " AND ";
2082 } else if ($match_on == "title") {
2084 foreach ($keywords as $k) {
2085 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2088 $search_query_part = implode("AND", $query_keywords) . " AND ";
2090 } else if ($match_on == "content") {
2092 foreach ($keywords as $k) {
2093 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2097 $search_query_part = implode("AND", $query_keywords);
2099 return $search_query_part;
2102 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2105 $search_query_part = getSearchSql($search, $match_on);
2106 $search_query_part .= " AND ";
2109 $search_query_part = "";
2112 $view_query_part = "";
2114 if ($view_mode == "adaptive") {
2116 $view_query_part = " ";
2117 } else if ($feed != -1) {
2118 $unread = getFeedUnread($link, $feed, $cat_view);
2120 $view_query_part = " unread = true AND ";
2125 if ($view_mode == "marked") {
2126 $view_query_part = " marked = true AND ";
2129 if ($view_mode == "unread") {
2130 $view_query_part = " unread = true AND ";
2134 $limit_query_part = "LIMIT " . $limit;
2137 $vfeed_query_part = "";
2139 // override query strategy and enable feed display when searching globally
2140 if ($search && $search_mode == "all_feeds") {
2141 $query_strategy_part = "ttrss_entries.id > 0";
2142 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2143 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2144 $query_strategy_part = "ttrss_entries.id > 0";
2145 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2146 id = feed_id) as feed_title,";
2147 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2149 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2151 $tmp_result = false;
2154 $tmp_result = db_query($link, "SELECT id
2155 FROM ttrss_feeds WHERE cat_id = '$feed'");
2157 $tmp_result = db_query($link, "SELECT id
2158 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2159 WHERE id = '$feed') AND id != '$feed'");
2162 $cat_siblings = array();
2164 if (db_num_rows($tmp_result) > 0) {
2165 while ($p = db_fetch_assoc($tmp_result)) {
2166 array_push($cat_siblings, "feed_id = " . $p["id"]);
2169 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2170 $feed, implode(" OR ", $cat_siblings));
2173 $query_strategy_part = "ttrss_entries.id > 0";
2176 } else if ($feed >= 0) {
2181 $query_strategy_part = "cat_id = '$feed'";
2183 $query_strategy_part = "cat_id IS NULL";
2186 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2189 $tmp_result = db_query($link, "SELECT id
2190 FROM ttrss_feeds WHERE parent_feed = '$feed'
2191 ORDER BY cat_id,title");
2193 $parent_ids = array();
2195 if (db_num_rows($tmp_result) > 0) {
2196 while ($p = db_fetch_assoc($tmp_result)) {
2197 array_push($parent_ids, "feed_id = " . $p["id"]);
2200 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2201 $feed, implode(" OR ", $parent_ids));
2203 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2205 $query_strategy_part = "feed_id = '$feed'";
2208 } else if ($feed == -1) { // starred virtual feed
2209 $query_strategy_part = "marked = true";
2210 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2211 } else if ($feed <= -10) { // labels
2212 $label_id = -$feed - 11;
2214 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2215 WHERE id = '$label_id'");
2217 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2219 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2221 $query_strategy_part = "id > 0"; // dumb
2224 if (get_pref($link, 'REVERSE_HEADLINES')) {
2225 $order_by = "updated";
2227 $order_by = "updated DESC";
2230 if ($override_order) {
2231 $order_by = $override_order;
2236 if ($search && $search_mode == "all_feeds") {
2237 $feed_title = "Global search results ($search)";
2238 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2239 $feed_title = "Tag search results ($search, $feed)";
2240 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2241 $feed_title = $feed;
2242 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2247 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2248 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2249 $feed_title = db_fetch_result($result, 0, "title");
2251 $feed_title = "Uncategorized";
2255 $feed_title = "Category search results ($search, $feed_title)";
2260 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2261 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2263 $feed_title = db_fetch_result($result, 0, "title");
2264 $feed_site_url = db_fetch_result($result, 0, "site_url");
2265 $last_error = db_fetch_result($result, 0, "last_error");
2268 $feed_title = "Feed search results ($search, $feed_title)";
2272 } else if ($feed == -1) {
2273 $feed_title = "Starred articles";
2274 } else if ($feed < -10) {
2275 $label_id = -$feed - 11;
2276 $result = db_query($link, "SELECT description FROM ttrss_labels
2277 WHERE id = '$label_id'");
2278 $feed_title = db_fetch_result($result, 0, "description");
2281 $feed_title = "Label search results ($search, $feed_title)";
2287 $feed_title = db_unescape_string($feed_title);
2289 if ($feed < -10) error_reporting (0);
2291 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2294 $feed_kind = "Feeds";
2296 $feed_kind = "Labels";
2299 $content_query_part = "content as content_preview,";
2303 ttrss_entries.id,ttrss_entries.title,
2304 SUBSTRING(updated,1,16) as updated,
2305 unread,feed_id,marked,link,last_read,
2306 SUBSTRING(last_read,1,19) as last_read_noms,
2309 SUBSTRING(updated,1,19) as updated_noms
2311 ttrss_entries,ttrss_user_entries,ttrss_feeds
2313 ttrss_feeds.hidden = false AND
2314 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2315 ttrss_user_entries.ref_id = ttrss_entries.id AND
2316 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2319 $query_strategy_part ORDER BY $order_by
2322 $result = db_query($link, $query);
2324 if ($_GET["debug"]) print $query;
2329 $feed_kind = "Tags";
2331 $result = db_query($link, "SELECT
2333 ttrss_entries.id as id,title,
2334 SUBSTRING(updated,1,16) as updated,
2336 marked,link,last_read,
2337 SUBSTRING(last_read,1,19) as last_read_noms,
2340 SUBSTRING(updated,1,19) as updated_noms
2342 ttrss_entries,ttrss_user_entries,ttrss_tags
2344 ref_id = ttrss_entries.id AND
2345 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2346 post_int_id = int_id AND tag_name = '$feed' AND
2349 $query_strategy_part ORDER BY $order_by
2350 $limit_query_part");
2353 return array($result, $feed_title, $feed_site_url, $last_error);
2357 function generate_syndicated_feed($link, $feed, $is_cat,
2358 $search, $search_mode, $match_on) {
2360 $qfh_ret = queryFeedHeadlines($link, $feed,
2361 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2363 $result = $qfh_ret[0];
2364 $feed_title = htmlspecialchars($qfh_ret[1]);
2365 $feed_site_url = $qfh_ret[2];
2366 $last_error = $qfh_ret[3];
2368 print "<rss version=\"2.0\">
2370 <title>$feed_title</title>
2371 <link>$feed_site_url</link>
2372 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2374 while ($line = db_fetch_assoc($result)) {
2376 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2377 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2379 $rfc822_date = date('r', strtotime($line["updated"]));
2381 print "<pubDate>$rfc822_date</pubDate>";
2384 htmlspecialchars($line["title"]) . "</title>";
2386 print "<description>" .
2387 htmlspecialchars($line["content_preview"]) . "</description>";
2392 print "</channel></rss>";
2396 function getCategoryTitle($link, $cat_id) {
2398 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2401 if (db_num_rows($result) == 1) {
2402 return db_fetch_result($result, 0, "title");
2404 return "Uncategorized";
2408 function sanitize_rss($str) {
2411 $res = preg_replace('/<script.*?>/i',
2412 "<p class=\"scriptWarn\">Disabled script: ", $res);
2414 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2416 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2418 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2419 "<p class=\"objectWarn\">(Disabled html object
2420 - flash or other embedded content)</p>", $res); */
2425 function send_headlines_digests($link, $limit = 100) {
2427 if (!DIGEST_ENABLE
) return false;
2429 $user_limit = DIGEST_EMAIL_LIMIT
;
2432 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2434 if (DB_TYPE
== "pgsql") {
2435 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2436 } else if (DB_TYPE
== "mysql") {
2437 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2440 $result = db_query($link, "SELECT id,email FROM ttrss_users
2441 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2443 while ($line = db_fetch_assoc($result)) {
2444 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2445 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2447 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2448 $digest = $tuple[0];
2449 $headlines_count = $tuple[1];
2451 if ($headlines_count > 0) {
2452 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2453 "[tt-rss] New headlines for last 24 hours", $digest,
2454 "From: " . MAIL_FROM
. "\n".
2455 "Content-Type: text/plain; charset=\"utf-8\"\n".
2456 "Content-Transfer-Encoding: 8bit\n");
2458 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2459 WHERE id = " . $line["id"]);
2461 print "No headlines\n";
2466 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2470 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2471 $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n";
2472 $tmp .= "=======================================================\n\n";
2474 if (DB_TYPE
== "pgsql") {
2475 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2476 } else if (DB_TYPE
== "mysql") {
2477 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2480 $result = db_query($link, "SELECT ttrss_entries.title,
2481 ttrss_feeds.title AS feed_title,
2484 SUBSTRING(last_updated,1,19) AS last_updated
2486 ttrss_user_entries,ttrss_entries,ttrss_feeds
2488 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2489 AND include_in_digest = true
2491 AND ttrss_user_entries.owner_uid = $user_id
2492 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2495 $cur_feed_title = "";
2497 $headlines_count = db_num_rows($result);
2499 while ($line = db_fetch_assoc($result)) {
2500 $updated = smart_date_time(strtotime($line["last_updated"]));
2501 $feed_title = $line["feed_title"];
2503 if ($cur_feed_title != $feed_title) {
2504 $cur_feed_title = $feed_title;
2506 $tmp .= "$feed_title\n\n";
2509 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2510 $tmp .= " " . trim($line["link"]) . "\n";
2515 $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " .
2516 DIGEST_HOSTNAME
. "\n".
2517 "To unsubscribe, visit your configuration options or contact instance owner.\n";
2520 return array($tmp, $headlines_count);
2523 function check_for_update($link) {
2524 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2526 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2531 $rss = fetch_rss($releases_feed);
2532 error_reporting (DEFAULT_ERROR_LEVEL
);
2536 $items = $rss->items
;
2538 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2539 if (!$items ||
!is_array($items)) $items = $rss;
2541 if (!is_array($items) ||
count($items) == 0) {
2545 $latest_item = $items[0];
2547 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2549 $release_url = sanitize_rss($latest_item["link"]);
2550 $content = sanitize_rss($latest_item["description"]);
2552 if (version_compare(VERSION
, $latest_version) == -1) {
2553 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2554 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2555 <div id=\"milestoneDetails\">$content</div></div>";
2560 function markArticlesById($link, $ids, $cmode) {
2564 foreach ($ids as $id) {
2565 array_push($tmp_ids, "ref_id = '$id'");
2568 $ids_qpart = join(" OR ", $tmp_ids);
2571 db_query($link, "UPDATE ttrss_user_entries SET
2572 marked = false,last_read = NOW()
2573 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2574 } else if ($cmode == 1) {
2575 db_query($link, "UPDATE ttrss_user_entries SET
2577 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2579 db_query($link, "UPDATE ttrss_user_entries SET
2580 marked = NOT marked,last_read = NOW()
2581 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2585 function catchupArticlesById($link, $ids, $cmode) {
2589 foreach ($ids as $id) {
2590 array_push($tmp_ids, "ref_id = '$id'");
2593 $ids_qpart = join(" OR ", $tmp_ids);
2596 db_query($link, "UPDATE ttrss_user_entries SET
2597 unread = false,last_read = NOW()
2598 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2599 } else if ($cmode == 1) {
2600 db_query($link, "UPDATE ttrss_user_entries SET
2602 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2604 db_query($link, "UPDATE ttrss_user_entries SET
2605 unread = NOT unread,last_read = NOW()
2606 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2610 function escape_for_form($s) {
2611 return htmlspecialchars(db_unescape_string($s));
2614 function make_guid_from_title($title) {
2615 return preg_replace("/[ \"\',.:;]/", "-",
2616 mb_strtolower(strip_tags($title)));
2619 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2620 $bottom = false, $rtl_content = false, $feed_id = 0,
2621 $is_cat = false, $search = false, $match_on = false,
2622 $search_mode = false) {
2625 $class = "headlinesSubToolbar";
2626 $tid = "headlineActionsTop";
2628 $class = "headlinesSubToolbar";
2629 $tid = "headlineActionsBottom";
2632 print "<table class=\"$class\" id=\"$tid\"
2633 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2641 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2643 print "<td class=\"headlineActions$rtl_cpart\">
2645 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
2646 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
2647 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
2649 Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>,
2650 <a href=\"javascript:selectionToggleMarked()\">Starred</a>
2653 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2654 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2657 if ($search && $feed_id > 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2658 print "<td class=\"headlineActions$rtl_cpart\">
2659 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2660 '$match_on', '$feed_id', '$is_cat');\">
2661 Convert this search to label</a></td>";
2666 print "<td class=\"headlineActions$rtl_cpart\">
2668 <a href=\"javascript:cdmSelectArticles('all')\">All</a>,
2669 <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
2670 <a href=\"javascript:cdmSelectArticles('none')\">None</a>
2672 Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>,
2673 <a href=\"javascript:selectionToggleMarked(true)\">Starred</a>";
2679 print "<td class=\"headlineTitle$rtl_cpart\">";
2681 if ($feed_site_url) {
2683 $target = "target=\"_blank\"";
2685 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2691 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2697 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2698 <img class=\"noborder\"
2699 alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\">
2704 print "</tr></table>";
2708 function outputFeedList($link, $tags = false) {
2710 print "<ul class=\"feedList\" id=\"feedList\">\n";
2712 $owner_uid = $_SESSION["uid"];
2718 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2719 print "<li class=\"feedCat\">Special</li>";
2720 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2723 $num_starred = getFeedUnread($link, -1);
2727 if ($num_starred > 0) $class .= "Unread";
2729 printFeedEntry(-1, $class, "Starred articles", $num_starred,
2730 "images/mark_set.png", $link);
2732 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2736 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
2738 $result = db_query($link, "SELECT id,sql_exp,description FROM
2739 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2741 if (db_num_rows($result) > 0) {
2742 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2743 print "<li class=\"feedCat\">Labels</li>";
2744 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2746 print "<li><hr></li>";
2750 while ($line = db_fetch_assoc($result)) {
2752 error_reporting (0);
2754 $label_id = -$line['id'] - 11;
2755 $count = getFeedUnread($link, $label_id);
2763 error_reporting (DEFAULT_ERROR_LEVEL
);
2765 printFeedEntry($label_id,
2766 $class, db_unescape_string($line["description"]),
2767 $count, "images/label.png", $link);
2771 if (db_num_rows($result) > 0) {
2772 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2779 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2780 print "<li><hr></li>";
2783 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2784 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2785 $order_by_qpart = "category,unread DESC,title";
2787 $order_by_qpart = "category,title";
2790 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2791 $order_by_qpart = "unread DESC,title";
2793 $order_by_qpart = "title";
2797 $result = db_query($link, "SELECT ttrss_feeds.*,
2798 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2799 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2800 WHERE feed_id = ttrss_feeds.id AND unread = true
2801 AND ttrss_user_entries.ref_id = ttrss_entries.id
2802 AND owner_uid = '$owner_uid') as unread,
2804 ttrss_feed_categories.title AS category,
2805 ttrss_feed_categories.collapsed
2806 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2807 ON (ttrss_feed_categories.id = cat_id)
2809 ttrss_feeds.hidden = false AND
2810 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2811 ORDER BY $order_by_qpart");
2813 $actid = $_GET["actid"];
2823 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2825 while ($line = db_fetch_assoc($result)) {
2827 $feed = db_unescape_string($line["title"]);
2828 $feed_id = $line["id"];
2830 $subop = $_GET["subop"];
2832 $unread = $line["unread"];
2834 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2835 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2837 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2840 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2843 $rtl_tag = "dir=\"RTL\"";
2848 $tmp_result = db_query($link,
2849 "SELECT id,COUNT(unread) AS unread
2850 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2851 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2852 WHERE parent_feed = '$feed_id' AND unread = true
2853 GROUP BY ttrss_feeds.id");
2855 if (db_num_rows($tmp_result) > 0) {
2856 while ($l = db_fetch_assoc($tmp_result)) {
2857 $unread +
= $l["unread"];
2861 $cat_id = $line["cat_id"];
2863 $tmp_category = $line["category"];
2865 if (!$tmp_category) {
2866 $tmp_category = "Uncategorized";
2869 // $class = ($lnum % 2) ? "even" : "odd";
2871 if ($line["last_error"]) {
2877 if ($unread > 0) $class .= "Unread";
2879 if ($actid == $feed_id) {
2880 $class .= "Selected";
2883 $total_unread +
= $unread;
2885 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
2891 $category = $tmp_category;
2893 $collapsed = $line["collapsed"];
2895 // workaround for NULL category
2896 if ($category == "Uncategorized") {
2897 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
2902 if ($collapsed == "t" ||
$collapsed == "1") {
2903 $holder_class = "invisible";
2910 $cat_id = sprintf("%d", $cat_id);
2912 $cat_unread = getCategoryUnread($link, $cat_id);
2914 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
2915 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
2916 <a href=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
2917 <span id=\"FCATCTR-$cat_id\"
2918 class=\"$catctr_class\">($cat_unread unread)$ellipsis</span>
2921 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
2922 // -> keyboard navigation, etc.
2923 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
2926 printFeedEntry($feed_id, $class, $feed, $unread,
2927 "icons/$feed_id.ico", $link, $rtl_content,
2928 $last_updated, $line["last_error"]);
2933 if (db_num_rows($result) == 0) {
2934 print "<li>No feeds to display.</li>";
2941 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2942 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2943 post_int_id = ttrss_user_entries.int_id AND
2944 unread = true AND ref_id = ttrss_entries.id
2945 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
2947 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
2948 ORDER BY tag_name"); */
2950 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2951 print "<li class=\"feedCat\">Tags</li>";
2952 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2955 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2956 FROM ttrss_user_entries WHERE int_id = post_int_id
2957 AND unread = true)) AS count FROM ttrss_tags
2958 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
2962 while ($line = db_fetch_assoc($result)) {
2963 $tags[$line["tag_name"]] +
= $line["count"];
2966 foreach (array_keys($tags) as $tag) {
2968 $unread = $tags[$tag];
2976 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
2980 if (db_num_rows($result) == 0) {
2981 print "<li>No tags to display.</li>";
2984 if (get_pref($link, 'ENABLE_FEED_CATS')) {