]>
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';
11 if (ENABLE_TRANSLATIONS
== true) {
12 require_once "accept-to-gettext.php";
13 require_once "gettext/gettext.inc";
15 function startup_gettext() {
17 # Get locale from Accept-Language header
18 $lang = al2gt(array("en_US", "ru_RU"), "text/html");
21 _setlocale(LC_MESSAGES
, $lang);
22 _bindtextdomain("messages", "locale");
23 _textdomain("messages");
24 _bind_textdomain_codeset("messages", "UTF-8");
34 function startup_gettext() {
40 require_once 'db-prefs.php';
41 require_once 'compat.php';
42 require_once 'errors.php';
43 require_once 'version.php';
45 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
46 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
48 require_once "magpierss/rss_fetch.inc";
49 require_once 'magpierss/rss_utils.inc';
51 include_once "tw/tw-config.php";
52 include_once "tw/tw.php";
53 include_once TW_SETUP
. "paranoya.php";
55 $tw_parser = new twParser();
57 function _debug($msg) {
58 $ts = strftime("%H:%M:%S", time());
62 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
66 if (DB_TYPE
== "pgsql") {
67 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
68 marked = false AND feed_id = '$feed_id' AND
69 (SELECT date_entered FROM ttrss_entries WHERE
70 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
72 $pg_version = get_pgsql_version($link);
74 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
76 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
77 ttrss_entries.id = ref_id AND
79 feed_id = '$feed_id' AND
80 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
84 $result = db_query($link, "DELETE FROM ttrss_user_entries
86 WHERE ttrss_entries.id = ref_id AND
88 feed_id = '$feed_id' AND
89 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
92 $rows = pg_affected_rows($result);
96 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
97 marked = false AND feed_id = '$feed_id' AND
98 (SELECT date_entered FROM ttrss_entries WHERE
99 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
101 $result = db_query($link, "DELETE FROM ttrss_user_entries
102 USING ttrss_user_entries, ttrss_entries
103 WHERE ttrss_entries.id = ref_id AND
105 feed_id = '$feed_id' AND
106 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
108 $rows = mysql_affected_rows($link);
113 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
117 function global_purge_old_posts($link, $do_output = false, $limit = false) {
119 $random_qpart = sql_random_function();
122 $limit_qpart = "LIMIT $limit";
127 $result = db_query($link,
128 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
129 ORDER BY $random_qpart $limit_qpart");
131 while ($line = db_fetch_assoc($result)) {
133 $feed_id = $line["id"];
134 $purge_interval = $line["purge_interval"];
135 $owner_uid = $line["owner_uid"];
137 if ($purge_interval == 0) {
139 $tmp_result = db_query($link,
140 "SELECT value FROM ttrss_user_prefs WHERE
141 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
143 if (db_num_rows($tmp_result) != 0) {
144 $purge_interval = db_fetch_result($tmp_result, 0, "value");
149 // print "Feed $feed_id: purge interval = $purge_interval\n";
152 if ($purge_interval > 0) {
153 purge_feed($link, $feed_id, $purge_interval, $do_output);
157 // purge orphaned posts in main content table
158 db_query($link, "DELETE FROM ttrss_entries WHERE
159 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
163 function purge_old_posts($link) {
165 $user_id = $_SESSION["uid"];
167 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
168 WHERE owner_uid = '$user_id'");
170 while ($line = db_fetch_assoc($result)) {
172 $feed_id = $line["id"];
173 $purge_interval = $line["purge_interval"];
175 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
177 if ($purge_interval > 0) {
178 purge_feed($link, $feed_id, $purge_interval);
182 // purge orphaned posts in main content table
183 db_query($link, "DELETE FROM ttrss_entries WHERE
184 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
187 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
189 if (WEB_DEMO_MODE
) return;
192 $user_id = $_SESSION["uid"];
193 purge_old_posts($link);
196 // db_query($link, "BEGIN");
198 if (MAX_UPDATE_TIME
> 0) {
199 if (DB_TYPE
== "mysql") {
202 $q_order = "RANDOM()";
205 $q_order = "last_updated DESC";
208 $result = db_query($link, "SELECT feed_url,id,
209 SUBSTRING(last_updated,1,19) AS last_updated,
210 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
215 while ($line = db_fetch_assoc($result)) {
216 $upd_intl = $line["update_interval"];
218 if (!$upd_intl ||
$upd_intl == 0) {
219 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
223 // Updates for this feed are disabled
227 if ($fetch ||
(!$line["last_updated"] ||
228 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
230 // print "<!-- feed: ".$line["feed_url"]." -->";
232 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
234 $upd_elapsed = time() - $upd_start;
236 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
242 // db_query($link, "COMMIT");
246 function fetch_file_contents($url) {
247 if (USE_CURL_FOR_ICONS
) {
248 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
250 $ch = curl_init($url);
251 $fp = fopen($tmpfile, "w");
254 curl_setopt($ch, CURLOPT_FILE
, $fp);
255 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
256 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
262 $contents = file_get_contents($tmpfile);
268 return file_get_contents($url);
273 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
274 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
276 function get_favicon_url($url) {
278 if ($html = @fetch_file_contents
($url)) {
280 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
281 // Attempt to grab a favicon link from their webpage url
282 $linkUrl = html_entity_decode($matches[1]);
284 if (substr($linkUrl, 0, 1) == '/') {
285 $urlParts = parse_url($url);
286 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
287 } else if (substr($linkUrl, 0, 7) == 'http://') {
288 $faviconURL = $linkUrl;
289 } else if (substr($url, -1, 1) == '/') {
290 $faviconURL = $url.$linkUrl;
292 $faviconURL = $url.'/'.$linkUrl;
296 // If unsuccessful, attempt to "guess" the favicon location
297 $urlParts = parse_url($url);
298 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
302 // Run a test to see if what we have attempted to get actually exists.
303 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
310 function url_validate($link) {
312 $url_parts = @parse_url
($link);
314 if ( empty( $url_parts["host"] ) )
317 if ( !empty( $url_parts["path"] ) ) {
318 $documentpath = $url_parts["path"];
323 if ( !empty( $url_parts["query"] ) )
324 $documentpath .= "?" . $url_parts["query"];
326 $host = $url_parts["host"];
327 $port = $url_parts["port"];
332 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
337 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
339 $http_response = fgets( $socket, 22 );
341 $responses = "/(200 OK)|(30[0-9] Moved)/";
342 if ( preg_match($responses, $http_response) ) {
351 function check_feed_favicon($site_url, $feed, $link) {
352 $favicon_url = get_favicon_url($site_url);
354 # print "FAVICON [$site_url]: $favicon_url\n";
358 $icon_file = ICONS_DIR
. "/$feed.ico";
360 if ($favicon_url && !file_exists($icon_file)) {
361 $contents = fetch_file_contents($favicon_url);
363 $fp = fopen($icon_file, "w");
366 fwrite($fp, $contents);
368 chmod($icon_file, 0644);
372 error_reporting(DEFAULT_ERROR_LEVEL
);
376 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
378 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
382 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
383 _debug("update_rss_feed: start");
386 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
387 FROM ttrss_feeds WHERE id = '$feed'");
389 $auth_login = db_fetch_result($result, 0, "auth_login");
390 $auth_pass = db_fetch_result($result, 0, "auth_pass");
392 $update_interval = db_fetch_result($result, 0, "update_interval");
394 if ($update_interval < 0) { return; }
396 $feed = db_escape_string($feed);
398 $fetch_url = $feed_url;
400 if ($auth_login && $auth_pass) {
401 $url_parts = array();
402 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
404 if ($url_parts[1] && $url_parts[2]) {
405 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
410 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
411 _debug("update_rss_feed: fetching...");
414 if (!defined('DAEMON_EXTENDED_DEBUG')) {
418 $rss = fetch_rss($fetch_url);
420 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
421 _debug("update_rss_feed: fetch done, parsing...");
423 error_reporting (DEFAULT_ERROR_LEVEL
);
426 $feed = db_escape_string($feed);
430 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
431 _debug("update_rss_feed: processing feed data...");
434 // db_query($link, "BEGIN");
436 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
437 FROM ttrss_feeds WHERE id = '$feed'");
439 $registered_title = db_fetch_result($result, 0, "title");
440 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
441 $orig_site_url = db_fetch_result($result, 0, "site_url");
443 $owner_uid = db_fetch_result($result, 0, "owner_uid");
445 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
446 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
447 _debug("update_rss_feed: checking favicon...");
449 check_feed_favicon($rss->channel
["link"], $feed, $link);
452 if (!$registered_title ||
$registered_title == "[Unknown]") {
454 $feed_title = db_escape_string($rss->channel
["title"]);
456 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
457 _debug("update_rss_feed: registering title: $feed_title");
460 db_query($link, "UPDATE ttrss_feeds SET
461 title = '$feed_title' WHERE id = '$feed'");
464 $site_url = $rss->channel
["link"];
465 // weird, weird Magpie
466 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
468 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
469 db_query($link, "UPDATE ttrss_feeds SET
470 site_url = '$site_url' WHERE id = '$feed'");
473 // print "I: " . $rss->channel["image"]["url"];
475 $icon_url = $rss->image
["url"];
477 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
478 $icon_url = db_escape_string($icon_url);
479 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
482 if (defined('DAEMON_EXTENDED_DEBUG' ||
$_GET['xdebug'])) {
483 _debug("update_rss_feed: loading filters...");
488 $result = db_query($link, "SELECT reg_exp,
489 ttrss_filter_types.name AS name,
490 ttrss_filter_actions.name AS action,
493 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
495 owner_uid = $owner_uid AND
496 ttrss_filter_types.id = filter_type AND
497 ttrss_filter_actions.id = action_id AND
498 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
500 while ($line = db_fetch_assoc($result)) {
501 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
503 $filter["reg_exp"] = $line["reg_exp"];
504 $filter["action"] = $line["action"];
505 $filter["action_param"] = $line["action_param"];
506 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
508 array_push($filters[$line["name"]], $filter);
511 $iterator = $rss->items
;
513 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
514 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
516 if (!is_array($iterator)) {
517 /* db_query($link, "UPDATE ttrss_feeds
518 SET last_error = 'Parse error: can\'t find any articles.'
519 WHERE id = '$feed'"); */
521 // clear any errors and mark feed as updated if fetched okay
522 // even if it's blank
524 if (defined('DAEMON_EXTENDED_DEBUG')) {
525 _debug("update_rss_feed: entry iterator is not an array, no articles?");
528 db_query($link, "UPDATE ttrss_feeds
529 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
531 return; // no articles
534 if (defined('DAEMON_EXTENDED_DEBUG')) {
535 _debug("update_rss_feed: processing articles...");
538 foreach ($iterator as $item) {
540 $entry_guid = $item["id"];
542 if (!$entry_guid) $entry_guid = $item["guid"];
543 if (!$entry_guid) $entry_guid = $item["link"];
544 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
546 if (defined('DAEMON_EXTENDED_DEBUG')) {
547 _debug("update_rss_feed: guid $entry_guid");
550 if (!$entry_guid) continue;
552 $entry_timestamp = "";
554 $rss_2_date = $item['pubdate'];
555 $rss_1_date = $item['dc']['date'];
556 $atom_date = $item['issued'];
557 if (!$atom_date) $atom_date = $item['updated'];
559 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
560 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
561 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
563 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
564 $entry_timestamp = time();
565 $no_orig_date = 'true';
567 $no_orig_date = 'false';
570 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
572 $entry_title = trim(strip_tags($item["title"]));
574 // strange Magpie workaround
575 $entry_link = $item["link_"];
576 if (!$entry_link) $entry_link = $item["link"];
578 if (!$entry_title) continue;
579 # if (!$entry_link) continue;
581 $entry_link = strip_tags($entry_link);
583 $entry_content = $item["content:escaped"];
585 if (!$entry_content) $entry_content = $item["content:encoded"];
586 if (!$entry_content) $entry_content = $item["content"];
587 if (!$entry_content) $entry_content = $item["atom_content"];
588 if (!$entry_content) $entry_content = $item["summary"];
589 if (!$entry_content) $entry_content = $item["description"];
591 // if (!$entry_content) continue;
594 if (is_array($entry_content)) {
595 $entry_content = $entry_content["encoded"];
596 if (!$entry_content) $entry_content = $entry_content["escaped"];
600 // print_r(htmlspecialchars($entry_content));
603 $entry_content_unescaped = $entry_content;
605 $entry_comments = strip_tags($item["comments"]);
607 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
609 if ($item['author']) {
611 if (is_array($item['author'])) {
613 if (!$entry_author) {
614 $entry_author = db_escape_string(strip_tags($item['author']['name']));
617 if (!$entry_author) {
618 $entry_author = db_escape_string(strip_tags($item['author']['email']));
622 if (!$entry_author) {
623 $entry_author = db_escape_string(strip_tags($item['author']));
627 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
629 $entry_guid = db_escape_string(strip_tags($entry_guid));
631 $result = db_query($link, "SELECT id FROM ttrss_entries
632 WHERE guid = '$entry_guid'");
634 $entry_content = db_escape_string($entry_content);
636 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
638 $entry_title = db_escape_string($entry_title);
639 $entry_link = db_escape_string($entry_link);
640 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
641 $entry_author = mb_substr($entry_author, 0, 250);
643 $num_comments = db_escape_string($item["slash"]["comments"]);
645 if (!$num_comments) $num_comments = 0;
647 // parse <category> entries into tags
649 $t_ctr = $item['category#'];
651 $additional_tags = false;
654 $additional_tags = false;
655 } else if ($t_ctr == 1) {
656 $additional_tags = array($item['category']);
658 $additional_tags = array();
659 for ($i = 0; $i <= $t_ctr; $i++
) {
660 if ($item["category#$i"]) {
661 array_push($additional_tags, $item["category#$i"]);
666 // parse <dc:subject> elements
668 $t_ctr = $item['dc']['subject#'];
671 $additional_tags = array($item['dc']['subject']);
672 } else if ($t_ctr > 1) {
673 $additional_tags = array();
674 for ($i = 0; $i <= $t_ctr; $i++
) {
675 if ($item['dc']["subject#$i"]) {
676 array_push($additional_tags, $item['dc']["subject#$i"]);
683 // $entry_content = sanitize_rss($entry_content);
685 if (defined('DAEMON_EXTENDED_DEBUG')) {
686 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
689 db_query($link, "BEGIN");
691 if (db_num_rows($result) == 0) {
693 if (defined('DAEMON_EXTENDED_DEBUG')) {
694 _debug("update_rss_feed: base guid not found");
697 // base post entry does not exist, create it
699 $result = db_query($link,
700 "INSERT INTO ttrss_entries
716 '$entry_timestamp_fmt',
725 // we keep encountering the entry in feeds, so we need to
726 // update date_entered column so that we don't get horrible
727 // dupes when the entry gets purged and reinserted again e.g.
728 // in the case of SLOW SLOW OMG SLOW updating feeds
730 $base_entry_id = db_fetch_result($result, 0, "id");
732 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
733 WHERE id = '$base_entry_id'");
736 // now it should exist, if not - bad luck then
738 $result = db_query($link, "SELECT
739 id,content_hash,no_orig_date,title,
740 substring(date_entered,1,19) as date_entered,
741 substring(updated,1,19) as updated,
745 WHERE guid = '$entry_guid'");
747 if (db_num_rows($result) == 1) {
749 if (defined('DAEMON_EXTENDED_DEBUG')) {
750 _debug("update_rss_feed: base guid found, checking for user record");
753 // this will be used below in update handler
754 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
755 $orig_title = db_fetch_result($result, 0, "title");
756 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
757 $orig_date_entered = strtotime(db_fetch_result($result,
760 $ref_id = db_fetch_result($result, 0, "id");
762 // check for user post link to main table
764 // do we allow duplicate posts with same GUID in different feeds?
765 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
766 $dupcheck_qpart = "AND feed_id = '$feed'";
768 $dupcheck_qpart = "";
771 // error_reporting(0);
773 $article_filters = get_article_filters($filters, $entry_title,
774 $entry_content, $entry_link);
776 if (defined('DAEMON_EXTENDED_DEBUG')) {
777 _debug("update_rss_feed: article filters: ");
778 if (count($article_filters) != 0) {
779 print_r($article_filters);
783 if (find_article_filter($article_filters, "filter")) {
787 // error_reporting (DEFAULT_ERROR_LEVEL);
789 $result = db_query($link,
790 "SELECT ref_id FROM ttrss_user_entries WHERE
791 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
794 // okay it doesn't exist - create user entry
795 if (db_num_rows($result) == 0) {
797 if (defined('DAEMON_EXTENDED_DEBUG')) {
798 _debug("update_rss_feed: user record not found, creating...");
801 if (!find_article_filter($article_filters, 'catchup')) {
803 $last_read_qpart = 'NULL';
806 $last_read_qpart = 'NOW()';
809 if (find_article_filter($article_filters, 'mark')) {
815 $result = db_query($link,
816 "INSERT INTO ttrss_user_entries
817 (ref_id, owner_uid, feed_id, unread, last_read, marked)
818 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
819 $last_read_qpart, $marked)");
822 $post_needs_update = false;
824 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
825 ($content_hash != $orig_content_hash)) {
826 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
827 $post_needs_update = true;
830 if (db_escape_string($orig_title) != $entry_title) {
831 $post_needs_update = true;
834 if ($orig_num_comments != $num_comments) {
835 $post_needs_update = true;
838 // this doesn't seem to be very reliable
840 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
841 // $post_needs_update = true;
844 // if post needs update, update it and mark all user entries
845 // linking to this post as updated
846 if ($post_needs_update) {
848 if (defined('DAEMON_EXTENDED_DEBUG')) {
849 _debug("update_rss_feed: post $entry_guid needs update...");
852 // print "<!-- post $orig_title needs update : $post_needs_update -->";
854 db_query($link, "UPDATE ttrss_entries
855 SET title = '$entry_title', content = '$entry_content',
856 content_hash = '$content_hash',
857 num_comments = '$num_comments'
858 WHERE id = '$ref_id'");
860 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
861 db_query($link, "UPDATE ttrss_user_entries
862 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
864 db_query($link, "UPDATE ttrss_user_entries
865 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
871 db_query($link, "COMMIT");
873 if (defined('DAEMON_EXTENDED_DEBUG')) {
874 _debug("update_rss_feed: looking for tags...");
878 // <a href="..." rel="tag">Xorg</a>, //
882 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
883 $entry_content_unescaped, $entry_tags);
885 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
886 print_r($entry_tags);
887 print "<br/></p>"; */
889 $entry_tags = $entry_tags[1];
891 # check for manual tags
893 $tag_filter = find_article_filter($article_filters, "tag");
897 $manual_tags = trim_array(split(",", $tag_filter[1]));
899 foreach ($manual_tags as $tag) {
900 if (tag_is_valid($tag)) {
901 array_push($entry_tags, $tag);
906 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
907 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
909 if ($additional_tags && is_array($additional_tags)) {
910 foreach ($additional_tags as $tag) {
911 if (tag_is_valid($tag) &&
912 array_search($tag, $boring_tags) === FALSE) {
913 array_push($entry_tags, $tag);
918 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
920 if (count($entry_tags) > 0) {
922 db_query($link, "BEGIN");
924 $result = db_query($link, "SELECT id,int_id
925 FROM ttrss_entries,ttrss_user_entries
926 WHERE guid = '$entry_guid'
927 AND feed_id = '$feed' AND ref_id = id
928 AND owner_uid = '$owner_uid'");
930 if (db_num_rows($result) == 1) {
932 $entry_id = db_fetch_result($result, 0, "id");
933 $entry_int_id = db_fetch_result($result, 0, "int_id");
935 foreach ($entry_tags as $tag) {
937 $tag = sanitize_tag($tag);
938 $tag = db_escape_string($tag);
940 if (!tag_is_valid($tag)) continue;
942 $result = db_query($link, "SELECT id FROM ttrss_tags
943 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
944 owner_uid = '$owner_uid' LIMIT 1");
946 // print db_fetch_result($result, 0, "id");
948 if ($result && db_num_rows($result) == 0) {
950 db_query($link, "INSERT INTO ttrss_tags
951 (owner_uid,tag_name,post_int_id)
952 VALUES ('$owner_uid','$tag', '$entry_int_id')");
956 db_query($link, "COMMIT");
960 db_query($link, "UPDATE ttrss_feeds
961 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
963 // db_query($link, "COMMIT");
966 $error_msg = db_escape_string(magpie_error());
968 "UPDATE ttrss_feeds SET last_error = '$error_msg',
969 last_updated = NOW() WHERE id = '$feed'");
972 if (defined('DAEMON_EXTENDED_DEBUG')) {
973 _debug("update_rss_feed: done");
978 function print_select($id, $default, $values, $attributes = "") {
979 print "<select name=\"$id\" id=\"$id\" $attributes>";
980 foreach ($values as $v) {
986 print "<option$sel>$v</option>";
991 function print_select_hash($id, $default, $values, $attributes = "") {
992 print "<select name=\"$id\" id='$id' $attributes>";
993 foreach (array_keys($values) as $v) {
999 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1005 function get_article_filters($filters, $title, $content, $link) {
1008 if ($filters["title"]) {
1009 foreach ($filters["title"] as $filter) {
1010 $reg_exp = $filter["reg_exp"];
1011 $inverse = $filter["inverse"];
1012 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1013 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1015 array_push($matches, array($filter["action"], $filter["action_param"]));
1020 if ($filters["content"]) {
1021 foreach ($filters["content"] as $filter) {
1022 $reg_exp = $filter["reg_exp"];
1023 $inverse = $filter["inverse"];
1025 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1026 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1028 array_push($matches, array($filter["action"], $filter["action_param"]));
1033 if ($filters["both"]) {
1034 foreach ($filters["both"] as $filter) {
1035 $reg_exp = $filter["reg_exp"];
1036 $inverse = $filter["inverse"];
1039 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1040 array_push($matches, array($filter["action"], $filter["action_param"]));
1043 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1044 array_push($matches, array($filter["action"], $filter["action_param"]));
1050 if ($filters["link"]) {
1051 $reg_exp = $filter["reg_exp"];
1052 foreach ($filters["link"] as $filter) {
1053 $reg_exp = $filter["reg_exp"];
1054 $inverse = $filter["inverse"];
1056 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1057 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1059 array_push($matches, array($filter["action"], $filter["action_param"]));
1067 function find_article_filter($filters, $filter_name) {
1068 foreach ($filters as $f) {
1069 if ($f[0] == $filter_name) {
1076 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1077 $rtl_content = false, $last_updated = false, $last_error = false) {
1079 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1080 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1082 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1086 $rtl_tag = "dir=\"rtl\"";
1088 $rtl_tag = "dir=\"ltr\"";
1091 $error_notify_msg = "";
1094 $link_title = "Error: $last_error ($last_updated)";
1095 $error_notify_msg = "(Error)";
1096 } else if ($last_updated) {
1097 $link_title = "Updated: $last_updated";
1100 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1101 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1103 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1104 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1108 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1113 $fctr_class = "class=\"invisible\"";
1116 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1117 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1119 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1120 print "<div class=\"feedExtInfo\">
1121 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1128 function getmicrotime() {
1129 list($usec, $sec) = explode(" ",microtime());
1130 return ((float)$usec +
(float)$sec);
1133 function print_radio($id, $default, $values, $attributes = "") {
1134 foreach ($values as $v) {
1142 $sel .= " value=\"1\"";
1144 $sel .= " value=\"0\"";
1147 print "<input class=\"noborder\"
1148 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1153 function initialize_user_prefs($link, $uid) {
1155 $uid = db_escape_string($uid);
1157 db_query($link, "BEGIN");
1159 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1161 $u_result = db_query($link, "SELECT pref_name
1162 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1164 $active_prefs = array();
1166 while ($line = db_fetch_assoc($u_result)) {
1167 array_push($active_prefs, $line["pref_name"]);
1170 while ($line = db_fetch_assoc($result)) {
1171 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1172 // print "adding " . $line["pref_name"] . "<br>";
1174 db_query($link, "INSERT INTO ttrss_user_prefs
1175 (owner_uid,pref_name,value) VALUES
1176 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1181 db_query($link, "COMMIT");
1185 function lookup_user_id($link, $user) {
1187 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1190 if (db_num_rows($result) == 1) {
1191 return db_fetch_result($result, 0, "id");
1197 function http_authenticate_user($link) {
1199 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1201 if (!$_SERVER["PHP_AUTH_USER"]) {
1203 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1204 header('HTTP/1.0 401 Unauthorized');
1208 $auth_result = authenticate_user($link,
1209 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1211 if (!$auth_result) {
1212 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1213 header('HTTP/1.0 401 Unauthorized');
1221 function authenticate_user($link, $login, $password, $force_auth = false) {
1223 if (!SINGLE_USER_MODE
) {
1225 $pwd_hash = 'SHA1:' . sha1($password);
1227 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1228 $query = "SELECT id,login,access_level
1229 FROM ttrss_users WHERE
1232 $query = "SELECT id,login,access_level
1233 FROM ttrss_users WHERE
1234 login = '$login' AND pwd_hash = '$pwd_hash'";
1237 $result = db_query($link, $query);
1239 if (db_num_rows($result) == 1) {
1240 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1241 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1242 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1244 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1247 $user_theme = get_user_theme_path($link);
1249 $_SESSION["theme"] = $user_theme;
1250 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1252 initialize_user_prefs($link, $_SESSION["uid"]);
1261 $_SESSION["uid"] = 1;
1262 $_SESSION["name"] = "admin";
1264 $user_theme = get_user_theme_path($link);
1266 $_SESSION["theme"] = $user_theme;
1267 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1269 initialize_user_prefs($link, $_SESSION["uid"]);
1275 function make_password($length = 8) {
1278 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1282 while ($i < $length) {
1283 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1285 if (!strstr($password, $char)) {
1293 // this is called after user is created to initialize default feeds, labels
1296 // user preferences are checked on every login, not here
1298 function initialize_user($link, $uid) {
1300 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1301 values ('$uid','unread = true', 'Unread articles')");
1303 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1304 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1306 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1307 values ('$uid', 'Tiny Tiny RSS: New Releases',
1308 'http://tt-rss.spb.ru/releases.rss')");
1310 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1311 values ('$uid', 'Tiny Tiny RSS: Forum',
1312 'http://tt-rss.spb.ru/forum/rss.php')");
1315 function logout_user() {
1317 if (isset($_COOKIE[session_name()])) {
1318 setcookie(session_name(), '', time()-42000, '/');
1322 function get_script_urlpath() {
1323 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1326 function validate_session($link) {
1327 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1328 if ($_SESSION["ip_address"]) {
1329 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1330 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1336 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1338 //print_r($_SESSION);
1340 if (time() > $_SESSION["cookie_lifetime"]) {
1348 function login_sequence($link, $mobile = false) {
1349 if (!SINGLE_USER_MODE
) {
1351 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1352 $swu = db_escape_string($_REQUEST["swu"]);
1354 $_SESSION["prefs_cache"] = false;
1355 return authenticate_user($link, $swu, null, true);
1359 $login_action = $_POST["login_action"];
1361 # try to authenticate user if called from login form
1362 if ($login_action == "do_login") {
1363 $login = $_POST["login"];
1364 $password = $_POST["password"];
1365 $remember_me = $_POST["remember_me"];
1367 if (authenticate_user($link, $login, $password)) {
1368 $_POST["password"] = "";
1370 header("Location: " . $_SERVER["REQUEST_URI"]);
1375 $_SESSION["login_error_msg"] = "Incorrect username or password";
1379 // print session_id();
1380 // print_r($_SESSION);
1382 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1383 render_login_form($link, $mobile);
1386 /* bump login timestamp */
1387 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1392 return authenticate_user($link, "admin", null);
1396 function truncate_string($str, $max_len) {
1397 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1398 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1404 function get_user_theme_path($link) {
1405 $result = db_query($link, "SELECT theme_path
1407 ttrss_themes,ttrss_users
1408 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1409 if (db_num_rows($result) != 0) {
1410 return db_fetch_result($result, 0, "theme_path");
1416 function smart_date_time($timestamp) {
1417 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1418 return date("G:i", $timestamp);
1419 } else if (date("Y", $timestamp) == date("Y")) {
1420 return date("M d, G:i", $timestamp);
1422 return date("Y/m/d, G:i", $timestamp);
1426 function smart_date($timestamp) {
1427 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1429 } else if (date("Y", $timestamp) == date("Y")) {
1430 return date("D m", $timestamp);
1432 return date("Y/m/d", $timestamp);
1436 function sql_bool_to_string($s) {
1437 if ($s == "t" ||
$s == "1") {
1444 function sql_bool_to_bool($s) {
1445 if ($s == "t" ||
$s == "1") {
1453 function toggleEvenOdd($a) {
1460 function sanity_check($link) {
1465 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1466 $schema_version = db_fetch_result($result, 0, "schema_version");
1468 if ($schema_version != SCHEMA_VERSION
) {
1472 if (DB_TYPE
== "mysql") {
1473 $result = db_query($link, "SELECT true", false);
1474 if (db_num_rows($result) != 1) {
1479 error_reporting (DEFAULT_ERROR_LEVEL
);
1481 if ($error_code != 0) {
1482 print_error_xml($error_code);
1489 function file_is_locked($filename) {
1491 $fp = fopen($filename, "r");
1492 error_reporting(DEFAULT_ERROR_LEVEL
);
1494 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1495 flock($fp, LOCK_UN
);
1505 function make_lockfile($filename) {
1506 $fp = fopen($filename, "w");
1508 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1515 function make_stampfile($filename) {
1516 $fp = fopen($filename, "w");
1518 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1519 fwrite($fp, time() . "\n");
1520 flock($fp, LOCK_UN
);
1528 function read_stampfile($filename) {
1531 $fp = fopen($filename, "r");
1532 error_reporting (DEFAULT_ERROR_LEVEL
);
1534 if (flock($fp, LOCK_EX
)) {
1535 $stamp = fgets($fp);
1536 flock($fp, LOCK_UN
);
1544 function sql_random_function() {
1545 if (DB_TYPE
== "mysql") {
1552 function catchup_feed($link, $feed, $cat_view) {
1554 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1559 $cat_qpart = "cat_id = '$feed'";
1561 $cat_qpart = "cat_id IS NULL";
1564 $tmp_result = db_query($link, "SELECT id
1565 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1568 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1570 $tmp_feed = $tmp_line["id"];
1572 db_query($link, "UPDATE ttrss_user_entries
1573 SET unread = false,last_read = NOW()
1574 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1577 } else if ($feed > 0) {
1579 $tmp_result = db_query($link, "SELECT id
1580 FROM ttrss_feeds WHERE parent_feed = '$feed'
1581 ORDER BY cat_id,title");
1583 $parent_ids = array();
1585 if (db_num_rows($tmp_result) > 0) {
1586 while ($p = db_fetch_assoc($tmp_result)) {
1587 array_push($parent_ids, "feed_id = " . $p["id"]);
1590 $children_qpart = implode(" OR ", $parent_ids);
1592 db_query($link, "UPDATE ttrss_user_entries
1593 SET unread = false,last_read = NOW()
1594 WHERE (feed_id = '$feed' OR $children_qpart)
1595 AND owner_uid = " . $_SESSION["uid"]);
1598 db_query($link, "UPDATE ttrss_user_entries
1599 SET unread = false,last_read = NOW()
1600 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1603 } else if ($feed < 0 && $feed > -10) { // special, like starred
1606 db_query($link, "UPDATE ttrss_user_entries
1607 SET unread = false,last_read = NOW()
1608 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1612 db_query($link, "UPDATE ttrss_user_entries
1613 SET unread = false,last_read = NOW()
1614 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1617 } else if ($feed < -10) { // label
1619 // TODO make this more efficient
1621 $label_id = -$feed - 11;
1623 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1624 WHERE id = '$label_id'");
1627 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1629 db_query($link, "BEGIN");
1631 $tmp2_result = db_query($link,
1635 ttrss_user_entries,ttrss_entries,ttrss_feeds
1637 ref_id = ttrss_entries.id AND
1638 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1640 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1642 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1643 db_query($link, "UPDATE
1646 unread = false, last_read = NOW()
1648 int_id = " . $tmp_line["int_id"]);
1651 db_query($link, "COMMIT");
1653 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1654 SET unread = false,last_read = NOW()
1657 AND owner_uid = ".$_SESSION["uid"]); */
1661 db_query($link, "BEGIN");
1663 $tag_name = db_escape_string($feed);
1665 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1666 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1668 while ($line = db_fetch_assoc($result)) {
1669 db_query($link, "UPDATE ttrss_user_entries SET
1670 unread = false, last_read = NOW()
1671 WHERE int_id = " . $line["post_int_id"]);
1673 db_query($link, "COMMIT");
1677 function update_generic_feed($link, $feed, $cat_view) {
1681 $cat_qpart = "cat_id = '$feed'";
1683 $cat_qpart = "cat_id IS NULL";
1686 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1687 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1689 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1690 $feed_url = $tmp_line["feed_url"];
1691 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1695 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1696 WHERE id = '$feed'");
1697 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1698 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1702 function getAllCounters($link, $omode = "tflc") {
1703 /* getLabelCounters($link);
1704 getFeedCounters($link);
1705 getTagCounters($link);
1706 getGlobalCounters($link);
1707 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1708 getCategoryCounters($link);
1711 if (!$omode) $omode = "tflc";
1713 getGlobalCounters($link);
1715 if (strchr($omode, "l")) getLabelCounters($link);
1716 if (strchr($omode, "f")) getFeedCounters($link);
1717 if (strchr($omode, "t")) getTagCounters($link);
1718 if (strchr($omode, "c")) {
1719 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1720 getCategoryCounters($link);
1725 function getCategoryCounters($link) {
1726 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1727 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1728 AND unread = true)) AS unread FROM ttrss_feeds
1730 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1732 while ($line = db_fetch_assoc($result)) {
1733 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1734 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1735 $line["unread"]."\"/>";
1739 function getCategoryUnread($link, $cat) {
1742 $cat_query = "cat_id = '$cat'";
1744 $cat_query = "cat_id IS NULL";
1747 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1749 AND owner_uid = " . $_SESSION["uid"]);
1751 $cat_feeds = array();
1752 while ($line = db_fetch_assoc($result)) {
1753 array_push($cat_feeds, "feed_id = " . $line["id"]);
1756 if (count($cat_feeds) == 0) return 0;
1758 $match_part = implode(" OR ", $cat_feeds);
1760 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1761 FROM ttrss_user_entries
1762 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1766 # this needs to be rewritten
1767 while ($line = db_fetch_assoc($result)) {
1768 $unread +
= $line["unread"];
1775 function getFeedUnread($link, $feed, $is_cat = false) {
1776 $n_feed = sprintf("%d", $feed);
1779 return getCategoryUnread($link, $n_feed);
1780 } else if ($n_feed == -1) {
1781 $match_part = "marked = true";
1782 } else if ($n_feed == -2) {
1783 $match_part = "published = true";
1784 } else if ($n_feed > 0) {
1786 $result = db_query($link, "SELECT id FROM ttrss_feeds
1787 WHERE parent_feed = '$n_feed'
1789 AND owner_uid = " . $_SESSION["uid"]);
1791 if (db_num_rows($result) > 0) {
1793 $linked_feeds = array();
1794 while ($line = db_fetch_assoc($result)) {
1795 array_push($linked_feeds, "feed_id = " . $line["id"]);
1798 array_push($linked_feeds, "feed_id = $n_feed");
1800 $match_part = implode(" OR ", $linked_feeds);
1802 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1803 FROM ttrss_user_entries
1804 WHERE unread = true AND ($match_part)
1805 AND owner_uid = " . $_SESSION["uid"]);
1809 # this needs to be rewritten
1810 while ($line = db_fetch_assoc($result)) {
1811 $unread +
= $line["unread"];
1817 $match_part = "feed_id = '$n_feed'";
1819 } else if ($feed < -10) {
1821 $label_id = -$feed - 11;
1823 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1824 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1826 $match_part = db_fetch_result($result, 0, "sql_exp");
1831 $result = db_query($link, "SELECT count(int_id) AS unread
1832 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1833 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1834 ttrss_user_entries.ref_id = ttrss_entries.id AND
1835 ttrss_feeds.hidden = false AND
1836 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1840 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1841 FROM ttrss_tags,ttrss_user_entries
1842 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1843 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1846 $unread = db_fetch_result($result, 0, "unread");
1851 /* FIXME this needs reworking */
1853 function getGlobalUnread($link, $user_id = false) {
1856 $user_id = $_SESSION["uid"];
1859 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1860 WHERE unread = true AND
1861 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1862 ttrss_user_entries.ref_id = ttrss_entries.id AND
1864 ttrss_user_entries.owner_uid = '$user_id'");
1865 $c_id = db_fetch_result($result, 0, "c_id");
1869 function getGlobalCounters($link, $global_unread = -1) {
1870 if ($global_unread == -1) {
1871 $global_unread = getGlobalUnread($link);
1873 print "<counter type=\"global\" id='global-unread'
1874 counter='$global_unread'/>";
1876 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1877 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1879 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1881 print "<counter type=\"global\" id='subscribed-feeds'
1882 counter='$subscribed_feeds'/>";
1886 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1889 if (!$_SESSION["tctr_last_value"]) {
1890 $_SESSION["tctr_last_value"] = array();
1894 $old_counters = $_SESSION["tctr_last_value"];
1896 $tctrs_modified = false;
1898 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1899 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1900 ttrss_user_entries.ref_id = ttrss_entries.id AND
1901 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1902 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1904 select tag_name,0 as count FROM ttrss_tags
1905 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1907 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1908 FROM ttrss_user_entries WHERE int_id = post_int_id
1909 AND unread = true)) AS count FROM ttrss_tags
1910 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
1914 while ($line = db_fetch_assoc($result)) {
1915 $tags[$line["tag_name"]] +
= $line["count"];
1918 foreach (array_keys($tags) as $tag) {
1919 $unread = $tags[$tag];
1921 $tag = htmlspecialchars($tag);
1923 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1924 $old_counters[$tag] = $unread;
1925 $tctrs_modified = true;
1926 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1931 if ($smart_mode && $tctrs_modified) {
1932 $_SESSION["tctr_last_value"] = $old_counters;
1937 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1940 if (!$_SESSION["lctr_last_value"]) {
1941 $_SESSION["lctr_last_value"] = array();
1947 $old_counters = $_SESSION["lctr_last_value"];
1948 $lctrs_modified = false;
1950 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1951 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1952 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1953 hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1955 $count = db_fetch_result($result, 0, "count");
1958 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1960 $ret_arr["-1"]["counter"] = $count;
1961 $ret_arr["-1"]["description"] = "Starred";
1964 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1965 WHERE published = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1966 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1967 hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1969 $count = db_fetch_result($result, 0, "count");
1972 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
1974 $ret_arr["-2"]["counter"] = $count;
1975 $ret_arr["-2"]["description"] = "Published";
1979 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1980 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1982 while ($line = db_fetch_assoc($result)) {
1984 $id = -$line["id"] - 11;
1986 $label_name = $line["description"];
1988 error_reporting (0);
1990 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1991 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1992 ttrss_feeds.hidden = false AND
1993 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1994 ttrss_user_entries.ref_id = ttrss_entries.id AND
1995 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1997 $count = db_fetch_result($tmp_result, 0, "count");
1999 if (!$smart_mode ||
$old_counters[$id] != $count) {
2000 $old_counters[$id] = $count;
2001 $lctrs_modified = true;
2003 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2005 $ret_arr[$id]["counter"] = $count;
2006 $ret_arr[$id]["description"] = $label_name;
2010 error_reporting (DEFAULT_ERROR_LEVEL
);
2013 if ($smart_mode && $lctrs_modified) {
2014 $_SESSION["lctr_last_value"] = $old_counters;
2020 /* function getFeedCounter($link, $id) {
2022 $result = db_query($link, "SELECT
2023 count(id) as count,last_error
2024 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2025 WHERE feed_id = '$id' AND unread = true
2026 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2027 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2029 $count = db_fetch_result($result, 0, "count");
2030 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2032 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2035 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2038 if (!$_SESSION["fctr_last_value"]) {
2039 $_SESSION["fctr_last_value"] = array();
2043 $old_counters = $_SESSION["fctr_last_value"];
2045 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2046 SUBSTRING(last_updated,1,19) AS last_updated,
2048 FROM ttrss_entries,ttrss_user_entries
2049 WHERE feed_id = ttrss_feeds.id AND
2050 ttrss_user_entries.ref_id = ttrss_entries.id
2051 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2052 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2053 AND parent_feed IS NULL"); */
2055 $result = db_query($link, "SELECT ttrss_feeds.id,
2056 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2058 COUNT(ttrss_entries.id) AS count
2060 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2061 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2062 AND ttrss_user_entries.unread = true)
2063 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2064 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2065 AND parent_feed IS NULL
2066 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error");
2068 $fctrs_modified = false;
2070 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2072 while ($line = db_fetch_assoc($result)) {
2075 $count = $line["count"];
2076 $last_error = htmlspecialchars($line["last_error"]);
2078 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2079 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2081 $last_updated = date($short_date, strtotime($line["last_updated"]));
2084 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2086 $tmp_result = db_query($link,
2087 "SELECT id,COUNT(unread) AS unread
2088 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2089 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2090 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2092 if (db_num_rows($tmp_result) > 0) {
2093 while ($l = db_fetch_assoc($tmp_result)) {
2094 $count +
= $l["unread"];
2098 if (!$smart_mode ||
$old_counters[$id] != $count) {
2099 $old_counters[$id] = $count;
2100 $fctrs_modified = true;
2103 $error_part = "error=\"$last_error\"";
2109 $has_img_part = "hi=\"$has_img\"";
2114 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
2118 if ($smart_mode && $fctrs_modified) {
2119 $_SESSION["fctr_last_value"] = $old_counters;
2123 function get_script_dt_add() {
2124 if (strpos(VERSION
, ".99") === false) {
2131 function get_pgsql_version($link) {
2132 $result = db_query($link, "SELECT version() AS version");
2133 $version = split(" ", db_fetch_result($result, 0, "version"));
2137 function print_error_xml($code, $add_msg = "") {
2140 $error_msg = $ERRORS[$code];
2143 $error_msg = "$error_msg; $add_msg";
2146 print "<rpc-reply>";
2147 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2148 print "</rpc-reply>";
2151 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2152 $auth_login = '', $auth_pass = '') {
2154 # check for feed:http://url
2155 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2157 # check for feed://URL
2158 if (strpos($feed_link, "//") === 0) {
2159 $feed_link = "http:$feed_link";
2162 if ($feed_link == "") return;
2164 if ($cat_id == "0" ||
!$cat_id) {
2165 $cat_qpart = "NULL";
2167 $cat_qpart = "'$cat_id'";
2170 $result = db_query($link,
2171 "SELECT id FROM ttrss_feeds
2172 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2174 if (db_num_rows($result) == 0) {
2176 $result = db_query($link,
2177 "INSERT INTO ttrss_feeds
2178 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2179 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2180 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2182 $result = db_query($link,
2183 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2184 AND owner_uid = " . $_SESSION["uid"]);
2186 $feed_id = db_fetch_result($result, 0, "id");
2189 update_rss_feed($link, $feed_link, $feed_id, true);
2198 function print_feed_select($link, $id, $default_id = "",
2199 $attributes = "", $include_all_feeds = true) {
2201 print "<select id=\"$id\" name=\"$id\" $attributes>";
2202 if ($include_all_feeds) {
2203 print "<option value=\"0\">All feeds</option>";
2206 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2207 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2209 if (db_num_rows($result) > 0 && $include_all_feeds) {
2210 print "<option disabled>--------</option>";
2213 while ($line = db_fetch_assoc($result)) {
2214 if ($line["id"] == $default_id) {
2215 $is_selected = "selected";
2219 printf("<option $is_selected value='%d'>%s</option>",
2220 $line["id"], htmlspecialchars($line["title"]));
2226 function print_feed_cat_select($link, $id, $default_id = "",
2227 $attributes = "", $include_all_cats = true) {
2229 print "<select id=\"$id\" name=\"$id\" $attributes>";
2231 if ($include_all_cats) {
2232 print "<option value=\"0\">".__('Uncategorized')."</option>";
2235 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2236 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2238 if (db_num_rows($result) > 0 && $include_all_cats) {
2239 print "<option disabled>--------</option>";
2242 while ($line = db_fetch_assoc($result)) {
2243 if ($line["id"] == $default_id) {
2244 $is_selected = "selected";
2248 printf("<option $is_selected value='%d'>%s</option>",
2249 $line["id"], htmlspecialchars($line["title"]));
2255 function checkbox_to_sql_bool($val) {
2256 return ($val == "on") ?
"true" : "false";
2259 function getFeedCatTitle($link, $id) {
2261 return __("Special");
2262 } else if ($id < -10) {
2263 return __("Labels");
2264 } else if ($id > 0) {
2265 $result = db_query($link, "SELECT ttrss_feed_categories.title
2266 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2267 cat_id = ttrss_feed_categories.id");
2268 if (db_num_rows($result) == 1) {
2269 return db_fetch_result($result, 0, "title");
2271 return __("Uncategorized");
2274 return "getFeedCatTitle($id) failed";
2279 function getFeedTitle($link, $id) {
2281 return __("Starred articles");
2282 } else if ($id < -10) {
2283 $label_id = -10 - $id;
2284 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2285 if (db_num_rows($result) == 1) {
2286 return db_fetch_result($result, 0, "description");
2288 return "Unknown label ($label_id)";
2291 } else if ($id > 0) {
2292 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2293 if (db_num_rows($result) == 1) {
2294 return db_fetch_result($result, 0, "title");
2296 return "Unknown feed ($id)";
2299 return "getFeedTitle($id) failed";
2304 function get_session_cookie_name() {
2305 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2308 function print_init_params($link) {
2309 print "<init-params>";
2310 if ($_SESSION["stored-params"]) {
2311 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2313 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2314 print "<param key=\"$key\" value=\"$value\"/>";
2319 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2320 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2321 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2323 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2324 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2326 print "<param key=\"hide_read_feeds\" value=\"" .
2327 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2329 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2330 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2332 print "<param key=\"confirm_feed_catchup\" value=\"" .
2333 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2335 print "<param key=\"cdm_auto_catchup\" value=\"" .
2336 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2338 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2340 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2342 print "<param key=\"default_view_mode\" value=\"" .
2343 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2345 print "<param key=\"default_view_limit\" value=\"" .
2346 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2348 print "<param key=\"prefs_active_tab\" value=\"" .
2349 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2351 print "<param key=\"infobox_disable_overlay\" value=\"" .
2352 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2354 print "</init-params>";
2357 function print_runtime_info($link) {
2358 print "<runtime-info>";
2359 if (ENABLE_UPDATE_DAEMON
) {
2360 print "<param key=\"daemon_is_running\" value=\"".
2361 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2363 if ($_SESSION["daemon_stamp_check"] +
600 < time()) {
2365 $stamp = (int)read_stampfile("update_daemon.stamp");
2368 if ($stamp +
86400*3 < time()) {
2369 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2371 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2374 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2376 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2379 $_SESSION["daemon_stamp_check"] = time();
2383 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2385 if ($_SESSION["last_version_check"] +
600 < time()) {
2386 $new_version_details = check_for_update($link);
2388 print "<param key=\"new_version_available\" value=\"".
2389 sprintf("%d", $new_version_details != ""). "\"/>";
2391 $_SESSION["last_version_check"] = time();
2395 print "</runtime-info>";
2398 function getSearchSql($search, $match_on) {
2400 $search_query_part = "";
2402 $keywords = split(" ", $search);
2403 $query_keywords = array();
2405 if ($match_on == "both") {
2407 foreach ($keywords as $k) {
2408 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2409 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2412 $search_query_part = implode("AND", $query_keywords) . " AND ";
2414 } else if ($match_on == "title") {
2416 foreach ($keywords as $k) {
2417 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2420 $search_query_part = implode("AND", $query_keywords) . " AND ";
2422 } else if ($match_on == "content") {
2424 foreach ($keywords as $k) {
2425 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2429 $search_query_part = implode("AND", $query_keywords);
2431 return $search_query_part;
2434 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2438 $search_query_part = getSearchSql($search, $match_on);
2439 $search_query_part .= " AND ";
2442 $search_query_part = "";
2445 $view_query_part = "";
2447 if ($view_mode == "adaptive") {
2449 $view_query_part = " ";
2450 } else if ($feed != -1) {
2451 $unread = getFeedUnread($link, $feed, $cat_view);
2453 $view_query_part = " unread = true AND ";
2458 if ($view_mode == "marked") {
2459 $view_query_part = " marked = true AND ";
2462 if ($view_mode == "unread") {
2463 $view_query_part = " unread = true AND ";
2467 $limit_query_part = "LIMIT " . $limit;
2470 $vfeed_query_part = "";
2472 // override query strategy and enable feed display when searching globally
2473 if ($search && $search_mode == "all_feeds") {
2474 $query_strategy_part = "ttrss_entries.id > 0";
2475 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2476 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2477 $query_strategy_part = "ttrss_entries.id > 0";
2478 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2479 id = feed_id) as feed_title,";
2480 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2482 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2484 $tmp_result = false;
2487 $tmp_result = db_query($link, "SELECT id
2488 FROM ttrss_feeds WHERE cat_id = '$feed'");
2490 $tmp_result = db_query($link, "SELECT id
2491 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2492 WHERE id = '$feed') AND id != '$feed'");
2495 $cat_siblings = array();
2497 if (db_num_rows($tmp_result) > 0) {
2498 while ($p = db_fetch_assoc($tmp_result)) {
2499 array_push($cat_siblings, "feed_id = " . $p["id"]);
2502 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2503 $feed, implode(" OR ", $cat_siblings));
2506 $query_strategy_part = "ttrss_entries.id > 0";
2509 } else if ($feed >= 0) {
2514 $query_strategy_part = "cat_id = '$feed'";
2516 $query_strategy_part = "cat_id IS NULL";
2519 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2522 $tmp_result = db_query($link, "SELECT id
2523 FROM ttrss_feeds WHERE parent_feed = '$feed'
2524 ORDER BY cat_id,title");
2526 $parent_ids = array();
2528 if (db_num_rows($tmp_result) > 0) {
2529 while ($p = db_fetch_assoc($tmp_result)) {
2530 array_push($parent_ids, "feed_id = " . $p["id"]);
2533 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2534 $feed, implode(" OR ", $parent_ids));
2536 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2538 $query_strategy_part = "feed_id = '$feed'";
2541 } else if ($feed == -1) { // starred virtual feed
2542 $query_strategy_part = "marked = true";
2543 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2544 } else if ($feed == -2) { // published virtual feed
2545 $query_strategy_part = "published = true";
2546 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2547 } else if ($feed <= -10) { // labels
2548 $label_id = -$feed - 11;
2550 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2551 WHERE id = '$label_id'");
2553 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2555 if (!$query_strategy_part) {
2559 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2561 $query_strategy_part = "id > 0"; // dumb
2564 if (get_pref($link, 'REVERSE_HEADLINES')) {
2565 $order_by = "updated";
2567 $order_by = "updated DESC";
2570 if ($override_order) {
2571 $order_by = $override_order;
2576 if ($search && $search_mode == "all_feeds") {
2577 $feed_title = __("Search results")." ($search)";
2578 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2579 $feed_title = __("Search results")." ($search, $feed)";
2580 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2581 $feed_title = $feed;
2582 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2587 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2588 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2589 $feed_title = db_fetch_result($result, 0, "title");
2591 $feed_title = __("Uncategorized");
2595 $feed_title = __("Searched for")." $search ($feed_title)";
2600 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2601 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2603 $feed_title = db_fetch_result($result, 0, "title");
2604 $feed_site_url = db_fetch_result($result, 0, "site_url");
2605 $last_error = db_fetch_result($result, 0, "last_error");
2608 $feed_title = __("Searched for") . " $search ($feed_title)";
2612 } else if ($feed == -1) {
2613 $feed_title = __("Starred articles");
2614 } else if ($feed == -2) {
2615 $feed_title = __("Published articles");
2616 } else if ($feed < -10) {
2617 $label_id = -$feed - 11;
2618 $result = db_query($link, "SELECT description FROM ttrss_labels
2619 WHERE id = '$label_id'");
2620 $feed_title = db_fetch_result($result, 0, "description");
2623 $feed_title = __("Searched for") . " $search ($feed_title)";
2629 if ($feed < -10) error_reporting (0);
2631 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2634 $feed_kind = "Feeds";
2636 $feed_kind = "Labels";
2639 $content_query_part = "content as content_preview,";
2641 if ($limit_query_part) {
2642 $offset_query_part = "OFFSET $offset";
2647 ttrss_entries.id,ttrss_entries.title,
2649 unread,feed_id,marked,published,link,last_read,
2650 SUBSTRING(last_read,1,19) as last_read_noms,
2653 SUBSTRING(updated,1,19) as updated_noms,
2656 ttrss_entries,ttrss_user_entries,ttrss_feeds
2658 ttrss_feeds.hidden = false AND
2659 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2660 ttrss_user_entries.ref_id = ttrss_entries.id AND
2661 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2664 $query_strategy_part ORDER BY $order_by
2665 $limit_query_part $offset_query_part";
2667 $result = db_query($link, $query);
2669 if ($_GET["debug"]) print $query;
2674 $feed_kind = "Tags";
2676 $result = db_query($link, "SELECT
2678 ttrss_entries.id as id,title,
2681 marked,link,last_read,
2682 SUBSTRING(last_read,1,19) as last_read_noms,
2685 SUBSTRING(updated,1,19) as updated_noms
2687 ttrss_entries,ttrss_user_entries,ttrss_tags
2689 ref_id = ttrss_entries.id AND
2690 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2691 post_int_id = int_id AND tag_name = '$feed' AND
2694 $query_strategy_part ORDER BY $order_by
2695 $limit_query_part");
2698 return array($result, $feed_title, $feed_site_url, $last_error);
2702 function generate_syndicated_feed($link, $feed, $is_cat,
2703 $search, $search_mode, $match_on) {
2705 $qfh_ret = queryFeedHeadlines($link, $feed,
2706 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2708 $result = $qfh_ret[0];
2709 $feed_title = htmlspecialchars($qfh_ret[1]);
2710 $feed_site_url = $qfh_ret[2];
2711 $last_error = $qfh_ret[3];
2713 # if (!$feed_site_url) $feed_site_url = "http://localhost/";
2715 print "<rss version=\"2.0\">
2717 <title>$feed_title</title>
2718 <link>$feed_site_url</link>
2719 <description>Feed generated by Tiny Tiny RSS</description>";
2721 while ($line = db_fetch_assoc($result)) {
2723 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
2724 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2726 $tags = get_article_tags($link, $line["id"]);
2728 foreach ($tags as $tag) {
2729 print "<category>" . htmlspecialchars($tag) . "</category>";
2732 $rfc822_date = date('r', strtotime($line["updated"]));
2734 print "<pubDate>$rfc822_date</pubDate>";
2737 htmlspecialchars($line["title"]) . "</title>";
2739 print "<description><![CDATA[" .
2740 $line["content_preview"] . "]]></description>";
2745 print "</channel></rss>";
2749 function getCategoryTitle($link, $cat_id) {
2751 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2754 if (db_num_rows($result) == 1) {
2755 return db_fetch_result($result, 0, "title");
2757 return "Uncategorized";
2761 // http://ru2.php.net/strip-tags
2763 function strip_tags_long($textstring, $allowed){
2764 while($textstring != strip_tags($textstring, $allowed))
2766 while (strlen($textstring) != 0)
2768 if (strlen($textstring) > 1024) {
2771 $otherlen = strlen($textstring);
2773 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
2774 $safetext .= $temptext;
2775 $textstring = substr_replace($textstring,'',0,$otherlen);
2777 $textstring = $safetext;
2783 function sanitize_rss($link, $str, $force_strip_tags = false) {
2786 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
2788 global $tw_paranoya_setup;
2790 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
2792 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
2793 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
2799 function send_headlines_digests($link, $limit = 100) {
2801 if (!DIGEST_ENABLE
) return false;
2803 $user_limit = DIGEST_EMAIL_LIMIT
;
2806 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2808 if (DB_TYPE
== "pgsql") {
2809 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2810 } else if (DB_TYPE
== "mysql") {
2811 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2814 $result = db_query($link, "SELECT id,email FROM ttrss_users
2815 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2817 while ($line = db_fetch_assoc($result)) {
2818 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2819 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2821 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2822 $digest = $tuple[0];
2823 $headlines_count = $tuple[1];
2825 if ($headlines_count > 0) {
2826 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2827 "[tt-rss] New headlines for last 24 hours", $digest,
2828 "From: " . MAIL_FROM
. "\n".
2829 "Content-Type: text/plain; charset=\"utf-8\"\n".
2830 "Content-Transfer-Encoding: 8bit\n");
2832 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2833 WHERE id = " . $line["id"]);
2835 print "No headlines\n";
2840 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2844 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2845 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
2846 $tmp .= "=======================================================\n\n";
2848 if (DB_TYPE
== "pgsql") {
2849 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2850 } else if (DB_TYPE
== "mysql") {
2851 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2854 $result = db_query($link, "SELECT ttrss_entries.title,
2855 ttrss_feeds.title AS feed_title,
2858 SUBSTRING(last_updated,1,19) AS last_updated
2860 ttrss_user_entries,ttrss_entries,ttrss_feeds
2862 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2863 AND include_in_digest = true
2865 AND ttrss_user_entries.owner_uid = $user_id
2866 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2869 $cur_feed_title = "";
2871 $headlines_count = db_num_rows($result);
2873 while ($line = db_fetch_assoc($result)) {
2874 $updated = smart_date_time(strtotime($line["last_updated"]));
2875 $feed_title = $line["feed_title"];
2877 if ($cur_feed_title != $feed_title) {
2878 $cur_feed_title = $feed_title;
2880 $tmp .= "$feed_title\n\n";
2883 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2884 $tmp .= " " . trim($line["link"]) . "\n";
2889 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
2890 DIGEST_HOSTNAME
. "\n".
2891 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
2894 return array($tmp, $headlines_count);
2897 function check_for_update($link, $brief_fmt = true) {
2898 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2900 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2905 $rss = fetch_rss($releases_feed);
2906 error_reporting (DEFAULT_ERROR_LEVEL
);
2910 $items = $rss->items
;
2912 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2913 if (!$items ||
!is_array($items)) $items = $rss;
2915 if (!is_array($items) ||
count($items) == 0) {
2919 $latest_item = $items[0];
2921 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2923 $release_url = sanitize_rss($link, $latest_item["link"]);
2924 $content = sanitize_rss($link, $latest_item["description"]);
2926 if (version_compare(VERSION
, $latest_version) == -1) {
2928 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
2929 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2930 <div id=\"milestoneDetails\">$content</div>");
2932 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2933 <div class='milestoneDetails'>$content</div>
2934 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2935 download and update information.";
2942 function markArticlesById($link, $ids, $cmode) {
2946 foreach ($ids as $id) {
2947 array_push($tmp_ids, "ref_id = '$id'");
2950 $ids_qpart = join(" OR ", $tmp_ids);
2953 db_query($link, "UPDATE ttrss_user_entries SET
2954 marked = false,last_read = NOW()
2955 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2956 } else if ($cmode == 1) {
2957 db_query($link, "UPDATE ttrss_user_entries SET
2959 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2961 db_query($link, "UPDATE ttrss_user_entries SET
2962 marked = NOT marked,last_read = NOW()
2963 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2967 function publishArticlesById($link, $ids, $cmode) {
2971 foreach ($ids as $id) {
2972 array_push($tmp_ids, "ref_id = '$id'");
2975 $ids_qpart = join(" OR ", $tmp_ids);
2978 db_query($link, "UPDATE ttrss_user_entries SET
2979 published = false,last_read = NOW()
2980 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2981 } else if ($cmode == 1) {
2982 db_query($link, "UPDATE ttrss_user_entries SET
2984 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2986 db_query($link, "UPDATE ttrss_user_entries SET
2987 published = NOT published,last_read = NOW()
2988 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2992 function catchupArticlesById($link, $ids, $cmode) {
2996 foreach ($ids as $id) {
2997 array_push($tmp_ids, "ref_id = '$id'");
3000 $ids_qpart = join(" OR ", $tmp_ids);
3003 db_query($link, "UPDATE ttrss_user_entries SET
3004 unread = false,last_read = NOW()
3005 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3006 } else if ($cmode == 1) {
3007 db_query($link, "UPDATE ttrss_user_entries SET
3009 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3011 db_query($link, "UPDATE ttrss_user_entries SET
3012 unread = NOT unread,last_read = NOW()
3013 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3017 function catchupArticleById($link, $id, $cmode) {
3020 db_query($link, "UPDATE ttrss_user_entries SET
3021 unread = false,last_read = NOW()
3022 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3023 } else if ($cmode == 1) {
3024 db_query($link, "UPDATE ttrss_user_entries SET
3026 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3028 db_query($link, "UPDATE ttrss_user_entries SET
3029 unread = NOT unread,last_read = NOW()
3030 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3034 function make_guid_from_title($title) {
3035 return preg_replace("/[ \"\',.:;]/", "-",
3036 mb_strtolower(strip_tags($title), 'utf-8'));
3039 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3040 $bottom = false, $rtl_content = false, $feed_id = 0,
3041 $is_cat = false, $search = false, $match_on = false,
3042 $search_mode = false, $offset = 0, $limit = 0) {
3044 $user_page_offset = $offset +
1;
3047 $class = "headlinesSubToolbar";
3048 $tid = "headlineActionsTop";
3050 $class = "headlinesSubToolbar";
3051 $tid = "headlineActionsBottom";
3054 print "<table class=\"$class\" id=\"$tid\"
3055 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3063 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3064 $page_next_link = "javascript:viewFeedGoPage(1)";
3065 $page_first_link = "javascript:viewFeedGoPage(0)";
3067 $catchup_page_link = "javascript:catchupPage()";
3068 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3070 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3072 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3073 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3074 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3076 $tog_unread_link = "javascript:selectionToggleUnread()";
3077 $tog_marked_link = "javascript:selectionToggleMarked()";
3078 $tog_published_link = "javascript:selectionTogglePublished()";
3082 $sel_all_link = "javascript:cdmSelectArticles('all')";
3083 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3084 $sel_none_link = "javascript:cdmSelectArticles('none')";
3086 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3087 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3088 $tog_published_link = "javascript:selectionTogglePublished(true)";
3092 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3094 print "<td class=\"headlineActions$rtl_cpart\">
3095 <ul class=\"headlineDropdownMenu\">
3098 <a href=\"$sel_all_link\">".__('All')."</a>,
3099 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3100 <a href=\"$sel_none_link\">".__('None')."</a></li>
3101 <li class=\"vsep\"> </li>
3102 <li class=\"top\">Toggle<ul>
3103 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3104 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3105 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3107 <li class=\"vsep\"> </li>
3108 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
3109 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
3110 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3113 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3115 if ($limit != 0 && !$search && $enable_pagination) {
3117 <li class=\"vsep\"> </li>
3118 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3119 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3120 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3124 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3126 <li class=\"vsep\"> </li>
3128 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3129 '$match_on', '$feed_id', '$is_cat');\">
3130 ".__('Convert to label')."</a></td>";
3136 // old style subtoolbar:
3138 print "<td class=\"headlineActions$rtl_cpart\">".
3140 <a href=\"$sel_all_link\">".__('All')."</a>,
3141 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3142 <a href=\"$sel_none_link\">".__('None')."</a>
3144 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3145 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3147 __('Mark as read:')."
3148 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3149 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3151 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3154 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3155 '$match_on', '$feed_id', '$is_cat');\">
3156 ".__('Convert to label')."</a>";
3163 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3164 print "<td class=\"headlineActions$rtl_cpart\">
3165 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3166 '$match_on', '$feed_id', '$is_cat');\">
3167 ".__('Convert to Label')."</a></td>";
3170 print "<td class=\"headlineTitle$rtl_cpart\">";
3172 if ($feed_site_url) {
3174 $target = "target=\"_blank\"";
3176 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
3182 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3185 if ($user_page_offset > 1) {
3186 print " [$user_page_offset] ";
3192 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3193 <img class=\"noborder\"
3194 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3199 print "</tr></table>";
3203 function outputFeedList($link, $tags = false) {
3205 print "<ul class=\"feedList\" id=\"feedList\">";
3207 $owner_uid = $_SESSION["uid"];
3211 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3212 print "<li class=\"feedCat\">".__('Special')."</li>";
3213 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3216 $num_starred = getFeedUnread($link, -1);
3217 $num_published = getFeedUnread($link, -2);
3221 if ($num_starred > 0) $class .= "Unread";
3223 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3224 "images/mark_set.png", $link);
3228 if ($num_published > 0) $class .= "Unread";
3230 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3231 "images/pub_set.png", $link);
3233 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3239 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3241 $result = db_query($link, "SELECT id,sql_exp,description FROM
3242 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3244 if (db_num_rows($result) > 0) {
3245 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3246 print "<li class=\"feedCat\">".__('Labels')."</li>";
3247 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3249 print "<li><hr></li>";
3253 while ($line = db_fetch_assoc($result)) {
3255 error_reporting (0);
3257 $label_id = -$line['id'] - 11;
3258 $count = getFeedUnread($link, $label_id);
3266 error_reporting (DEFAULT_ERROR_LEVEL
);
3268 printFeedEntry($label_id,
3269 $class, $line["description"],
3270 $count, "images/label.png", $link);
3274 if (db_num_rows($result) > 0) {
3275 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3282 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3283 print "<li><hr></li>";
3286 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3287 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3288 $order_by_qpart = "category,unread DESC,title";
3290 $order_by_qpart = "category,title";
3293 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3294 $order_by_qpart = "unread DESC,title";
3296 $order_by_qpart = "title";
3300 $result = db_query($link, "SELECT ttrss_feeds.*,
3301 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3302 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3303 WHERE feed_id = ttrss_feeds.id AND unread = true
3304 AND ttrss_user_entries.ref_id = ttrss_entries.id
3305 AND owner_uid = '$owner_uid') as unread,
3307 ttrss_feed_categories.title AS category,
3308 ttrss_feed_categories.collapsed
3309 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3310 ON (ttrss_feed_categories.id = cat_id)
3312 ttrss_feeds.hidden = false AND
3313 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3314 ORDER BY $order_by_qpart");
3316 $actid = $_GET["actid"];
3326 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3328 while ($line = db_fetch_assoc($result)) {
3330 $feed = trim($line["title"]);
3332 if (!$feed) $feed = "[Untitled]";
3334 $feed_id = $line["id"];
3336 $subop = $_GET["subop"];
3338 $unread = $line["unread"];
3340 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3341 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3343 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3346 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3349 $rtl_tag = "dir=\"RTL\"";
3354 $tmp_result = db_query($link,
3355 "SELECT id,COUNT(unread) AS unread
3356 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3357 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3358 WHERE parent_feed = '$feed_id' AND unread = true
3359 GROUP BY ttrss_feeds.id");
3361 if (db_num_rows($tmp_result) > 0) {
3362 while ($l = db_fetch_assoc($tmp_result)) {
3363 $unread +
= $l["unread"];
3367 $cat_id = $line["cat_id"];
3369 $tmp_category = $line["category"];
3371 if (!$tmp_category) {
3372 $tmp_category = __("Uncategorized");
3375 // $class = ($lnum % 2) ? "even" : "odd";
3377 if ($line["last_error"]) {
3383 if ($unread > 0) $class .= "Unread";
3385 if ($actid == $feed_id) {
3386 $class .= "Selected";
3389 $total_unread +
= $unread;
3391 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3397 $category = $tmp_category;
3399 $collapsed = $line["collapsed"];
3401 // workaround for NULL category
3402 if ($category == __("Uncategorized")) {
3403 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3408 if ($collapsed == "t" ||
$collapsed == "1") {
3409 $holder_class = "invisible";
3412 $holder_class = "feedCatHolder";
3416 $cat_id = sprintf("%d", $cat_id);
3418 $cat_unread = getCategoryUnread($link, $cat_id);
3420 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3422 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3423 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3424 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3425 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3426 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3429 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3430 // -> keyboard navigation, etc.
3431 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3434 printFeedEntry($feed_id, $class, $feed, $unread,
3435 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
3436 $last_updated, $line["last_error"]);
3441 if (db_num_rows($result) == 0) {
3442 print "<li>".__('No feeds to display.')."</li>";
3449 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3450 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3451 post_int_id = ttrss_user_entries.int_id AND
3452 unread = true AND ref_id = ttrss_entries.id
3453 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3455 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3456 ORDER BY tag_name"); */
3458 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3459 print "<li class=\"feedCat\">".__('Tags')."</li>";
3460 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3463 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3464 FROM ttrss_user_entries WHERE int_id = post_int_id
3465 AND unread = true)) AS count FROM ttrss_tags
3466 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
3470 while ($line = db_fetch_assoc($result)) {
3471 $tags[$line["tag_name"]] +
= $line["count"];
3474 foreach (array_keys($tags) as $tag) {
3476 $unread = $tags[$tag];
3484 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3488 if (db_num_rows($result) == 0) {
3489 print "<li>No tags to display.</li>";
3492 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3502 function get_article_tags($link, $id) {
3504 $a_id = db_escape_string($id);
3506 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3507 owner_uid as owner FROM
3508 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3509 ref_id = '$a_id' LIMIT 1) ORDER BY tag_name");
3513 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3514 array_push($tags, $tmp_line["tag_name"]);
3520 function trim_value(&$value) {
3521 $value = trim($value);
3524 function trim_array($array) {
3526 array_walk($tmp, 'trim_value');
3530 function tag_is_valid($tag) {
3531 if ($tag == '') return false;
3532 if (preg_match("/^[0-9]*$/", $tag)) return false;
3534 $tag = iconv("utf-8", "utf-8", $tag);
3535 if (!$tag) return false;
3540 function render_login_form($link, $mobile = false) {
3542 require_once "login_form.php";
3544 require_once "mobile/login_form.php";
3548 // from http://developer.apple.com/internet/safari/faq.html
3549 function no_cache_incantation() {
3550 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3551 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3552 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3553 header("Cache-Control: post-check=0, pre-check=0", false);
3554 header("Pragma: no-cache"); // HTTP/1.0
3557 function format_warning($msg, $id = "") {
3558 return "<div class=\"warning\" id=\"$id\">
3559 <img src=\"images/sign_excl.png\">$msg</div>";
3562 function format_notice($msg) {
3563 return "<div class=\"notice\">
3564 <img src=\"images/sign_info.png\">$msg</div>";
3567 function format_error($msg) {
3568 return "<div class=\"error\">
3569 <img src=\"images/sign_excl.png\">$msg</div>";
3572 function print_notice($msg) {
3573 return print format_notice($msg);
3576 function print_warning($msg) {
3577 return print format_warning($msg);
3580 function print_error($msg) {
3581 return print format_error($msg);
3585 function T_sprintf() {
3586 $args = func_get_args();
3587 return vsprintf(__(array_shift($args)), $args);
3590 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3592 /* we can figure out feed_id from article id anyway, why do we
3593 * pass feed_id here? */
3595 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3596 WHERE ref_id = '$id'");
3598 $feed_id = db_fetch_result($result, 0, "feed_id");
3600 print "<article id='$id'><![CDATA[";
3602 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3603 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3605 if (db_num_rows($result) == 1) {
3606 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3608 $rtl_content = false;
3612 $rtl_tag = "dir=\"RTL\"";
3619 if ($mark_as_read) {
3620 $result = db_query($link, "UPDATE ttrss_user_entries
3621 SET unread = false,last_read = NOW()
3622 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3625 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3626 SUBSTRING(updated,1,16) as updated,
3627 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3630 FROM ttrss_entries,ttrss_user_entries
3631 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3637 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3638 $link_target = "target=\"_new\"";
3641 $line = db_fetch_assoc($result);
3643 if ($line["icon_url"]) {
3644 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3646 $feed_icon = " ";
3649 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
3650 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
3652 $entry_comments = "";
3655 $num_comments = $line["num_comments"];
3656 $entry_comments = "";
3658 if ($num_comments > 0) {
3659 if ($line["comments"]) {
3660 $comments_url = $line["comments"];
3662 $comments_url = $line["link"];
3664 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
3666 if ($line["comments"] && $line["link"] != $line["comments"]) {
3667 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
3671 print "<div class=\"postReply\">";
3673 print "<div class=\"postHeader\">";
3675 $entry_author = $line["author"];
3677 if ($entry_author) {
3678 $entry_author = __(" - by ") . $entry_author;
3681 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
3682 strtotime($line["updated"]));
3684 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3686 if ($line["link"]) {
3687 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
3688 $line["title"] . "</a>$entry_author</div>";
3690 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3693 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3694 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
3695 ORDER BY tag_name");
3702 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3704 $tag = $tmp_line["tag_name"];
3705 $tag_escaped = str_replace("'", "\\'", $tag);
3707 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
3709 if ($num_tags == 6) {
3710 $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
3711 } else if ($num_tags < 6) {
3712 $tags_str .= $tag_str;
3714 $f_tags_str .= $tag_str;
3717 $tags_str = preg_replace("/, $/", "", $tags_str);
3718 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
3720 if (!$entry_comments) $entry_comments = " "; # placeholder
3722 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
3724 print "<div style='float : right'>$tags_str
3725 <a title=\"Edit tags for this article\"
3726 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
3727 <div clear='both'>$entry_comments</div>";
3731 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
3732 print "<div class=\"postContent\">";
3734 if (db_num_rows($tmp_result) > 0) {
3735 print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
3738 $line["content"] = sanitize_rss($link, $line["content"]);
3740 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3741 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
3744 print $line["content"] . "</div>";
3750 print "]]></article>";
3754 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
3755 $next_unread_feed, $offset) {
3757 $timing_info = getmicrotime();
3759 $topmost_article_ids = array();
3765 if ($subop == "undefined") $subop = "";
3767 if ($subop == "CatchupSelected") {
3768 $ids = split(",", db_escape_string($_GET["ids"]));
3769 $cmode = sprintf("%d", $_GET["cmode"]);
3771 catchupArticlesById($link, $ids, $cmode);
3774 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
3775 update_generic_feed($link, $feed, $cat_view);
3778 if ($subop == "MarkAllRead") {
3779 catchup_feed($link, $feed, $cat_view);
3781 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
3782 if ($next_unread_feed) {
3783 $feed = $next_unread_feed;
3789 $result = db_query($link,
3790 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
3792 if (db_num_rows($result) == 0) {
3793 print "<div align='center'>".__('Feed not found.')."</div>";
3798 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3800 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3801 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
3803 if (db_num_rows($result) == 1) {
3804 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3806 $rtl_content = false;
3810 $rtl_tag = "dir=\"RTL\"";
3816 $rtl_content = false;
3819 $script_dt_add = get_script_dt_add();
3821 /// START /////////////////////////////////////////////////////////////////////////////////
3823 $search = db_escape_string($_GET["query"]);
3824 $search_mode = db_escape_string($_GET["search_mode"]);
3825 $match_on = db_escape_string($_GET["match_on"]);
3831 $real_offset = $offset * $limit;
3833 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
3835 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
3836 $search, $search_mode, $match_on, false, $real_offset);
3838 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
3840 $result = $qfh_ret[0];
3841 $feed_title = $qfh_ret[1];
3842 $feed_site_url = $qfh_ret[2];
3843 $last_error = $qfh_ret[3];
3845 /// STOP //////////////////////////////////////////////////////////////////////////////////
3848 print "<div id=\"headlinesContainer\" $rtl_tag>";
3851 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
3855 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
3856 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
3859 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
3862 if (db_num_rows($result) > 0) {
3864 # print "\{$offset}";
3866 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3867 print "<table class=\"headlinesList\" id=\"headlinesList\"
3868 cellspacing=\"0\">";
3873 error_reporting (DEFAULT_ERROR_LEVEL
);
3877 while ($line = db_fetch_assoc($result)) {
3879 $class = ($lnum %
2) ?
"even" : "odd";
3882 $feed_id = $line["feed_id"];
3884 if (count($topmost_article_ids) < 5) {
3885 array_push($topmost_article_ids, $id);
3888 if ($line["last_read"] == "" &&
3889 ($line["unread"] != "t" && $line["unread"] != "1")) {
3891 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
3894 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
3898 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
3906 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
3907 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\"
3909 alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
3911 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\"
3913 alt=\"Set mark\" onclick='javascript:tMark($id)'>";
3916 if ($line["published"] == "t" ||
$line["published"] == "1") {
3917 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\"
3919 alt=\"Unpublish\" onclick='javascript:tPub($id)'>";
3921 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\"
3923 alt=\"Publish\" onclick='javascript:tPub($id)'>";
3926 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
3927 # $line["title"] . "</a>";
3929 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
3930 $line["title"] . "</a>";
3932 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
3933 # $line["title"] . "</a>";
3935 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3936 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3938 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3939 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3942 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
3943 $content_preview = truncate_string(strip_tags($line["content_preview"]),
3947 $entry_author = $line["author"];
3949 if ($entry_author) {
3950 $entry_author = " - by $entry_author";
3953 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3955 print "<tr class='$class' id='RROW-$id'>";
3957 print "<td class='hlUpdPic'>$update_pic</td>";
3959 print "<td class='hlSelectRow'>
3960 <input type=\"checkbox\" onclick=\"tSR(this)\"
3964 print "<td class='hlMarkedPic'>$marked_pic</td>";
3965 print "<td class='hlMarkedPic'>$published_pic</td>";
3967 if ($line["feed_title"]) {
3968 print "<td class='hlContent'>$content_link</td>";
3969 print "<td class='hlFeed'>
3970 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
3971 $line["feed_title"]."</a> </td>";
3973 print "<td class='hlContent' valign='middle'>";
3975 print "<a href=\"javascript:view($id,$feed_id);\">" .
3978 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
3979 if ($content_preview) {
3980 print "<span class=\"contentPreview\"> - $content_preview</span>";
3988 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
3995 $add_class = "Unread";
4000 print "<div class=\"cdmArticle$add_class\"
4001 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4002 onmouseout='cdmMouseOut(this)'>";
4004 print "<div class=\"cdmHeader\">";
4006 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4008 print "<a class=\"title\"
4009 onclick=\"javascript:toggleUnread($id, 0)\"
4010 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4012 print $entry_author;
4014 if ($line["feed_title"]) {
4015 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4020 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4022 print "<div class=\"cdmFooter\"><span class='s0'>";
4024 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4026 print __("Select:").
4027 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4028 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4030 print "</span><span class='s1'>$marked_pic</span> ";
4031 print "<span class='s1'>$published_pic</span> ";
4033 $tags = get_article_tags($link, $id);
4037 foreach ($tags as $tag) {
4039 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4042 $tags_str = preg_replace("/, $/", "", $tags_str);
4044 if ($tags_str == "") $tags_str = "no tags";
4046 // print "<img src='images/tag.png' class='markedPic'>";
4048 print "<span class='s1'>Tags: $tags_str <a title=\"Edit tags for this article\"
4049 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4053 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4054 href=\"javascript:toggleUnread($id)\">
4065 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4069 // print_headline_subtoolbar($link,
4070 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4074 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
4082 return $topmost_article_ids;
4085 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
4087 function printTagCloud($link) {
4089 /* get first ref_id to count from */
4095 if (DB_TYPE == "pgsql") {
4096 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4097 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4098 AND date_entered > NOW() - INTERVAL '30 days'";
4100 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4101 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4102 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4105 $result = db_query($link, $query);
4106 $first_id = db_fetch_result($result, 0, "id"); */
4108 //AND post_int_id >= '$first_id'
4109 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4110 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
4111 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
4113 $result = db_query($link, $query);
4117 while ($line = db_fetch_assoc($result)) {
4118 $tags[$line["tag_name"]] = $line["count"];
4123 $max_size = 32; // max font size in pixels
4124 $min_size = 11; // min font size in pixels
4126 // largest and smallest array values
4127 $max_qty = max(array_values($tags));
4128 $min_qty = min(array_values($tags));
4130 // find the range of values
4131 $spread = $max_qty - $min_qty;
4132 if ($spread == 0) { // we don't want to divide by zero
4136 // set the font-size increment
4137 $step = ($max_size - $min_size) / ($spread);
4139 // loop through the tag array
4140 foreach ($tags as $key => $value) {
4141 // calculate font-size
4142 // find the $value in excess of $min_qty
4143 // multiply by the font-size increment ($size)
4144 // and add the $min_size set above
4145 $size = round($min_size +
(($value - $min_qty) * $step));
4147 $key_escaped = str_replace("'", "\\'", $key);
4149 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
4150 $size . "px\" title=\"$value articles tagged with " .
4151 $key . '">' . $key . '</a> ';
4155 function print_checkpoint($n, $s) {
4156 $ts = getmicrotime();
4157 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4161 function sanitize_tag($tag) {
4164 $tag = mb_strtolower($tag, 'utf-8');
4166 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4168 // $tag = str_replace('"', "", $tag);
4169 // $tag = str_replace("+", " ", $tag);
4170 $tag = str_replace("technorati tag: ", "", $tag);
4175 function generate_publish_key() {
4176 return sha1(uniqid(rand(), true));