]>
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 "accept-to-gettext.php";
10 require_once "gettext/gettext.inc";
12 require_once 'config.php';
14 function startup_gettext() {
16 # Get locale from Accept-Language header
17 $lang = al2gt(array("en_US", "ru_RU"), "text/html");
20 _setlocale(LC_MESSAGES
, $lang);
21 _bindtextdomain("messages", "locale");
22 _textdomain("messages");
23 _bind_textdomain_codeset("messages", "UTF-8");
27 if (ENABLE_TRANSLATIONS
== true) {
31 require_once 'db-prefs.php';
32 require_once 'compat.php';
33 require_once 'errors.php';
34 require_once 'version.php';
36 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
37 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
39 require_once "magpierss/rss_fetch.inc";
40 require_once 'magpierss/rss_utils.inc';
42 function _debug($msg) {
43 $ts = strftime("%H:%M:%S", time());
47 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
51 if (DB_TYPE
== "pgsql") {
52 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
53 marked = false AND feed_id = '$feed_id' AND
54 (SELECT date_entered FROM ttrss_entries WHERE
55 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
57 $pg_version = get_pgsql_version($link);
59 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
61 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
62 ttrss_entries.id = ref_id AND
64 feed_id = '$feed_id' AND
65 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
69 $result = db_query($link, "DELETE FROM ttrss_user_entries
71 WHERE ttrss_entries.id = ref_id AND
73 feed_id = '$feed_id' AND
74 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
77 $rows = pg_affected_rows($result);
81 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
82 marked = false AND feed_id = '$feed_id' AND
83 (SELECT date_entered FROM ttrss_entries WHERE
84 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
86 $result = db_query($link, "DELETE FROM ttrss_user_entries
87 USING ttrss_user_entries, ttrss_entries
88 WHERE ttrss_entries.id = ref_id AND
90 feed_id = '$feed_id' AND
91 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
93 $rows = mysql_affected_rows($link);
98 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
102 function global_purge_old_posts($link, $do_output = false, $limit = false) {
104 $random_qpart = sql_random_function();
107 $limit_qpart = "LIMIT $limit";
112 $result = db_query($link,
113 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
114 ORDER BY $random_qpart $limit_qpart");
116 while ($line = db_fetch_assoc($result)) {
118 $feed_id = $line["id"];
119 $purge_interval = $line["purge_interval"];
120 $owner_uid = $line["owner_uid"];
122 if ($purge_interval == 0) {
124 $tmp_result = db_query($link,
125 "SELECT value FROM ttrss_user_prefs WHERE
126 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
128 if (db_num_rows($tmp_result) != 0) {
129 $purge_interval = db_fetch_result($tmp_result, 0, "value");
134 // print "Feed $feed_id: purge interval = $purge_interval\n";
137 if ($purge_interval > 0) {
138 purge_feed($link, $feed_id, $purge_interval, $do_output);
142 // purge orphaned posts in main content table
143 db_query($link, "DELETE FROM ttrss_entries WHERE
144 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
148 function purge_old_posts($link) {
150 $user_id = $_SESSION["uid"];
152 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
153 WHERE owner_uid = '$user_id'");
155 while ($line = db_fetch_assoc($result)) {
157 $feed_id = $line["id"];
158 $purge_interval = $line["purge_interval"];
160 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
162 if ($purge_interval > 0) {
163 purge_feed($link, $feed_id, $purge_interval);
167 // purge orphaned posts in main content table
168 db_query($link, "DELETE FROM ttrss_entries WHERE
169 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
172 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
174 if (WEB_DEMO_MODE
) return;
177 $user_id = $_SESSION["uid"];
178 purge_old_posts($link);
181 // db_query($link, "BEGIN");
183 if (MAX_UPDATE_TIME
> 0) {
184 if (DB_TYPE
== "mysql") {
187 $q_order = "RANDOM()";
190 $q_order = "last_updated DESC";
193 $result = db_query($link, "SELECT feed_url,id,
194 SUBSTRING(last_updated,1,19) AS last_updated,
195 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
200 while ($line = db_fetch_assoc($result)) {
201 $upd_intl = $line["update_interval"];
203 if (!$upd_intl ||
$upd_intl == 0) {
204 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
208 // Updates for this feed are disabled
212 if ($fetch ||
(!$line["last_updated"] ||
213 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
215 // print "<!-- feed: ".$line["feed_url"]." -->";
217 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
219 $upd_elapsed = time() - $upd_start;
221 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
227 // db_query($link, "COMMIT");
231 function fetch_file_contents($url) {
232 if (USE_CURL_FOR_ICONS
) {
233 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
235 $ch = curl_init($url);
236 $fp = fopen($tmpfile, "w");
239 curl_setopt($ch, CURLOPT_FILE
, $fp);
240 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
241 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
247 $contents = file_get_contents($tmpfile);
253 return file_get_contents($url);
258 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
259 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
261 function get_favicon_url($url) {
263 if ($html = @fetch_file_contents
($url)) {
265 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
266 // Attempt to grab a favicon link from their webpage url
267 $linkUrl = html_entity_decode($matches[1]);
269 if (substr($linkUrl, 0, 1) == '/') {
270 $urlParts = parse_url($url);
271 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
272 } else if (substr($linkUrl, 0, 7) == 'http://') {
273 $faviconURL = $linkUrl;
274 } else if (substr($url, -1, 1) == '/') {
275 $faviconURL = $url.$linkUrl;
277 $faviconURL = $url.'/'.$linkUrl;
281 // If unsuccessful, attempt to "guess" the favicon location
282 $urlParts = parse_url($url);
283 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
287 // Run a test to see if what we have attempted to get actually exists.
288 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
295 function url_validate($link) {
297 $url_parts = @parse_url
($link);
299 if ( empty( $url_parts["host"] ) )
302 if ( !empty( $url_parts["path"] ) ) {
303 $documentpath = $url_parts["path"];
308 if ( !empty( $url_parts["query"] ) )
309 $documentpath .= "?" . $url_parts["query"];
311 $host = $url_parts["host"];
312 $port = $url_parts["port"];
317 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
322 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
324 $http_response = fgets( $socket, 22 );
326 $responses = "/(200 OK)|(30[0-9] Moved)/";
327 if ( preg_match($responses, $http_response) ) {
336 function check_feed_favicon($site_url, $feed, $link) {
337 $favicon_url = get_favicon_url($site_url);
339 # print "FAVICON [$site_url]: $favicon_url\n";
343 $icon_file = ICONS_DIR
. "/$feed.ico";
345 if ($favicon_url && !file_exists($icon_file)) {
346 $contents = fetch_file_contents($favicon_url);
348 $fp = fopen($icon_file, "w");
351 fwrite($fp, $contents);
353 chmod($icon_file, 0644);
357 error_reporting(DEFAULT_ERROR_LEVEL
);
361 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
363 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
367 if (defined('DAEMON_EXTENDED_DEBUG')) {
368 _debug("update_rss_feed: start");
371 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
372 FROM ttrss_feeds WHERE id = '$feed'");
374 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
375 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
377 $update_interval = db_fetch_result($result, 0, "update_interval");
379 if ($update_interval < 0) { return; }
381 $feed = db_escape_string($feed);
383 $fetch_url = $feed_url;
385 if ($auth_login && $auth_pass) {
386 $url_parts = array();
387 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
389 if ($url_parts[1] && $url_parts[2]) {
390 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
395 if (defined('DAEMON_EXTENDED_DEBUG')) {
396 _debug("update_rss_feed: fetching...");
399 if (!defined('DAEMON_EXTENDED_DEBUG')) {
403 $rss = fetch_rss($fetch_url);
405 if (defined('DAEMON_EXTENDED_DEBUG')) {
406 _debug("update_rss_feed: fetch done, parsing...");
408 error_reporting (DEFAULT_ERROR_LEVEL
);
411 $feed = db_escape_string($feed);
415 if (defined('DAEMON_EXTENDED_DEBUG')) {
416 _debug("update_rss_feed: processing feed data...");
419 // db_query($link, "BEGIN");
421 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
422 FROM ttrss_feeds WHERE id = '$feed'");
424 $registered_title = db_fetch_result($result, 0, "title");
425 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
426 $orig_site_url = db_fetch_result($result, 0, "site_url");
428 $owner_uid = db_fetch_result($result, 0, "owner_uid");
430 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
431 if (defined('DAEMON_EXTENDED_DEBUG')) {
432 _debug("update_rss_feed: checking favicon...");
434 check_feed_favicon($rss->channel
["link"], $feed, $link);
437 if (!$registered_title ||
$registered_title == "[Unknown]") {
439 $feed_title = db_escape_string($rss->channel
["title"]);
441 db_query($link, "UPDATE ttrss_feeds SET
442 title = '$feed_title' WHERE id = '$feed'");
445 $site_url = $rss->channel
["link"];
446 // weird, weird Magpie
447 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
449 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
450 db_query($link, "UPDATE ttrss_feeds SET
451 site_url = '$site_url' WHERE id = '$feed'");
454 // print "I: " . $rss->channel["image"]["url"];
456 $icon_url = $rss->image
["url"];
458 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
459 $icon_url = db_escape_string($icon_url);
460 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
463 if (defined('DAEMON_EXTENDED_DEBUG')) {
464 _debug("update_rss_feed: loading filters...");
469 $result = db_query($link, "SELECT reg_exp,
470 ttrss_filter_types.name AS name,
471 ttrss_filter_actions.name AS action,
474 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
476 owner_uid = $owner_uid AND
477 ttrss_filter_types.id = filter_type AND
478 ttrss_filter_actions.id = action_id AND
479 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
481 while ($line = db_fetch_assoc($result)) {
482 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
484 $filter["reg_exp"] = $line["reg_exp"];
485 $filter["action"] = $line["action"];
486 $filter["action_param"] = $line["action_param"];
487 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
489 array_push($filters[$line["name"]], $filter);
492 $iterator = $rss->items
;
494 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
495 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
497 if (!is_array($iterator)) {
498 /* db_query($link, "UPDATE ttrss_feeds
499 SET last_error = 'Parse error: can\'t find any articles.'
500 WHERE id = '$feed'"); */
502 // clear any errors and mark feed as updated if fetched okay
503 // even if it's blank
505 db_query($link, "UPDATE ttrss_feeds
506 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
508 return; // no articles
511 if (defined('DAEMON_EXTENDED_DEBUG')) {
512 _debug("update_rss_feed: processing articles...");
515 foreach ($iterator as $item) {
517 $entry_guid = $item["id"];
519 if (!$entry_guid) $entry_guid = $item["guid"];
520 if (!$entry_guid) $entry_guid = $item["link"];
521 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
523 if (defined('DAEMON_EXTENDED_DEBUG')) {
524 _debug("update_rss_feed: guid $entry_guid");
527 if (!$entry_guid) continue;
529 $entry_timestamp = "";
531 $rss_2_date = $item['pubdate'];
532 $rss_1_date = $item['dc']['date'];
533 $atom_date = $item['issued'];
534 if (!$atom_date) $atom_date = $item['updated'];
536 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
537 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
538 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
540 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
541 $entry_timestamp = time();
542 $no_orig_date = 'true';
544 $no_orig_date = 'false';
547 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
549 $entry_title = trim(strip_tags($item["title"]));
551 // strange Magpie workaround
552 $entry_link = $item["link_"];
553 if (!$entry_link) $entry_link = $item["link"];
555 if (!$entry_title) continue;
556 # if (!$entry_link) continue;
558 $entry_link = strip_tags($entry_link);
560 $entry_content = $item["content:escaped"];
562 if (!$entry_content) $entry_content = $item["content:encoded"];
563 if (!$entry_content) $entry_content = $item["content"];
564 if (!$entry_content) $entry_content = $item["atom_content"];
565 if (!$entry_content) $entry_content = $item["summary"];
566 if (!$entry_content) $entry_content = $item["description"];
568 // if (!$entry_content) continue;
571 if (is_array($entry_content)) {
572 $entry_content = $entry_content["encoded"];
573 if (!$entry_content) $entry_content = $entry_content["escaped"];
577 // print_r(htmlspecialchars($entry_content));
580 $entry_content_unescaped = $entry_content;
581 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
583 $entry_comments = strip_tags($item["comments"]);
585 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
587 if ($item['author']) {
589 if (is_array($item['author'])) {
591 if (!$entry_author) {
592 $entry_author = db_escape_string(strip_tags($item['author']['name']));
595 if (!$entry_author) {
596 $entry_author = db_escape_string(strip_tags($item['author']['email']));
600 if (!$entry_author) {
601 $entry_author = db_escape_string(strip_tags($item['author']));
605 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
607 $entry_guid = db_escape_string(strip_tags($entry_guid));
609 $result = db_query($link, "SELECT id FROM ttrss_entries
610 WHERE guid = '$entry_guid'");
612 $entry_content = db_escape_string($entry_content);
613 $entry_title = db_escape_string($entry_title);
614 $entry_link = db_escape_string($entry_link);
615 $entry_comments = db_escape_string($entry_comments);
617 $num_comments = db_escape_string($item["slash"]["comments"]);
619 if (!$num_comments) $num_comments = 0;
621 // parse <category> entries into tags
623 $t_ctr = $item['category#'];
625 $additional_tags = false;
628 $additional_tags = false;
629 } else if ($t_ctr == 1) {
630 $additional_tags = array($item['category']);
632 $additional_tags = array();
633 for ($i = 0; $i <= $t_ctr; $i++
) {
634 if ($item["category#$i"]) {
635 array_push($additional_tags, $item["category#$i"]);
640 // parse <dc:subject> elements
642 $t_ctr = $item['dc']['subject#'];
645 $additional_tags = array($item['dc']['subject']);
646 } else if ($t_ctr > 1) {
647 $additional_tags = array();
648 for ($i = 0; $i <= $t_ctr; $i++
) {
649 if ($item['dc']["subject#$i"]) {
650 array_push($additional_tags, $item['dc']["subject#$i"]);
657 // $entry_content = sanitize_rss($entry_content);
659 if (defined('DAEMON_EXTENDED_DEBUG')) {
660 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
663 db_query($link, "BEGIN");
665 if (db_num_rows($result) == 0) {
667 if (defined('DAEMON_EXTENDED_DEBUG')) {
668 _debug("update_rss_feed: base guid not found");
671 // base post entry does not exist, create it
673 $result = db_query($link,
674 "INSERT INTO ttrss_entries
690 '$entry_timestamp_fmt',
699 // we keep encountering the entry in feeds, so we need to
700 // update date_entered column so that we don't get horrible
701 // dupes when the entry gets purged and reinserted again e.g.
702 // in the case of SLOW SLOW OMG SLOW updating feeds
704 $base_entry_id = db_fetch_result($result, 0, "id");
706 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
707 WHERE id = '$base_entry_id'");
710 // now it should exist, if not - bad luck then
712 $result = db_query($link, "SELECT
713 id,content_hash,no_orig_date,title,
714 substring(date_entered,1,19) as date_entered,
715 substring(updated,1,19) as updated,
719 WHERE guid = '$entry_guid'");
721 if (db_num_rows($result) == 1) {
723 if (defined('DAEMON_EXTENDED_DEBUG')) {
724 _debug("update_rss_feed: base guid found, checking for user record");
727 // this will be used below in update handler
728 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
729 $orig_title = db_fetch_result($result, 0, "title");
730 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
731 $orig_date_entered = strtotime(db_fetch_result($result,
734 $ref_id = db_fetch_result($result, 0, "id");
736 // check for user post link to main table
738 // do we allow duplicate posts with same GUID in different feeds?
739 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
740 $dupcheck_qpart = "AND feed_id = '$feed'";
742 $dupcheck_qpart = "";
745 // error_reporting(0);
747 $article_filters = get_article_filters($filters, $entry_title,
748 $entry_content, $entry_link);
750 if (defined('DAEMON_EXTENDED_DEBUG')) {
751 _debug("update_rss_feed: article filters: ");
752 if (count($article_filters) != 0) {
753 print_r($article_filters);
757 if (find_article_filter($article_filters, "filter")) {
761 // error_reporting (DEFAULT_ERROR_LEVEL);
763 $result = db_query($link,
764 "SELECT ref_id FROM ttrss_user_entries WHERE
765 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
768 // okay it doesn't exist - create user entry
769 if (db_num_rows($result) == 0) {
771 if (defined('DAEMON_EXTENDED_DEBUG')) {
772 _debug("update_rss_feed: user record not found, creating...");
775 if (!find_article_filter($article_filters, 'catchup')) {
777 $last_read_qpart = 'NULL';
780 $last_read_qpart = 'NOW()';
783 if (find_article_filter($article_filters, 'mark')) {
789 $result = db_query($link,
790 "INSERT INTO ttrss_user_entries
791 (ref_id, owner_uid, feed_id, unread, last_read, marked)
792 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
793 $last_read_qpart, $marked)");
796 $post_needs_update = false;
798 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
799 ($content_hash != $orig_content_hash)) {
800 $post_needs_update = true;
803 if ($orig_title != $entry_title) {
804 $post_needs_update = true;
807 if ($orig_num_comments != $num_comments) {
808 $post_needs_update = true;
811 // this doesn't seem to be very reliable
813 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
814 // $post_needs_update = true;
817 // if post needs update, update it and mark all user entries
818 // linking to this post as updated
819 if ($post_needs_update) {
821 if (defined('DAEMON_EXTENDED_DEBUG')) {
822 _debug("update_rss_feed: post $entry_guid needs update...");
825 // print "<!-- post $orig_title needs update : $post_needs_update -->";
827 db_query($link, "UPDATE ttrss_entries
828 SET title = '$entry_title', content = '$entry_content',
829 num_comments = '$num_comments'
830 WHERE id = '$ref_id'");
832 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
833 db_query($link, "UPDATE ttrss_user_entries
834 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
836 db_query($link, "UPDATE ttrss_user_entries
837 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
843 db_query($link, "COMMIT");
845 if (defined('DAEMON_EXTENDED_DEBUG')) {
846 _debug("update_rss_feed: looking for tags...");
850 // <a href="..." rel="tag">Xorg</a>, //
854 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
855 $entry_content_unescaped, $entry_tags);
857 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
858 print_r($entry_tags);
859 print "<br/></p>"; */
861 $entry_tags = $entry_tags[1];
863 # check for manual tags
865 $tag_filter = find_article_filter($article_filters, "tag");
869 $manual_tags = trim_array(split(",", $tag_filter[1]));
871 foreach ($manual_tags as $tag) {
872 if (tag_is_valid($tag)) {
873 array_push($entry_tags, $tag);
878 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
879 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
881 if ($additional_tags && is_array($additional_tags)) {
882 foreach ($additional_tags as $tag) {
883 if (tag_is_valid($tag) &&
884 array_search($tag, $boring_tags) === FALSE) {
885 array_push($entry_tags, $tag);
890 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
892 if (count($entry_tags) > 0) {
894 db_query($link, "BEGIN");
896 $result = db_query($link, "SELECT id,int_id
897 FROM ttrss_entries,ttrss_user_entries
898 WHERE guid = '$entry_guid'
899 AND feed_id = '$feed' AND ref_id = id
900 AND owner_uid = '$owner_uid'");
902 if (db_num_rows($result) == 1) {
904 $entry_id = db_fetch_result($result, 0, "id");
905 $entry_int_id = db_fetch_result($result, 0, "int_id");
907 foreach ($entry_tags as $tag) {
909 $tag = mb_strtolower($tag, 'utf-8');
910 $tag = db_escape_string($tag);
912 $tag = str_replace("+", " ", $tag);
913 $tag = str_replace("technorati tag: ", "", $tag);
915 if (!tag_is_valid($tag)) continue;
917 $result = db_query($link, "SELECT id FROM ttrss_tags
918 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
919 owner_uid = '$owner_uid' LIMIT 1");
921 // print db_fetch_result($result, 0, "id");
923 if ($result && db_num_rows($result) == 0) {
925 // print "tagging $entry_id as $tag<br>";
927 db_query($link, "INSERT INTO ttrss_tags
928 (owner_uid,tag_name,post_int_id)
929 VALUES ('$owner_uid','$tag', '$entry_int_id')");
933 db_query($link, "COMMIT");
937 db_query($link, "UPDATE ttrss_feeds
938 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
940 // db_query($link, "COMMIT");
943 $error_msg = db_escape_string(magpie_error());
945 "UPDATE ttrss_feeds SET last_error = '$error_msg',
946 last_updated = NOW() WHERE id = '$feed'");
949 if (defined('DAEMON_EXTENDED_DEBUG')) {
950 _debug("update_rss_feed: done");
955 function print_select($id, $default, $values, $attributes = "") {
956 print "<select name=\"$id\" id=\"$id\" $attributes>";
957 foreach ($values as $v) {
963 print "<option$sel>$v</option>";
968 function print_select_hash($id, $default, $values, $attributes = "") {
969 print "<select name=\"$id\" id='$id' $attributes>";
970 foreach (array_keys($values) as $v) {
976 print "<option $sel value=\"$v\">".$values[$v]."</option>";
982 function get_article_filters($filters, $title, $content, $link) {
985 if ($filters["title"]) {
986 foreach ($filters["title"] as $filter) {
987 $reg_exp = $filter["reg_exp"];
988 $inverse = $filter["inverse"];
989 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
990 ($inverse && !preg_match("/$reg_exp/i", $title))) {
992 array_push($matches, array($filter["action"], $filter["action_param"]));
997 if ($filters["content"]) {
998 foreach ($filters["content"] as $filter) {
999 $reg_exp = $filter["reg_exp"];
1000 $inverse = $filter["inverse"];
1002 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1003 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1005 array_push($matches, array($filter["action"], $filter["action_param"]));
1010 if ($filters["both"]) {
1011 foreach ($filters["both"] as $filter) {
1012 $reg_exp = $filter["reg_exp"];
1013 $inverse = $filter["inverse"];
1016 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1017 array_push($matches, array($filter["action"], $filter["action_param"]));
1020 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1021 array_push($matches, array($filter["action"], $filter["action_param"]));
1027 if ($filters["link"]) {
1028 $reg_exp = $filter["reg_exp"];
1029 foreach ($filters["link"] as $filter) {
1030 $reg_exp = $filter["reg_exp"];
1031 $inverse = $filter["inverse"];
1033 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1034 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1036 array_push($matches, array($filter["action"], $filter["action_param"]));
1044 function find_article_filter($filters, $filter_name) {
1045 foreach ($filters as $f) {
1046 if ($f[0] == $filter_name) {
1053 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1054 $rtl_content = false, $last_updated = false, $last_error = false) {
1056 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1057 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1059 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1063 $rtl_tag = "dir=\"rtl\"";
1065 $rtl_tag = "dir=\"ltr\"";
1068 $error_notify_msg = "";
1071 $link_title = "Error: $last_error ($last_updated)";
1072 $error_notify_msg = "(Error)";
1073 } else if ($last_updated) {
1074 $link_title = "Updated: $last_updated";
1077 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1078 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1080 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1081 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1085 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1090 $fctr_class = "class=\"invisible\"";
1093 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1094 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1096 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1097 print "<div class=\"feedExtInfo\">
1098 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1105 function getmicrotime() {
1106 list($usec, $sec) = explode(" ",microtime());
1107 return ((float)$usec +
(float)$sec);
1110 function print_radio($id, $default, $values, $attributes = "") {
1111 foreach ($values as $v) {
1119 $sel .= " value=\"1\"";
1121 $sel .= " value=\"0\"";
1124 print "<input class=\"noborder\"
1125 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1130 function initialize_user_prefs($link, $uid) {
1132 $uid = db_escape_string($uid);
1134 db_query($link, "BEGIN");
1136 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1138 $u_result = db_query($link, "SELECT pref_name
1139 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1141 $active_prefs = array();
1143 while ($line = db_fetch_assoc($u_result)) {
1144 array_push($active_prefs, $line["pref_name"]);
1147 while ($line = db_fetch_assoc($result)) {
1148 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1149 // print "adding " . $line["pref_name"] . "<br>";
1151 db_query($link, "INSERT INTO ttrss_user_prefs
1152 (owner_uid,pref_name,value) VALUES
1153 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1158 db_query($link, "COMMIT");
1162 function lookup_user_id($link, $user) {
1164 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1167 if (db_num_rows($result) == 1) {
1168 return db_fetch_result($result, 0, "id");
1174 function http_authenticate_user($link) {
1176 if (!$_SERVER["PHP_AUTH_USER"]) {
1178 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1179 header('HTTP/1.0 401 Unauthorized');
1183 $auth_result = authenticate_user($link,
1184 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1186 if (!$auth_result) {
1187 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1188 header('HTTP/1.0 401 Unauthorized');
1196 function authenticate_user($link, $login, $password, $force_auth = false) {
1198 if (!SINGLE_USER_MODE
) {
1200 $pwd_hash = 'SHA1:' . sha1($password);
1202 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1203 $query = "SELECT id,login,access_level
1204 FROM ttrss_users WHERE
1207 $query = "SELECT id,login,access_level
1208 FROM ttrss_users WHERE
1209 login = '$login' AND pwd_hash = '$pwd_hash'";
1212 $result = db_query($link, $query);
1214 if (db_num_rows($result) == 1) {
1215 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1216 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1217 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1219 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1222 $user_theme = get_user_theme_path($link);
1224 $_SESSION["theme"] = $user_theme;
1225 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1227 initialize_user_prefs($link, $_SESSION["uid"]);
1236 $_SESSION["uid"] = 1;
1237 $_SESSION["name"] = "admin";
1239 $user_theme = get_user_theme_path($link);
1241 $_SESSION["theme"] = $user_theme;
1242 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1244 initialize_user_prefs($link, $_SESSION["uid"]);
1250 function make_password($length = 8) {
1253 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1257 while ($i < $length) {
1258 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1260 if (!strstr($password, $char)) {
1268 // this is called after user is created to initialize default feeds, labels
1271 // user preferences are checked on every login, not here
1273 function initialize_user($link, $uid) {
1275 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1276 values ('$uid','unread = true', 'Unread articles')");
1278 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1279 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1281 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1282 values ('$uid', 'Tiny Tiny RSS: New Releases',
1283 'http://tt-rss.spb.ru/releases.rss')");
1285 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1286 values ('$uid', 'Tiny Tiny RSS: Forum',
1287 'http://tt-rss.spb.ru/forum/rss.php')");
1290 function logout_user() {
1292 if (isset($_COOKIE[session_name()])) {
1293 setcookie(session_name(), '', time()-42000, '/');
1297 function get_script_urlpath() {
1298 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1301 function validate_session($link) {
1302 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1303 if ($_SESSION["ip_address"]) {
1304 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1305 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1311 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1313 //print_r($_SESSION);
1315 if (time() > $_SESSION["cookie_lifetime"]) {
1323 function login_sequence($link, $mobile = false) {
1324 if (!SINGLE_USER_MODE
) {
1326 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1327 $swu = db_escape_string($_REQUEST["swu"]);
1329 $_SESSION["prefs_cache"] = false;
1330 return authenticate_user($link, $swu, null, true);
1334 $login_action = $_POST["login_action"];
1336 # try to authenticate user if called from login form
1337 if ($login_action == "do_login") {
1338 $login = $_POST["login"];
1339 $password = $_POST["password"];
1340 $remember_me = $_POST["remember_me"];
1342 if (authenticate_user($link, $login, $password)) {
1343 $_POST["password"] = "";
1345 header("Location: " . $_SERVER["REQUEST_URI"]);
1350 $_SESSION["login_error_msg"] = "Incorrect username or password";
1354 // print session_id();
1355 // print_r($_SESSION);
1357 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1358 render_login_form($link, $mobile);
1361 /* bump login timestamp */
1362 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1367 return authenticate_user($link, "admin", null);
1371 function truncate_string($str, $max_len) {
1372 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1373 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1379 function get_user_theme_path($link) {
1380 $result = db_query($link, "SELECT theme_path
1382 ttrss_themes,ttrss_users
1383 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1384 if (db_num_rows($result) != 0) {
1385 return db_fetch_result($result, 0, "theme_path");
1391 function smart_date_time($timestamp) {
1392 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1393 return date("G:i", $timestamp);
1394 } else if (date("Y", $timestamp) == date("Y")) {
1395 return date("M d, G:i", $timestamp);
1397 return date("Y/m/d, G:i", $timestamp);
1401 function smart_date($timestamp) {
1402 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1404 } else if (date("Y", $timestamp) == date("Y")) {
1405 return date("D m", $timestamp);
1407 return date("Y/m/d", $timestamp);
1411 function sql_bool_to_string($s) {
1412 if ($s == "t" ||
$s == "1") {
1419 function sql_bool_to_bool($s) {
1420 if ($s == "t" ||
$s == "1") {
1428 function toggleEvenOdd($a) {
1435 function sanity_check($link) {
1440 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1441 $schema_version = db_fetch_result($result, 0, "schema_version");
1443 if ($schema_version != SCHEMA_VERSION
) {
1447 if (DB_TYPE
== "mysql") {
1448 $result = db_query($link, "SELECT true", false);
1449 if (db_num_rows($result) != 1) {
1454 error_reporting (DEFAULT_ERROR_LEVEL
);
1456 if ($error_code != 0) {
1457 print_error_xml($error_code);
1464 function file_is_locked($filename) {
1466 $fp = fopen($filename, "r");
1467 error_reporting(DEFAULT_ERROR_LEVEL
);
1469 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1470 flock($fp, LOCK_UN
);
1480 function make_lockfile($filename) {
1481 $fp = fopen($filename, "w");
1483 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1490 function sql_random_function() {
1491 if (DB_TYPE
== "mysql") {
1498 function catchup_feed($link, $feed, $cat_view) {
1500 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1505 $cat_qpart = "cat_id = '$feed'";
1507 $cat_qpart = "cat_id IS NULL";
1510 $tmp_result = db_query($link, "SELECT id
1511 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1514 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1516 $tmp_feed = $tmp_line["id"];
1518 db_query($link, "UPDATE ttrss_user_entries
1519 SET unread = false,last_read = NOW()
1520 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1523 } else if ($feed > 0) {
1525 $tmp_result = db_query($link, "SELECT id
1526 FROM ttrss_feeds WHERE parent_feed = '$feed'
1527 ORDER BY cat_id,title");
1529 $parent_ids = array();
1531 if (db_num_rows($tmp_result) > 0) {
1532 while ($p = db_fetch_assoc($tmp_result)) {
1533 array_push($parent_ids, "feed_id = " . $p["id"]);
1536 $children_qpart = implode(" OR ", $parent_ids);
1538 db_query($link, "UPDATE ttrss_user_entries
1539 SET unread = false,last_read = NOW()
1540 WHERE (feed_id = '$feed' OR $children_qpart)
1541 AND owner_uid = " . $_SESSION["uid"]);
1544 db_query($link, "UPDATE ttrss_user_entries
1545 SET unread = false,last_read = NOW()
1546 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1549 } else if ($feed < 0 && $feed > -10) { // special, like starred
1552 db_query($link, "UPDATE ttrss_user_entries
1553 SET unread = false,last_read = NOW()
1554 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1557 } else if ($feed < -10) { // label
1559 // TODO make this more efficient
1561 $label_id = -$feed - 11;
1563 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1564 WHERE id = '$label_id'");
1567 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1569 db_query($link, "BEGIN");
1571 $tmp2_result = db_query($link,
1575 ttrss_user_entries,ttrss_entries,ttrss_feeds
1577 ref_id = ttrss_entries.id AND
1578 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1580 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1582 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1583 db_query($link, "UPDATE
1586 unread = false, last_read = NOW()
1588 int_id = " . $tmp_line["int_id"]);
1591 db_query($link, "COMMIT");
1593 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1594 SET unread = false,last_read = NOW()
1597 AND owner_uid = ".$_SESSION["uid"]); */
1601 db_query($link, "BEGIN");
1603 $tag_name = db_escape_string($feed);
1605 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1606 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1608 while ($line = db_fetch_assoc($result)) {
1609 db_query($link, "UPDATE ttrss_user_entries SET
1610 unread = false, last_read = NOW()
1611 WHERE int_id = " . $line["post_int_id"]);
1613 db_query($link, "COMMIT");
1617 function update_generic_feed($link, $feed, $cat_view) {
1621 $cat_qpart = "cat_id = '$feed'";
1623 $cat_qpart = "cat_id IS NULL";
1626 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1627 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1629 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1630 $feed_url = $tmp_line["feed_url"];
1631 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1635 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1636 WHERE id = '$feed'");
1637 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1638 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1642 function getAllCounters($link, $omode = "tflc") {
1643 /* getLabelCounters($link);
1644 getFeedCounters($link);
1645 getTagCounters($link);
1646 getGlobalCounters($link);
1647 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1648 getCategoryCounters($link);
1651 if (!$omode) $omode = "tflc";
1653 getGlobalCounters($link);
1655 if (strchr($omode, "l")) getLabelCounters($link);
1656 if (strchr($omode, "f")) getFeedCounters($link);
1657 if (strchr($omode, "t")) getTagCounters($link);
1658 if (strchr($omode, "c")) {
1659 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1660 getCategoryCounters($link);
1665 function getCategoryCounters($link) {
1666 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1667 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1668 AND unread = true)) AS unread FROM ttrss_feeds
1670 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1672 while ($line = db_fetch_assoc($result)) {
1673 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1674 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1675 $line["unread"]."\"/>";
1679 function getCategoryUnread($link, $cat) {
1682 $cat_query = "cat_id = '$cat'";
1684 $cat_query = "cat_id IS NULL";
1687 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1689 AND owner_uid = " . $_SESSION["uid"]);
1691 $cat_feeds = array();
1692 while ($line = db_fetch_assoc($result)) {
1693 array_push($cat_feeds, "feed_id = " . $line["id"]);
1696 if (count($cat_feeds) == 0) return 0;
1698 $match_part = implode(" OR ", $cat_feeds);
1700 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1701 FROM ttrss_user_entries
1702 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1706 # this needs to be rewritten
1707 while ($line = db_fetch_assoc($result)) {
1708 $unread +
= $line["unread"];
1715 function getFeedUnread($link, $feed, $is_cat = false) {
1716 $n_feed = sprintf("%d", $feed);
1719 return getCategoryUnread($link, $n_feed);
1720 } else if ($n_feed == -1) {
1721 $match_part = "marked = true";
1722 } else if ($n_feed > 0) {
1724 $result = db_query($link, "SELECT id FROM ttrss_feeds
1725 WHERE parent_feed = '$n_feed'
1727 AND owner_uid = " . $_SESSION["uid"]);
1729 if (db_num_rows($result) > 0) {
1731 $linked_feeds = array();
1732 while ($line = db_fetch_assoc($result)) {
1733 array_push($linked_feeds, "feed_id = " . $line["id"]);
1736 array_push($linked_feeds, "feed_id = $n_feed");
1738 $match_part = implode(" OR ", $linked_feeds);
1740 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1741 FROM ttrss_user_entries
1742 WHERE unread = true AND ($match_part)
1743 AND owner_uid = " . $_SESSION["uid"]);
1747 # this needs to be rewritten
1748 while ($line = db_fetch_assoc($result)) {
1749 $unread +
= $line["unread"];
1755 $match_part = "feed_id = '$n_feed'";
1757 } else if ($feed < -10) {
1759 $label_id = -$feed - 11;
1761 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1762 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1764 $match_part = db_fetch_result($result, 0, "sql_exp");
1769 $result = db_query($link, "SELECT count(int_id) AS unread
1770 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1771 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1772 ttrss_user_entries.ref_id = ttrss_entries.id AND
1773 ttrss_feeds.hidden = false AND
1774 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1778 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1779 FROM ttrss_tags,ttrss_user_entries
1780 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1781 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1784 $unread = db_fetch_result($result, 0, "unread");
1789 /* FIXME this needs reworking */
1791 function getGlobalUnread($link, $user_id = false) {
1794 $user_id = $_SESSION["uid"];
1797 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1798 WHERE unread = true AND
1799 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1800 ttrss_user_entries.ref_id = ttrss_entries.id AND
1802 ttrss_user_entries.owner_uid = '$user_id'");
1803 $c_id = db_fetch_result($result, 0, "c_id");
1807 function getGlobalCounters($link, $global_unread = -1) {
1808 if ($global_unread == -1) {
1809 $global_unread = getGlobalUnread($link);
1811 print "<counter type=\"global\" id='global-unread'
1812 counter='$global_unread'/>";
1814 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1815 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1817 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1819 print "<counter type=\"global\" id='subscribed-feeds'
1820 counter='$subscribed_feeds'/>";
1824 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1827 if (!$_SESSION["tctr_last_value"]) {
1828 $_SESSION["tctr_last_value"] = array();
1832 $old_counters = $_SESSION["tctr_last_value"];
1834 $tctrs_modified = false;
1836 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1837 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1838 ttrss_user_entries.ref_id = ttrss_entries.id AND
1839 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1840 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1842 select tag_name,0 as count FROM ttrss_tags
1843 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1845 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1846 FROM ttrss_user_entries WHERE int_id = post_int_id
1847 AND unread = true)) AS count FROM ttrss_tags
1848 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
1852 while ($line = db_fetch_assoc($result)) {
1853 $tags[$line["tag_name"]] +
= $line["count"];
1856 foreach (array_keys($tags) as $tag) {
1857 $unread = $tags[$tag];
1859 $tag = htmlspecialchars($tag);
1861 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1862 $old_counters[$tag] = $unread;
1863 $tctrs_modified = true;
1864 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1869 if ($smart_mode && $tctrs_modified) {
1870 $_SESSION["tctr_last_value"] = $old_counters;
1875 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1878 if (!$_SESSION["lctr_last_value"]) {
1879 $_SESSION["lctr_last_value"] = array();
1885 $old_counters = $_SESSION["lctr_last_value"];
1886 $lctrs_modified = false;
1888 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1889 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1890 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1891 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1893 $count = db_fetch_result($result, 0, "count");
1896 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1898 $ret_arr["-1"]["counter"] = $count;
1899 $ret_arr["-1"]["description"] = "Starred";
1902 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1903 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1905 while ($line = db_fetch_assoc($result)) {
1907 $id = -$line["id"] - 11;
1909 $label_name = $line["description"];
1911 error_reporting (0);
1913 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1914 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1915 ttrss_feeds.hidden = false AND
1916 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1917 ttrss_user_entries.ref_id = ttrss_entries.id AND
1918 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1920 $count = db_fetch_result($tmp_result, 0, "count");
1922 if (!$smart_mode ||
$old_counters[$id] != $count) {
1923 $old_counters[$id] = $count;
1924 $lctrs_modified = true;
1926 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1928 $ret_arr[$id]["counter"] = $count;
1929 $ret_arr[$id]["description"] = $label_name;
1933 error_reporting (DEFAULT_ERROR_LEVEL
);
1936 if ($smart_mode && $lctrs_modified) {
1937 $_SESSION["lctr_last_value"] = $old_counters;
1943 /* function getFeedCounter($link, $id) {
1945 $result = db_query($link, "SELECT
1946 count(id) as count,last_error
1947 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1948 WHERE feed_id = '$id' AND unread = true
1949 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1950 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1952 $count = db_fetch_result($result, 0, "count");
1953 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1955 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1958 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1961 if (!$_SESSION["fctr_last_value"]) {
1962 $_SESSION["fctr_last_value"] = array();
1966 $old_counters = $_SESSION["fctr_last_value"];
1968 $result = db_query($link, "SELECT id,last_error,parent_feed,
1969 SUBSTRING(last_updated,1,19) AS last_updated,
1971 FROM ttrss_entries,ttrss_user_entries
1972 WHERE feed_id = ttrss_feeds.id AND
1973 ttrss_user_entries.ref_id = ttrss_entries.id
1974 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1975 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1976 AND parent_feed IS NULL");
1978 $fctrs_modified = false;
1980 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1982 while ($line = db_fetch_assoc($result)) {
1985 $count = $line["count"];
1986 $last_error = htmlspecialchars($line["last_error"]);
1988 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1989 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1991 $last_updated = date($short_date, strtotime($line["last_updated"]));
1994 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1996 $tmp_result = db_query($link,
1997 "SELECT id,COUNT(unread) AS unread
1998 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1999 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2000 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2002 if (db_num_rows($tmp_result) > 0) {
2003 while ($l = db_fetch_assoc($tmp_result)) {
2004 $count +
= $l["unread"];
2008 if (!$smart_mode ||
$old_counters[$id] != $count) {
2009 $old_counters[$id] = $count;
2010 $fctrs_modified = true;
2013 $error_part = "error=\"$last_error\"";
2019 $has_img_part = "hi=\"$has_img\"";
2024 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
2028 if ($smart_mode && $fctrs_modified) {
2029 $_SESSION["fctr_last_value"] = $old_counters;
2033 function get_script_dt_add() {
2034 if (strpos(VERSION
, ".99") === false) {
2041 function get_pgsql_version($link) {
2042 $result = db_query($link, "SELECT version() AS version");
2043 $version = split(" ", db_fetch_result($result, 0, "version"));
2047 function print_error_xml($code, $add_msg = "") {
2050 $error_msg = $ERRORS[$code];
2053 $error_msg = "$error_msg; $add_msg";
2056 print "<rpc-reply>";
2057 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2058 print "</rpc-reply>";
2061 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2062 $auth_login = '', $auth_pass = '') {
2064 # check for feed:http://url
2065 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2067 # check for feed://URL
2068 if (strpos($feed_link, "//") === 0) {
2069 $feed_link = "http:$feed_link";
2072 if ($feed_link == "") return;
2074 if ($cat_id == "0" ||
!$cat_id) {
2075 $cat_qpart = "NULL";
2077 $cat_qpart = "'$cat_id'";
2080 $result = db_query($link,
2081 "SELECT id FROM ttrss_feeds
2082 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2084 if (db_num_rows($result) == 0) {
2086 $result = db_query($link,
2087 "INSERT INTO ttrss_feeds
2088 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2089 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2090 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2092 $result = db_query($link,
2093 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2094 AND owner_uid = " . $_SESSION["uid"]);
2096 $feed_id = db_fetch_result($result, 0, "id");
2099 update_rss_feed($link, $feed_link, $feed_id, true);
2108 function print_feed_select($link, $id, $default_id = "",
2109 $attributes = "", $include_all_feeds = true) {
2111 print "<select id=\"$id\" name=\"$id\" $attributes>";
2112 if ($include_all_feeds) {
2113 print "<option value=\"0\">All feeds</option>";
2116 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2117 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2119 if (db_num_rows($result) > 0 && $include_all_feeds) {
2120 print "<option disabled>--------</option>";
2123 while ($line = db_fetch_assoc($result)) {
2124 if ($line["id"] == $default_id) {
2125 $is_selected = "selected";
2129 printf("<option $is_selected value='%d'>%s</option>",
2130 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2136 function print_feed_cat_select($link, $id, $default_id = "",
2137 $attributes = "", $include_all_cats = true) {
2139 print "<select id=\"$id\" name=\"$id\" $attributes>";
2141 if ($include_all_cats) {
2142 print "<option value=\"0\">".__('Uncategorized')."</option>";
2145 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2146 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2148 if (db_num_rows($result) > 0 && $include_all_cats) {
2149 print "<option disabled>--------</option>";
2152 while ($line = db_fetch_assoc($result)) {
2153 if ($line["id"] == $default_id) {
2154 $is_selected = "selected";
2158 printf("<option $is_selected value='%d'>%s</option>",
2159 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2165 function checkbox_to_sql_bool($val) {
2166 return ($val == "on") ?
"true" : "false";
2169 function getFeedCatTitle($link, $id) {
2171 return __("Special");
2172 } else if ($id < -10) {
2173 return __("Labels");
2174 } else if ($id > 0) {
2175 $result = db_query($link, "SELECT ttrss_feed_categories.title
2176 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2177 cat_id = ttrss_feed_categories.id");
2178 if (db_num_rows($result) == 1) {
2179 return db_fetch_result($result, 0, "title");
2181 return __("Uncategorized");
2184 return "getFeedCatTitle($id) failed";
2189 function getFeedTitle($link, $id) {
2191 return __("Starred articles");
2192 } else if ($id < -10) {
2193 $label_id = -10 - $id;
2194 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2195 if (db_num_rows($result) == 1) {
2196 return db_fetch_result($result, 0, "description");
2198 return "Unknown label ($label_id)";
2201 } else if ($id > 0) {
2202 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2203 if (db_num_rows($result) == 1) {
2204 return db_fetch_result($result, 0, "title");
2206 return "Unknown feed ($id)";
2209 return "getFeedTitle($id) failed";
2214 function get_session_cookie_name() {
2215 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2218 function print_init_params($link) {
2219 print "<init-params>";
2220 if ($_SESSION["stored-params"]) {
2221 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2223 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2224 print "<param key=\"$key\" value=\"$value\"/>";
2229 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2230 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2231 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2233 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2234 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2236 print "<param key=\"hide_read_feeds\" value=\"" .
2237 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2239 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2240 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2242 print "<param key=\"confirm_feed_catchup\" value=\"" .
2243 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2245 print "<param key=\"cdm_auto_catchup\" value=\"" .
2246 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2248 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2250 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2252 print "<param key=\"default_view_mode\" value=\"" .
2253 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2255 print "<param key=\"default_view_limit\" value=\"" .
2256 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2258 print "<param key=\"prefs_active_tab\" value=\"" .
2259 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2261 print "<param key=\"infobox_disable_overlay\" value=\"" .
2262 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2264 print "</init-params>";
2267 function print_runtime_info($link) {
2268 print "<runtime-info>";
2269 if (ENABLE_UPDATE_DAEMON
) {
2270 print "<param key=\"daemon_is_running\" value=\"".
2271 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2273 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2275 if ($_SESSION["last_version_check"] +
600 < time()) {
2276 $new_version_details = check_for_update($link);
2278 print "<param key=\"new_version_available\" value=\"".
2279 sprintf("%d", $new_version_details != ""). "\"/>";
2281 $_SESSION["last_version_check"] = time();
2285 print "</runtime-info>";
2288 function getSearchSql($search, $match_on) {
2290 $search_query_part = "";
2292 $keywords = split(" ", $search);
2293 $query_keywords = array();
2295 if ($match_on == "both") {
2297 foreach ($keywords as $k) {
2298 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2299 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2302 $search_query_part = implode("AND", $query_keywords) . " AND ";
2304 } else if ($match_on == "title") {
2306 foreach ($keywords as $k) {
2307 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2310 $search_query_part = implode("AND", $query_keywords) . " AND ";
2312 } else if ($match_on == "content") {
2314 foreach ($keywords as $k) {
2315 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2319 $search_query_part = implode("AND", $query_keywords);
2321 return $search_query_part;
2324 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2328 $search_query_part = getSearchSql($search, $match_on);
2329 $search_query_part .= " AND ";
2332 $search_query_part = "";
2335 $view_query_part = "";
2337 if ($view_mode == "adaptive") {
2339 $view_query_part = " ";
2340 } else if ($feed != -1) {
2341 $unread = getFeedUnread($link, $feed, $cat_view);
2343 $view_query_part = " unread = true AND ";
2348 if ($view_mode == "marked") {
2349 $view_query_part = " marked = true AND ";
2352 if ($view_mode == "unread") {
2353 $view_query_part = " unread = true AND ";
2357 $limit_query_part = "LIMIT " . $limit;
2360 $vfeed_query_part = "";
2362 // override query strategy and enable feed display when searching globally
2363 if ($search && $search_mode == "all_feeds") {
2364 $query_strategy_part = "ttrss_entries.id > 0";
2365 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2366 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2367 $query_strategy_part = "ttrss_entries.id > 0";
2368 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2369 id = feed_id) as feed_title,";
2370 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2372 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2374 $tmp_result = false;
2377 $tmp_result = db_query($link, "SELECT id
2378 FROM ttrss_feeds WHERE cat_id = '$feed'");
2380 $tmp_result = db_query($link, "SELECT id
2381 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2382 WHERE id = '$feed') AND id != '$feed'");
2385 $cat_siblings = array();
2387 if (db_num_rows($tmp_result) > 0) {
2388 while ($p = db_fetch_assoc($tmp_result)) {
2389 array_push($cat_siblings, "feed_id = " . $p["id"]);
2392 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2393 $feed, implode(" OR ", $cat_siblings));
2396 $query_strategy_part = "ttrss_entries.id > 0";
2399 } else if ($feed >= 0) {
2404 $query_strategy_part = "cat_id = '$feed'";
2406 $query_strategy_part = "cat_id IS NULL";
2409 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2412 $tmp_result = db_query($link, "SELECT id
2413 FROM ttrss_feeds WHERE parent_feed = '$feed'
2414 ORDER BY cat_id,title");
2416 $parent_ids = array();
2418 if (db_num_rows($tmp_result) > 0) {
2419 while ($p = db_fetch_assoc($tmp_result)) {
2420 array_push($parent_ids, "feed_id = " . $p["id"]);
2423 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2424 $feed, implode(" OR ", $parent_ids));
2426 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2428 $query_strategy_part = "feed_id = '$feed'";
2431 } else if ($feed == -1) { // starred virtual feed
2432 $query_strategy_part = "marked = true";
2433 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2434 } else if ($feed <= -10) { // labels
2435 $label_id = -$feed - 11;
2437 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2438 WHERE id = '$label_id'");
2440 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2442 if (!$query_strategy_part) {
2446 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2448 $query_strategy_part = "id > 0"; // dumb
2451 if (get_pref($link, 'REVERSE_HEADLINES')) {
2452 $order_by = "updated";
2454 $order_by = "updated DESC";
2457 if ($override_order) {
2458 $order_by = $override_order;
2463 if ($search && $search_mode == "all_feeds") {
2464 $feed_title = __("Search results")." ($search)";
2465 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2466 $feed_title = __("Search results")." ($search, $feed)";
2467 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2468 $feed_title = $feed;
2469 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2474 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2475 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2476 $feed_title = db_fetch_result($result, 0, "title");
2478 $feed_title = __("Uncategorized");
2482 $feed_title = __("Searched for")." $search ($feed_title)";
2487 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2488 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2490 $feed_title = db_fetch_result($result, 0, "title");
2491 $feed_site_url = db_fetch_result($result, 0, "site_url");
2492 $last_error = db_fetch_result($result, 0, "last_error");
2495 $feed_title = __("Searched for") . " $search ($feed_title)";
2499 } else if ($feed == -1) {
2500 $feed_title = __("Starred articles");
2501 } else if ($feed < -10) {
2502 $label_id = -$feed - 11;
2503 $result = db_query($link, "SELECT description FROM ttrss_labels
2504 WHERE id = '$label_id'");
2505 $feed_title = db_fetch_result($result, 0, "description");
2508 $feed_title = __("Searched for") . " $search ($feed_title)";
2514 $feed_title = db_unescape_string($feed_title);
2516 if ($feed < -10) error_reporting (0);
2518 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2521 $feed_kind = "Feeds";
2523 $feed_kind = "Labels";
2526 $content_query_part = "content as content_preview,";
2528 if ($limit_query_part) {
2529 $offset_query_part = "OFFSET $offset";
2534 ttrss_entries.id,ttrss_entries.title,
2535 SUBSTRING(updated,1,16) as updated,
2536 unread,feed_id,marked,link,last_read,
2537 SUBSTRING(last_read,1,19) as last_read_noms,
2540 SUBSTRING(updated,1,19) as updated_noms,
2543 ttrss_entries,ttrss_user_entries,ttrss_feeds
2545 ttrss_feeds.hidden = false AND
2546 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2547 ttrss_user_entries.ref_id = ttrss_entries.id AND
2548 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2551 $query_strategy_part ORDER BY $order_by
2552 $limit_query_part $offset_query_part";
2554 $result = db_query($link, $query);
2556 if ($_GET["debug"]) print $query;
2561 $feed_kind = "Tags";
2563 $result = db_query($link, "SELECT
2565 ttrss_entries.id as id,title,
2566 SUBSTRING(updated,1,16) as updated,
2568 marked,link,last_read,
2569 SUBSTRING(last_read,1,19) as last_read_noms,
2572 SUBSTRING(updated,1,19) as updated_noms
2574 ttrss_entries,ttrss_user_entries,ttrss_tags
2576 ref_id = ttrss_entries.id AND
2577 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2578 post_int_id = int_id AND tag_name = '$feed' AND
2581 $query_strategy_part ORDER BY $order_by
2582 $limit_query_part");
2585 return array($result, $feed_title, $feed_site_url, $last_error);
2589 function generate_syndicated_feed($link, $feed, $is_cat,
2590 $search, $search_mode, $match_on) {
2592 $qfh_ret = queryFeedHeadlines($link, $feed,
2593 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2595 $result = $qfh_ret[0];
2596 $feed_title = htmlspecialchars($qfh_ret[1]);
2597 $feed_site_url = $qfh_ret[2];
2598 $last_error = $qfh_ret[3];
2600 print "<rss version=\"2.0\">
2602 <title>$feed_title</title>
2603 <link>$feed_site_url</link>
2604 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2606 while ($line = db_fetch_assoc($result)) {
2608 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2609 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2611 $rfc822_date = date('r', strtotime($line["updated"]));
2613 print "<pubDate>$rfc822_date</pubDate>";
2616 htmlspecialchars($line["title"]) . "</title>";
2618 print "<description>" .
2619 htmlspecialchars($line["content_preview"]) . "</description>";
2624 print "</channel></rss>";
2628 function getCategoryTitle($link, $cat_id) {
2630 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2633 if (db_num_rows($result) == 1) {
2634 return db_fetch_result($result, 0, "title");
2636 return "Uncategorized";
2640 function sanitize_rss($link, $str) {
2643 if (get_pref($link, "STRIP_UNSAFE_TAGS")) {
2644 $res = strip_tags($res, "<p><a><i><em><b><strong><blockquote><br><img>");
2650 function send_headlines_digests($link, $limit = 100) {
2652 if (!DIGEST_ENABLE
) return false;
2654 $user_limit = DIGEST_EMAIL_LIMIT
;
2657 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2659 if (DB_TYPE
== "pgsql") {
2660 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2661 } else if (DB_TYPE
== "mysql") {
2662 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2665 $result = db_query($link, "SELECT id,email FROM ttrss_users
2666 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2668 while ($line = db_fetch_assoc($result)) {
2669 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2670 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2672 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2673 $digest = $tuple[0];
2674 $headlines_count = $tuple[1];
2676 if ($headlines_count > 0) {
2677 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2678 "[tt-rss] New headlines for last 24 hours", $digest,
2679 "From: " . MAIL_FROM
. "\n".
2680 "Content-Type: text/plain; charset=\"utf-8\"\n".
2681 "Content-Transfer-Encoding: 8bit\n");
2683 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2684 WHERE id = " . $line["id"]);
2686 print "No headlines\n";
2691 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2695 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2696 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
2697 $tmp .= "=======================================================\n\n";
2699 if (DB_TYPE
== "pgsql") {
2700 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2701 } else if (DB_TYPE
== "mysql") {
2702 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2705 $result = db_query($link, "SELECT ttrss_entries.title,
2706 ttrss_feeds.title AS feed_title,
2709 SUBSTRING(last_updated,1,19) AS last_updated
2711 ttrss_user_entries,ttrss_entries,ttrss_feeds
2713 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2714 AND include_in_digest = true
2716 AND ttrss_user_entries.owner_uid = $user_id
2717 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2720 $cur_feed_title = "";
2722 $headlines_count = db_num_rows($result);
2724 while ($line = db_fetch_assoc($result)) {
2725 $updated = smart_date_time(strtotime($line["last_updated"]));
2726 $feed_title = $line["feed_title"];
2728 if ($cur_feed_title != $feed_title) {
2729 $cur_feed_title = $feed_title;
2731 $tmp .= "$feed_title\n\n";
2734 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2735 $tmp .= " " . trim($line["link"]) . "\n";
2740 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
2741 DIGEST_HOSTNAME
. "\n".
2742 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
2745 return array($tmp, $headlines_count);
2748 function check_for_update($link, $brief_fmt = true) {
2749 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2751 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2756 $rss = fetch_rss($releases_feed);
2757 error_reporting (DEFAULT_ERROR_LEVEL
);
2761 $items = $rss->items
;
2763 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2764 if (!$items ||
!is_array($items)) $items = $rss;
2766 if (!is_array($items) ||
count($items) == 0) {
2770 $latest_item = $items[0];
2772 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2774 $release_url = sanitize_rss($link, $latest_item["link"]);
2775 $content = sanitize_rss($link, $latest_item["description"]);
2777 if (version_compare(VERSION
, $latest_version) == -1) {
2779 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
2780 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2781 <div id=\"milestoneDetails\">$content</div>");
2783 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2784 <div class='milestoneDetails'>$content</div>
2785 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2786 download and update information.";
2793 function markArticlesById($link, $ids, $cmode) {
2797 foreach ($ids as $id) {
2798 array_push($tmp_ids, "ref_id = '$id'");
2801 $ids_qpart = join(" OR ", $tmp_ids);
2804 db_query($link, "UPDATE ttrss_user_entries SET
2805 marked = false,last_read = NOW()
2806 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2807 } else if ($cmode == 1) {
2808 db_query($link, "UPDATE ttrss_user_entries SET
2810 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2812 db_query($link, "UPDATE ttrss_user_entries SET
2813 marked = NOT marked,last_read = NOW()
2814 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2818 function catchupArticlesById($link, $ids, $cmode) {
2822 foreach ($ids as $id) {
2823 array_push($tmp_ids, "ref_id = '$id'");
2826 $ids_qpart = join(" OR ", $tmp_ids);
2829 db_query($link, "UPDATE ttrss_user_entries SET
2830 unread = false,last_read = NOW()
2831 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2832 } else if ($cmode == 1) {
2833 db_query($link, "UPDATE ttrss_user_entries SET
2835 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2837 db_query($link, "UPDATE ttrss_user_entries SET
2838 unread = NOT unread,last_read = NOW()
2839 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2843 function catchupArticleById($link, $id, $cmode) {
2846 db_query($link, "UPDATE ttrss_user_entries SET
2847 unread = false,last_read = NOW()
2848 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2849 } else if ($cmode == 1) {
2850 db_query($link, "UPDATE ttrss_user_entries SET
2852 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2854 db_query($link, "UPDATE ttrss_user_entries SET
2855 unread = NOT unread,last_read = NOW()
2856 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2860 function escape_for_form($s) {
2861 return htmlspecialchars(db_unescape_string($s));
2864 function make_guid_from_title($title) {
2865 return preg_replace("/[ \"\',.:;]/", "-",
2866 mb_strtolower(strip_tags($title), 'utf-8'));
2869 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2870 $bottom = false, $rtl_content = false, $feed_id = 0,
2871 $is_cat = false, $search = false, $match_on = false,
2872 $search_mode = false, $offset = 0, $limit = 0) {
2874 $user_page_offset = $offset +
1;
2877 $class = "headlinesSubToolbar";
2878 $tid = "headlineActionsTop";
2880 $class = "headlinesSubToolbar";
2881 $tid = "headlineActionsBottom";
2884 print "<table class=\"$class\" id=\"$tid\"
2885 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2893 $page_prev_link = "javascript:viewFeedGoPage(-1)";
2894 $page_next_link = "javascript:viewFeedGoPage(1)";
2895 $page_first_link = "javascript:viewFeedGoPage(0)";
2897 $catchup_page_link = "javascript:catchupPage()";
2898 $catchup_feed_link = "javascript:catchupCurrentFeed()";
2900 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2902 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
2903 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
2904 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
2906 $tog_unread_link = "javascript:selectionToggleUnread()";
2907 $tog_marked_link = "javascript:selectionToggleMarked()";
2911 $sel_all_link = "javascript:cdmSelectArticles('all')";
2912 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
2913 $sel_none_link = "javascript:cdmSelectArticles('none')";
2915 $tog_unread_link = "javascript:selectionToggleUnread(true)";
2916 $tog_marked_link = "javascript:selectionToggleMarked(true)";
2920 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
2922 print "<td class=\"headlineActions$rtl_cpart\">
2923 <ul class=\"headlineDropdownMenu\">
2926 <a href=\"$sel_all_link\">".__('All')."</a>,
2927 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2928 <a href=\"$sel_none_link\">".__('None')."</a></li>
2929 <li class=\"vsep\"> </li>
2930 <li class=\"top\">Toggle<ul>
2931 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
2932 <li onclick=\"$tog_marked_link\">".__('Starred')."</li></ul></li>
2933 <li class=\"vsep\"> </li>
2934 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
2935 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
2936 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
2937 <li class=\"vsep\"> </li>";
2939 if ($limit != 0 && !$search) {
2941 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
2942 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
2943 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
2947 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2948 print "<li class=\"top3\">
2949 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2950 '$match_on', '$feed_id', '$is_cat');\">
2951 ".__('Convert to label')."</a></td>";
2957 // old style subtoolbar:
2959 print "<td class=\"headlineActions$rtl_cpart\">".
2961 <a href=\"$sel_all_link\">".__('All')."</a>,
2962 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2963 <a href=\"$sel_none_link\">".__('None')."</a>
2965 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
2966 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2968 __('Mark as read:')."
2969 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
2970 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
2972 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2975 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2976 '$match_on', '$feed_id', '$is_cat');\">
2977 ".__('Convert to label')."</a>";
2984 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
2985 print "<td class=\"headlineActions$rtl_cpart\">
2986 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2987 '$match_on', '$feed_id', '$is_cat');\">
2988 ".__('Convert to Label')."</a></td>";
2991 print "<td class=\"headlineTitle$rtl_cpart\">";
2993 if ($feed_site_url) {
2995 $target = "target=\"_blank\"";
2997 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
3003 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3006 if ($user_page_offset > 1) {
3007 print " [$user_page_offset] ";
3013 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3014 <img class=\"noborder\"
3015 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3020 print "</tr></table>";
3024 function outputFeedList($link, $tags = false) {
3026 print "<ul class=\"feedList\" id=\"feedList\">";
3028 $owner_uid = $_SESSION["uid"];
3032 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3033 print "<li class=\"feedCat\">".__('Special')."</li>";
3034 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3037 $num_starred = getFeedUnread($link, -1);
3041 if ($num_starred > 0) $class .= "Unread";
3043 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3044 "images/mark_set.png", $link);
3046 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3052 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3054 $result = db_query($link, "SELECT id,sql_exp,description FROM
3055 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3057 if (db_num_rows($result) > 0) {
3058 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3059 print "<li class=\"feedCat\">".__('Labels')."</li>";
3060 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3062 print "<li><hr></li>";
3066 while ($line = db_fetch_assoc($result)) {
3068 error_reporting (0);
3070 $label_id = -$line['id'] - 11;
3071 $count = getFeedUnread($link, $label_id);
3079 error_reporting (DEFAULT_ERROR_LEVEL
);
3081 printFeedEntry($label_id,
3082 $class, db_unescape_string($line["description"]),
3083 $count, "images/label.png", $link);
3087 if (db_num_rows($result) > 0) {
3088 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3095 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3096 print "<li><hr></li>";
3099 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3100 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3101 $order_by_qpart = "category,unread DESC,title";
3103 $order_by_qpart = "category,title";
3106 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3107 $order_by_qpart = "unread DESC,title";
3109 $order_by_qpart = "title";
3113 $result = db_query($link, "SELECT ttrss_feeds.*,
3114 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3115 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3116 WHERE feed_id = ttrss_feeds.id AND unread = true
3117 AND ttrss_user_entries.ref_id = ttrss_entries.id
3118 AND owner_uid = '$owner_uid') as unread,
3120 ttrss_feed_categories.title AS category,
3121 ttrss_feed_categories.collapsed
3122 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3123 ON (ttrss_feed_categories.id = cat_id)
3125 ttrss_feeds.hidden = false AND
3126 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3127 ORDER BY $order_by_qpart");
3129 $actid = $_GET["actid"];
3139 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3141 while ($line = db_fetch_assoc($result)) {
3143 $feed = trim(db_unescape_string($line["title"]));
3145 if (!$feed) $feed = "[Untitled]";
3147 $feed_id = $line["id"];
3149 $subop = $_GET["subop"];
3151 $unread = $line["unread"];
3153 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3154 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3156 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3159 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3162 $rtl_tag = "dir=\"RTL\"";
3167 $tmp_result = db_query($link,
3168 "SELECT id,COUNT(unread) AS unread
3169 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3170 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3171 WHERE parent_feed = '$feed_id' AND unread = true
3172 GROUP BY ttrss_feeds.id");
3174 if (db_num_rows($tmp_result) > 0) {
3175 while ($l = db_fetch_assoc($tmp_result)) {
3176 $unread +
= $l["unread"];
3180 $cat_id = $line["cat_id"];
3182 $tmp_category = $line["category"];
3184 if (!$tmp_category) {
3185 $tmp_category = __("Uncategorized");
3188 // $class = ($lnum % 2) ? "even" : "odd";
3190 if ($line["last_error"]) {
3196 if ($unread > 0) $class .= "Unread";
3198 if ($actid == $feed_id) {
3199 $class .= "Selected";
3202 $total_unread +
= $unread;
3204 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3210 $category = $tmp_category;
3212 $collapsed = $line["collapsed"];
3214 // workaround for NULL category
3215 if ($category == __("Uncategorized")) {
3216 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3221 if ($collapsed == "t" ||
$collapsed == "1") {
3222 $holder_class = "invisible";
3225 $holder_class = "feedCatHolder";
3229 $cat_id = sprintf("%d", $cat_id);
3231 $cat_unread = getCategoryUnread($link, $cat_id);
3233 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3235 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3236 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3237 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3238 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3239 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3242 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3243 // -> keyboard navigation, etc.
3244 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3247 printFeedEntry($feed_id, $class, $feed, $unread,
3248 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
3249 $last_updated, $line["last_error"]);
3254 if (db_num_rows($result) == 0) {
3255 print "<li>".__('No feeds to display.')."</li>";
3262 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3263 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3264 post_int_id = ttrss_user_entries.int_id AND
3265 unread = true AND ref_id = ttrss_entries.id
3266 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3268 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3269 ORDER BY tag_name"); */
3271 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3272 print "<li class=\"feedCat\">".__('Tags')."</li>";
3273 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3276 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3277 FROM ttrss_user_entries WHERE int_id = post_int_id
3278 AND unread = true)) AS count FROM ttrss_tags
3279 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
3283 while ($line = db_fetch_assoc($result)) {
3284 $tags[$line["tag_name"]] +
= $line["count"];
3287 foreach (array_keys($tags) as $tag) {
3289 $unread = $tags[$tag];
3297 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3301 if (db_num_rows($result) == 0) {
3302 print "<li>No tags to display.</li>";
3305 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3315 function get_article_tags($link, $id) {
3317 $a_id = db_escape_string($id);
3319 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3320 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3321 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
3325 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3326 array_push($tags, $tmp_line["tag_name"]);
3332 function trim_value(&$value) {
3333 $value = trim($value);
3336 function trim_array($array) {
3338 array_walk($tmp, 'trim_value');
3342 function tag_is_valid($tag) {
3343 if ($tag == '') return false;
3344 if (preg_match("/^[0-9]*$/", $tag)) return false;
3346 $tag = iconv("utf-8", "utf-8", $tag);
3347 if (!$tag) return false;
3352 function render_login_form($link, $mobile = false) {
3354 require_once "login_form.php";
3356 require_once "mobile/login_form.php";
3360 // from http://developer.apple.com/internet/safari/faq.html
3361 function no_cache_incantation() {
3362 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3363 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3364 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3365 header("Cache-Control: post-check=0, pre-check=0", false);
3366 header("Pragma: no-cache"); // HTTP/1.0
3369 function format_warning($msg, $id = "") {
3370 return "<div class=\"warning\" id=\"$id\">
3371 <img src=\"images/sign_excl.png\">$msg</div>";
3374 function format_notice($msg) {
3375 return "<div class=\"notice\">
3376 <img src=\"images/sign_info.png\">$msg</div>";
3379 function format_error($msg) {
3380 return "<div class=\"error\">
3381 <img src=\"images/sign_excl.png\">$msg</div>";
3384 function print_notice($msg) {
3385 return print format_notice($msg);
3388 function print_warning($msg) {
3389 return print format_warning($msg);
3392 function print_error($msg) {
3393 return print format_error($msg);
3397 function T_sprintf() {
3398 $args = func_get_args();
3399 return vsprintf(__(array_shift($args)), $args);
3402 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3404 /* we can figure out feed_id from article id anyway, why do we
3405 * pass feed_id here? */
3407 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3408 WHERE ref_id = '$id'");
3410 $feed_id = db_fetch_result($result, 0, "feed_id");
3412 print "<article id='$id'><![CDATA[";
3414 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3415 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3417 if (db_num_rows($result) == 1) {
3418 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3420 $rtl_content = false;
3424 $rtl_tag = "dir=\"RTL\"";
3431 if ($mark_as_read) {
3432 $result = db_query($link, "UPDATE ttrss_user_entries
3433 SET unread = false,last_read = NOW()
3434 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3437 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3438 SUBSTRING(updated,1,16) as updated,
3439 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3442 FROM ttrss_entries,ttrss_user_entries
3443 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3449 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3450 $link_target = "target=\"_new\"";
3453 $line = db_fetch_assoc($result);
3455 if ($line["icon_url"]) {
3456 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3458 $feed_icon = " ";
3461 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
3462 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
3464 $entry_comments = "";
3467 $num_comments = $line["num_comments"];
3468 $entry_comments = "";
3470 if ($num_comments > 0) {
3471 if ($line["comments"]) {
3472 $comments_url = $line["comments"];
3474 $comments_url = $line["link"];
3476 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
3478 if ($line["comments"] && $line["link"] != $line["comments"]) {
3479 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
3483 print "<div class=\"postReply\">";
3485 print "<div class=\"postHeader\">";
3487 $entry_author = $line["author"];
3489 if ($entry_author) {
3490 $entry_author = __(" - by ") . $entry_author;
3493 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
3494 strtotime($line["updated"]));
3496 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3498 if ($line["link"]) {
3499 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
3500 $line["title"] . "</a>$entry_author</div>";
3502 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3505 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3506 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
3507 ORDER BY tag_name");
3514 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3516 $tag = $tmp_line["tag_name"];
3517 $tag_str = "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
3519 if ($num_tags == 6) {
3520 $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
3521 } else if ($num_tags < 6) {
3522 $tags_str .= $tag_str;
3524 $f_tags_str .= $tag_str;
3527 $tags_str = preg_replace("/, $/", "", $tags_str);
3528 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
3530 if (!$entry_comments) $entry_comments = " "; # placeholder
3532 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
3534 print "<div style='float : right'>$tags_str
3535 <a title=\"Edit tags for this article\"
3536 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
3537 <div clear='both'>$entry_comments</div>";
3541 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
3542 print "<div class=\"postContent\">";
3544 if (db_num_rows($tmp_result) > 0) {
3545 print "<div id=\"allEntryTags\">".__('Tags:')."$f_tags_str</div>";
3548 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3549 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
3552 $line["content"] = sanitize_rss($link, $line["content"]);
3554 print $line["content"] . "</div>";
3560 print "]]></article>";
3564 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
3565 $next_unread_feed, $offset) {
3567 $topmost_article_ids = array();
3569 if (!$offset) $offset = 0;
3571 if ($subop == "undefined") $subop = "";
3573 if ($subop == "CatchupSelected") {
3574 $ids = split(",", db_escape_string($_GET["ids"]));
3575 $cmode = sprintf("%d", $_GET["cmode"]);
3577 catchupArticlesById($link, $ids, $cmode);
3580 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
3581 update_generic_feed($link, $feed, $cat_view);
3584 if ($subop == "MarkAllRead") {
3585 catchup_feed($link, $feed, $cat_view);
3587 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
3588 if ($next_unread_feed) {
3589 $feed = $next_unread_feed;
3595 $result = db_query($link,
3596 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
3598 if (db_num_rows($result) == 0) {
3599 print "<div align='center'>".__('Feed not found.')."</div>";
3604 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3606 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3607 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
3609 if (db_num_rows($result) == 1) {
3610 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3612 $rtl_content = false;
3616 $rtl_tag = "dir=\"RTL\"";
3622 $rtl_content = false;
3625 $script_dt_add = get_script_dt_add();
3627 /// START /////////////////////////////////////////////////////////////////////////////////
3629 $search = db_escape_string($_GET["query"]);
3630 $search_mode = db_escape_string($_GET["search_mode"]);
3631 $match_on = db_escape_string($_GET["match_on"]);
3637 $real_offset = $offset * $limit;
3639 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
3640 $search, $search_mode, $match_on, false, $real_offset);
3642 $result = $qfh_ret[0];
3643 $feed_title = $qfh_ret[1];
3644 $feed_site_url = $qfh_ret[2];
3645 $last_error = $qfh_ret[3];
3647 /// STOP //////////////////////////////////////////////////////////////////////////////////
3649 print "<div id=\"headlinesContainer\" $rtl_tag>";
3652 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
3656 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
3657 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
3660 print "<div id=\"headlinesInnerContainer\">";
3662 if (db_num_rows($result) > 0) {
3664 # print "\{$offset}";
3666 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3667 print "<table class=\"headlinesList\" id=\"headlinesList\"
3668 cellspacing=\"0\">";
3673 error_reporting (DEFAULT_ERROR_LEVEL
);
3677 while ($line = db_fetch_assoc($result)) {
3679 $class = ($lnum %
2) ?
"even" : "odd";
3682 $feed_id = $line["feed_id"];
3684 if (count($topmost_article_ids) < 5) {
3685 array_push($topmost_article_ids, $id);
3688 if ($line["last_read"] == "" &&
3689 ($line["unread"] != "t" && $line["unread"] != "1")) {
3691 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
3694 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
3698 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
3706 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
3707 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\"
3709 alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
3711 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\"
3713 alt=\"Set mark\" onclick='javascript:tMark($id)'>";
3716 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
3717 # $line["title"] . "</a>";
3719 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
3720 $line["title"] . "</a>";
3722 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
3723 # $line["title"] . "</a>";
3725 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3726 $updated_fmt = smart_date_time(strtotime($line["updated"]));
3728 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3729 $updated_fmt = date($short_date, strtotime($line["updated"]));
3732 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
3733 $content_preview = truncate_string(strip_tags($line["content_preview"]),
3737 $entry_author = $line["author"];
3739 if ($entry_author) {
3740 $entry_author = " - by $entry_author";
3743 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3745 print "<tr class='$class' id='RROW-$id'>";
3747 print "<td class='hlUpdPic'>$update_pic</td>";
3749 print "<td class='hlSelectRow'>
3750 <input type=\"checkbox\" onclick=\"tSR(this)\"
3754 print "<td class='hlMarkedPic'>$marked_pic</td>";
3756 if ($line["feed_title"]) {
3757 print "<td class='hlContent'>$content_link</td>";
3758 print "<td class='hlFeed'>
3759 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
3760 $line["feed_title"]."</a> </td>";
3762 print "<td class='hlContent' valign='middle'>";
3764 print "<a href=\"javascript:view($id,$feed_id);\">" .
3767 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
3768 if ($content_preview) {
3769 print "<span class=\"contentPreview\"> - $content_preview</span>";
3777 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
3784 $add_class = "Unread";
3789 print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
3791 print "<div class=\"cdmHeader\">";
3793 print "<div class=\"articleUpdated\">$updated_fmt</div>";
3795 print "<a class=\"title\"
3796 onclick=\"javascript:toggleUnread($id, 0)\"
3797 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
3799 print $entry_author;
3801 if ($line["feed_title"]) {
3802 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
3807 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
3809 print "<div class=\"cdmFooter\">";
3811 print "$marked_pic";
3813 print "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3814 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
3816 $tags = get_article_tags($link, $id);
3820 foreach ($tags as $tag) {
3822 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
3825 $tags_str = preg_replace("/, $/", "", $tags_str);
3827 if ($tags_str == "") $tags_str = "no tags";
3829 print " $tags_str <a title=\"Edit tags for this article\"
3830 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
3834 # print "<div align=\"center\"><a class=\"cdmToggleLink\"
3835 # href=\"javascript:toggleUnread($id)\">
3836 # Toggle unread</a></div>";
3845 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3849 // print_headline_subtoolbar($link,
3850 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
3854 print "<div class='whiteBox'>".__('No articles found.')."</div>";
3861 return $topmost_article_ids;
3864 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
3866 function printTagCloud($link) {
3868 /* get first ref_id to count from */
3872 if (DB_TYPE
== "pgsql") {
3873 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
3874 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
3875 AND date_entered > NOW() - INTERVAL '30 days'";
3877 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
3878 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
3879 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
3882 $result = db_query($link, $query);
3883 $first_id = db_fetch_result($result, 0, "id");
3885 $query = "SELECT tag_name, COUNT(post_int_id) AS count
3886 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
3887 AND post_int_id >= '$first_id'
3888 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
3890 $result = db_query($link, $query);
3894 while ($line = db_fetch_assoc($result)) {
3895 $tags[$line["tag_name"]] = $line["count"];
3900 $max_size = 32; // max font size in pixels
3901 $min_size = 11; // min font size in pixels
3903 // largest and smallest array values
3904 $max_qty = max(array_values($tags));
3905 $min_qty = min(array_values($tags));
3907 // find the range of values
3908 $spread = $max_qty - $min_qty;
3909 if ($spread == 0) { // we don't want to divide by zero
3913 // set the font-size increment
3914 $step = ($max_size - $min_size) / ($spread);
3916 // loop through the tag array
3917 foreach ($tags as $key => $value) {
3918 // calculate font-size
3919 // find the $value in excess of $min_qty
3920 // multiply by the font-size increment ($size)
3921 // and add the $min_size set above
3922 $size = round($min_size +
(($value - $min_qty) * $step));
3924 echo "<a href=\"javascript:viewfeed('$key') \" style=\"font-size: " .
3925 $size . "px\" title=\"$value articles tagged with " .
3926 $key . '">' . $key . '</a> ';