3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
9 require_once 'config.php';
11 function get_translations() {
13 "auto" => "Detect automatically",
15 "fr_FR" => "Français",
17 "zh_CN" => "Simplified Chinese");
22 if (ENABLE_TRANSLATIONS
== true) {
23 require_once "accept-to-gettext.php";
24 require_once "gettext/gettext.inc";
26 function startup_gettext() {
28 # Get locale from Accept-Language header
29 $lang = al2gt(array_keys(get_translations()), "text/html");
31 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
32 $lang = _TRANSLATION_OVERRIDE_DEFAULT
;
35 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
36 $lang = $_COOKIE["ttrss_lang"];
40 _setlocale(LC_MESSAGES
, $lang);
41 _bindtextdomain("messages", "locale");
42 _textdomain("messages");
43 _bind_textdomain_codeset("messages", "UTF-8");
53 function startup_gettext() {
59 require_once 'db-prefs.php';
60 require_once 'compat.php';
61 require_once 'errors.php';
62 require_once 'version.php';
64 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
65 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
67 if (ENABLE_SIMPLEPIE
) {
68 require_once "simplepie/simplepie.inc";
70 require_once "magpierss/rss_fetch.inc";
71 require_once 'magpierss/rss_utils.inc';
74 include_once "tw/tw-config.php";
75 include_once "tw/tw.php";
76 include_once TW_SETUP
. "paranoya.php";
78 $tw_parser = new twParser();
80 function _debug($msg) {
81 $ts = strftime("%H:%M:%S", time());
85 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
89 if (DB_TYPE
== "pgsql") {
90 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
91 marked = false AND feed_id = '$feed_id' AND
92 (SELECT date_entered FROM ttrss_entries WHERE
93 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
95 $pg_version = get_pgsql_version($link);
97 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
99 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
100 ttrss_entries.id = ref_id AND
102 feed_id = '$feed_id' AND
103 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
107 $result = db_query($link, "DELETE FROM ttrss_user_entries
109 WHERE ttrss_entries.id = ref_id AND
111 feed_id = '$feed_id' AND
112 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
115 $rows = pg_affected_rows($result);
119 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
120 marked = false AND feed_id = '$feed_id' AND
121 (SELECT date_entered FROM ttrss_entries WHERE
122 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
124 $result = db_query($link, "DELETE FROM ttrss_user_entries
125 USING ttrss_user_entries, ttrss_entries
126 WHERE ttrss_entries.id = ref_id AND
128 feed_id = '$feed_id' AND
129 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
131 $rows = mysql_affected_rows($link);
136 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
140 function global_purge_old_posts($link, $do_output = false, $limit = false) {
142 $random_qpart = sql_random_function();
145 $limit_qpart = "LIMIT $limit";
150 $result = db_query($link,
151 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
152 ORDER BY $random_qpart $limit_qpart");
154 while ($line = db_fetch_assoc($result)) {
156 $feed_id = $line["id"];
157 $purge_interval = $line["purge_interval"];
158 $owner_uid = $line["owner_uid"];
160 if ($purge_interval == 0) {
162 $tmp_result = db_query($link,
163 "SELECT value FROM ttrss_user_prefs WHERE
164 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
166 if (db_num_rows($tmp_result) != 0) {
167 $purge_interval = db_fetch_result($tmp_result, 0, "value");
172 // print "Feed $feed_id: purge interval = $purge_interval\n";
175 if ($purge_interval > 0) {
176 purge_feed($link, $feed_id, $purge_interval, $do_output);
180 // purge orphaned posts in main content table
181 db_query($link, "DELETE FROM ttrss_entries WHERE
182 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
186 function purge_old_posts($link) {
188 $user_id = $_SESSION["uid"];
190 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
191 WHERE owner_uid = '$user_id'");
193 while ($line = db_fetch_assoc($result)) {
195 $feed_id = $line["id"];
196 $purge_interval = $line["purge_interval"];
198 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
200 if ($purge_interval > 0) {
201 purge_feed($link, $feed_id, $purge_interval);
205 // purge orphaned posts in main content table
206 db_query($link, "DELETE FROM ttrss_entries WHERE
207 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
210 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
212 if (WEB_DEMO_MODE
) return;
215 $user_id = $_SESSION["uid"];
216 purge_old_posts($link);
219 // db_query($link, "BEGIN");
221 if (MAX_UPDATE_TIME
> 0) {
222 if (DB_TYPE
== "mysql") {
225 $q_order = "RANDOM()";
228 $q_order = "last_updated DESC";
231 $result = db_query($link, "SELECT feed_url,id,
232 SUBSTRING(last_updated,1,19) AS last_updated,
233 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
238 while ($line = db_fetch_assoc($result)) {
239 $upd_intl = $line["update_interval"];
241 if (!$upd_intl ||
$upd_intl == 0) {
242 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
246 // Updates for this feed are disabled
250 if ($fetch ||
(!$line["last_updated"] ||
251 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
253 // print "<!-- feed: ".$line["feed_url"]." -->";
255 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
257 $upd_elapsed = time() - $upd_start;
259 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
265 // db_query($link, "COMMIT");
269 function fetch_file_contents($url) {
270 if (USE_CURL_FOR_ICONS
) {
271 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
273 $ch = curl_init($url);
274 $fp = fopen($tmpfile, "w");
277 curl_setopt($ch, CURLOPT_FILE
, $fp);
278 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
279 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
285 $contents = file_get_contents($tmpfile);
291 return file_get_contents($url);
296 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
297 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
299 function get_favicon_url($url) {
301 if ($html = @fetch_file_contents
($url)) {
303 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
304 // Attempt to grab a favicon link from their webpage url
305 $linkUrl = html_entity_decode($matches[1]);
307 if (substr($linkUrl, 0, 1) == '/') {
308 $urlParts = parse_url($url);
309 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
310 } else if (substr($linkUrl, 0, 7) == 'http://') {
311 $faviconURL = $linkUrl;
312 } else if (substr($url, -1, 1) == '/') {
313 $faviconURL = $url.$linkUrl;
315 $faviconURL = $url.'/'.$linkUrl;
319 // If unsuccessful, attempt to "guess" the favicon location
320 $urlParts = parse_url($url);
321 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
325 // Run a test to see if what we have attempted to get actually exists.
326 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
333 function url_validate($link) {
335 $url_parts = @parse_url
($link);
337 if ( empty( $url_parts["host"] ) )
340 if ( !empty( $url_parts["path"] ) ) {
341 $documentpath = $url_parts["path"];
346 if ( !empty( $url_parts["query"] ) )
347 $documentpath .= "?" . $url_parts["query"];
349 $host = $url_parts["host"];
350 $port = $url_parts["port"];
355 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
360 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
362 $http_response = fgets( $socket, 22 );
364 $responses = "/(200 OK)|(30[0-9] Moved)/";
365 if ( preg_match($responses, $http_response) ) {
374 function check_feed_favicon($site_url, $feed, $link) {
375 $favicon_url = get_favicon_url($site_url);
377 # print "FAVICON [$site_url]: $favicon_url\n";
381 $icon_file = ICONS_DIR
. "/$feed.ico";
383 if ($favicon_url && !file_exists($icon_file)) {
384 $contents = fetch_file_contents($favicon_url);
386 $fp = fopen($icon_file, "w");
389 fwrite($fp, $contents);
391 chmod($icon_file, 0644);
395 error_reporting(DEFAULT_ERROR_LEVEL
);
399 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
401 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
405 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
406 _debug("update_rss_feed: start");
409 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
410 FROM ttrss_feeds WHERE id = '$feed'");
412 $auth_login = db_fetch_result($result, 0, "auth_login");
413 $auth_pass = db_fetch_result($result, 0, "auth_pass");
415 $update_interval = db_fetch_result($result, 0, "update_interval");
417 if ($update_interval < 0) { return; }
419 $feed = db_escape_string($feed);
421 $fetch_url = $feed_url;
423 if ($auth_login && $auth_pass) {
424 $url_parts = array();
425 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
427 if ($url_parts[1] && $url_parts[2]) {
428 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
433 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
434 _debug("update_rss_feed: fetching [$fetch_url]...");
437 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
441 if (!ENABLE_SIMPLEPIE
) {
442 $rss = fetch_rss($fetch_url);
444 $rss = new SimplePie();
445 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
446 $rss->set_timeout(MAGPIE_FETCH_TIME_OUT
);
447 $rss->set_feed_url($fetch_url);
448 $rss->set_output_encoding('UTF-8');
454 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
455 _debug("update_rss_feed: fetch done, parsing...");
457 error_reporting (DEFAULT_ERROR_LEVEL
);
460 $feed = db_escape_string($feed);
462 if (ENABLE_SIMPLEPIE
) {
463 $fetch_ok = !$rss->error();
470 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
471 _debug("update_rss_feed: processing feed data...");
474 // db_query($link, "BEGIN");
476 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
477 FROM ttrss_feeds WHERE id = '$feed'");
479 $registered_title = db_fetch_result($result, 0, "title");
480 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
481 $orig_site_url = db_fetch_result($result, 0, "site_url");
483 $owner_uid = db_fetch_result($result, 0, "owner_uid");
485 if (ENABLE_SIMPLEPIE
) {
486 $site_url = $rss->get_link();
488 $site_url = $rss->channel
["link"];
491 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
492 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
493 _debug("update_rss_feed: checking favicon...");
496 check_feed_favicon($site_url, $feed, $link);
499 if (!$registered_title ||
$registered_title == "[Unknown]") {
501 if (ENABLE_SIMPLEPIE
) {
502 $feed_title = $rss->get_title();
504 $feed_title = db_escape_string($rss->channel
["title"]);
507 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
508 _debug("update_rss_feed: registering title: $feed_title");
511 db_query($link, "UPDATE ttrss_feeds SET
512 title = '$feed_title' WHERE id = '$feed'");
515 // weird, weird Magpie
516 if (!ENABLE_SIMPLEPIE
) {
517 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
520 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
521 db_query($link, "UPDATE ttrss_feeds SET
522 site_url = '$site_url' WHERE id = '$feed'");
525 // print "I: " . $rss->channel["image"]["url"];
527 if (!ENABLE_SIMPLEPIE
) {
528 $icon_url = $rss->image
["url"];
530 $icon_url = $rss->get_image_url();
533 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
534 $icon_url = db_escape_string($icon_url);
535 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
538 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
539 _debug("update_rss_feed: loading filters...");
544 $result = db_query($link, "SELECT reg_exp,
545 ttrss_filter_types.name AS name,
546 ttrss_filter_actions.name AS action,
549 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
551 owner_uid = $owner_uid AND
552 ttrss_filter_types.id = filter_type AND
553 ttrss_filter_actions.id = action_id AND
554 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
556 while ($line = db_fetch_assoc($result)) {
557 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
559 $filter["reg_exp"] = $line["reg_exp"];
560 $filter["action"] = $line["action"];
561 $filter["action_param"] = $line["action_param"];
562 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
564 array_push($filters[$line["name"]], $filter);
567 if (ENABLE_SIMPLEPIE
) {
568 $iterator = $rss->get_items();
570 $iterator = $rss->items
;
571 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
572 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
575 if (!is_array($iterator)) {
576 /* db_query($link, "UPDATE ttrss_feeds
577 SET last_error = 'Parse error: can\'t find any articles.'
578 WHERE id = '$feed'"); */
580 // clear any errors and mark feed as updated if fetched okay
581 // even if it's blank
583 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
584 _debug("update_rss_feed: entry iterator is not an array, no articles?");
587 db_query($link, "UPDATE ttrss_feeds
588 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
590 return; // no articles
593 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
594 _debug("update_rss_feed: processing articles...");
597 foreach ($iterator as $item) {
599 if (ENABLE_SIMPLEPIE
) {
600 $entry_guid = $item->get_id();
601 if (!$entry_guid) $entry_guid = $item->get_link();
602 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
606 $entry_guid = $item["id"];
608 if (!$entry_guid) $entry_guid = $item["guid"];
609 if (!$entry_guid) $entry_guid = $item["link"];
610 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
613 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
614 _debug("update_rss_feed: guid $entry_guid");
617 if (!$entry_guid) continue;
619 $entry_timestamp = "";
621 if (ENABLE_SIMPLEPIE
) {
622 $entry_timestamp = strtotime($item->get_date());
624 $rss_2_date = $item['pubdate'];
625 $rss_1_date = $item['dc']['date'];
626 $atom_date = $item['issued'];
627 if (!$atom_date) $atom_date = $item['updated'];
629 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
630 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
631 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
634 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
635 $entry_timestamp = time();
636 $no_orig_date = 'true';
638 $no_orig_date = 'false';
641 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
643 if (ENABLE_SIMPLEPIE
) {
644 $entry_title = $item->get_title();
646 $entry_title = trim(strip_tags($item["title"]));
649 if (ENABLE_SIMPLEPIE
) {
650 $entry_link = $item->get_link();
652 // strange Magpie workaround
653 $entry_link = $item["link_"];
654 if (!$entry_link) $entry_link = $item["link"];
657 if (!$entry_title) continue;
659 $entry_link = strip_tags($entry_link);
661 if (ENABLE_SIMPLEPIE
) {
662 $entry_content = $item->get_description();
664 $entry_content = $item["content:escaped"];
666 if (!$entry_content) $entry_content = $item["content:encoded"];
667 if (!$entry_content) $entry_content = $item["content"];
668 if (!$entry_content) $entry_content = $item["atom_content"];
669 if (!$entry_content) $entry_content = $item["summary"];
670 if (!$entry_content) $entry_content = $item["description"];
673 if (is_array($entry_content)) {
674 $entry_content = $entry_content["encoded"];
675 if (!$entry_content) $entry_content = $entry_content["escaped"];
680 // print_r(htmlspecialchars($entry_content));
683 $entry_content_unescaped = $entry_content;
685 if (ENABLE_SIMPLEPIE
) {
686 $entry_comments = strip_tags($item->data
["comments"]);
687 if ($item->get_author()) {
688 $entry_author = $item->get_author()->get_name();
691 $entry_comments = strip_tags($item["comments"]);
693 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
695 if ($item['author']) {
697 if (is_array($item['author'])) {
699 if (!$entry_author) {
700 $entry_author = db_escape_string(strip_tags($item['author']['name']));
703 if (!$entry_author) {
704 $entry_author = db_escape_string(strip_tags($item['author']['email']));
708 if (!$entry_author) {
709 $entry_author = db_escape_string(strip_tags($item['author']));
714 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
716 $entry_guid = db_escape_string(strip_tags($entry_guid));
718 $result = db_query($link, "SELECT id FROM ttrss_entries
719 WHERE guid = '$entry_guid'");
721 $entry_content = db_escape_string($entry_content);
723 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
725 $entry_title = db_escape_string($entry_title);
726 $entry_link = db_escape_string($entry_link);
727 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
728 $entry_author = mb_substr($entry_author, 0, 250);
730 if (ENABLE_SIMPLEPIE
) {
731 $num_comments = 0; #FIXME#
733 $num_comments = db_escape_string($item["slash"]["comments"]);
736 if (!$num_comments) $num_comments = 0;
738 // parse <category> entries into tags
740 if (ENABLE_SIMPLEPIE
) {
742 $additional_tags = array();
743 $additional_tags_src = $item->get_categories();
745 if (is_array($additional_tags_src)) {
746 foreach ($additional_tags_src as $tobj) {
747 array_push($additional_tags, $tobj->get_term());
751 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
752 _debug("update_rss_feed: category tags:");
753 print_r($additional_tags);
758 $t_ctr = $item['category#'];
760 $additional_tags = false;
763 $additional_tags = false;
764 } else if ($t_ctr == 1) {
765 $additional_tags = array($item['category']);
767 $additional_tags = array();
768 for ($i = 0; $i <= $t_ctr; $i++
) {
769 if ($item["category#$i"]) {
770 array_push($additional_tags, $item["category#$i"]);
775 // parse <dc:subject> elements
777 $t_ctr = $item['dc']['subject#'];
780 $additional_tags = array($item['dc']['subject']);
781 } else if ($t_ctr > 1) {
782 $additional_tags = array();
783 for ($i = 0; $i <= $t_ctr; $i++
) {
784 if ($item['dc']["subject#$i"]) {
785 array_push($additional_tags, $item['dc']["subject#$i"]);
793 // $entry_content = sanitize_rss($entry_content);
795 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
796 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
799 db_query($link, "BEGIN");
801 if (db_num_rows($result) == 0) {
803 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
804 _debug("update_rss_feed: base guid not found");
807 // base post entry does not exist, create it
809 $result = db_query($link,
810 "INSERT INTO ttrss_entries
826 '$entry_timestamp_fmt',
835 // we keep encountering the entry in feeds, so we need to
836 // update date_entered column so that we don't get horrible
837 // dupes when the entry gets purged and reinserted again e.g.
838 // in the case of SLOW SLOW OMG SLOW updating feeds
840 $base_entry_id = db_fetch_result($result, 0, "id");
842 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
843 WHERE id = '$base_entry_id'");
846 // now it should exist, if not - bad luck then
848 $result = db_query($link, "SELECT
849 id,content_hash,no_orig_date,title,
850 substring(date_entered,1,19) as date_entered,
851 substring(updated,1,19) as updated,
855 WHERE guid = '$entry_guid'");
857 if (db_num_rows($result) == 1) {
859 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
860 _debug("update_rss_feed: base guid found, checking for user record");
863 // this will be used below in update handler
864 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
865 $orig_title = db_fetch_result($result, 0, "title");
866 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
867 $orig_date_entered = strtotime(db_fetch_result($result,
870 $ref_id = db_fetch_result($result, 0, "id");
872 // check for user post link to main table
874 // do we allow duplicate posts with same GUID in different feeds?
875 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
876 $dupcheck_qpart = "AND feed_id = '$feed'";
878 $dupcheck_qpart = "";
881 // error_reporting(0);
883 $article_filters = get_article_filters($filters, $entry_title,
884 $entry_content, $entry_link);
886 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
887 _debug("update_rss_feed: article filters: ");
888 if (count($article_filters) != 0) {
889 print_r($article_filters);
893 if (find_article_filter($article_filters, "filter")) {
897 // error_reporting (DEFAULT_ERROR_LEVEL);
899 $result = db_query($link,
900 "SELECT ref_id FROM ttrss_user_entries WHERE
901 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
904 // okay it doesn't exist - create user entry
905 if (db_num_rows($result) == 0) {
907 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
908 _debug("update_rss_feed: user record not found, creating...");
911 if (!find_article_filter($article_filters, 'catchup')) {
913 $last_read_qpart = 'NULL';
916 $last_read_qpart = 'NOW()';
919 if (find_article_filter($article_filters, 'mark')) {
925 if (find_article_filter($article_filters, 'publish')) {
928 $published = 'false';
931 $result = db_query($link,
932 "INSERT INTO ttrss_user_entries
933 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
934 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
935 $last_read_qpart, $marked, $published)");
938 $post_needs_update = false;
940 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
941 ($content_hash != $orig_content_hash)) {
942 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
943 $post_needs_update = true;
946 if (db_escape_string($orig_title) != $entry_title) {
947 $post_needs_update = true;
950 if ($orig_num_comments != $num_comments) {
951 $post_needs_update = true;
954 // this doesn't seem to be very reliable
956 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
957 // $post_needs_update = true;
960 // if post needs update, update it and mark all user entries
961 // linking to this post as updated
962 if ($post_needs_update) {
964 if (defined('DAEMON_EXTENDED_DEBUG')) {
965 _debug("update_rss_feed: post $entry_guid needs update...");
968 // print "<!-- post $orig_title needs update : $post_needs_update -->";
970 db_query($link, "UPDATE ttrss_entries
971 SET title = '$entry_title', content = '$entry_content',
972 content_hash = '$content_hash',
973 num_comments = '$num_comments'
974 WHERE id = '$ref_id'");
976 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
977 db_query($link, "UPDATE ttrss_user_entries
978 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
980 db_query($link, "UPDATE ttrss_user_entries
981 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
987 db_query($link, "COMMIT");
989 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
990 _debug("update_rss_feed: looking for tags...");
994 // <a href="..." rel="tag">Xorg</a>, //
998 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
999 $entry_content_unescaped, $entry_tags);
1001 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1002 print_r($entry_tags);
1003 print "<br/></p>"; */
1005 $entry_tags = $entry_tags[1];
1007 # check for manual tags
1009 $tag_filter = find_article_filter($article_filters, "tag");
1013 $manual_tags = trim_array(split(",", $tag_filter[1]));
1015 foreach ($manual_tags as $tag) {
1016 if (tag_is_valid($tag)) {
1017 array_push($entry_tags, $tag);
1022 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1023 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1025 if ($additional_tags && is_array($additional_tags)) {
1026 foreach ($additional_tags as $tag) {
1027 if (tag_is_valid($tag) &&
1028 array_search($tag, $boring_tags) === FALSE) {
1029 array_push($entry_tags, $tag);
1034 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1036 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1037 print_r($entry_tags);
1040 if (count($entry_tags) > 0) {
1042 db_query($link, "BEGIN");
1044 $result = db_query($link, "SELECT id,int_id
1045 FROM ttrss_entries,ttrss_user_entries
1046 WHERE guid = '$entry_guid'
1047 AND feed_id = '$feed' AND ref_id = id
1048 AND owner_uid = '$owner_uid'");
1050 if (db_num_rows($result) == 1) {
1052 $entry_id = db_fetch_result($result, 0, "id");
1053 $entry_int_id = db_fetch_result($result, 0, "int_id");
1055 foreach ($entry_tags as $tag) {
1057 $tag = sanitize_tag($tag);
1058 $tag = db_escape_string($tag);
1060 if (!tag_is_valid($tag)) continue;
1062 $result = db_query($link, "SELECT id FROM ttrss_tags
1063 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1064 owner_uid = '$owner_uid' LIMIT 1");
1066 // print db_fetch_result($result, 0, "id");
1068 if ($result && db_num_rows($result) == 0) {
1070 db_query($link, "INSERT INTO ttrss_tags
1071 (owner_uid,tag_name,post_int_id)
1072 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1076 db_query($link, "COMMIT");
1079 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1080 _debug("update_rss_feed: article processed");
1084 db_query($link, "UPDATE ttrss_feeds
1085 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1087 // db_query($link, "COMMIT");
1091 if (ENABLE_SIMPLEPIE
) {
1092 $error_msg = mb_substr($rss->error(), 0, 250);
1094 $error_msg = mb_substr(magpie_error(), 0, 250);
1097 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1098 _debug("update_rss_feed: error fetching feed: $error_msg");
1101 $error_msg = db_escape_string($error_msg);
1104 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1105 last_updated = NOW() WHERE id = '$feed'");
1108 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1109 _debug("update_rss_feed: done");
1114 function print_select($id, $default, $values, $attributes = "") {
1115 print "<select name=\"$id\" id=\"$id\" $attributes>";
1116 foreach ($values as $v) {
1122 print "<option$sel>$v</option>";
1127 function print_select_hash($id, $default, $values, $attributes = "") {
1128 print "<select name=\"$id\" id='$id' $attributes>";
1129 foreach (array_keys($values) as $v) {
1135 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1141 function get_article_filters($filters, $title, $content, $link) {
1144 if ($filters["title"]) {
1145 foreach ($filters["title"] as $filter) {
1146 $reg_exp = $filter["reg_exp"];
1147 $inverse = $filter["inverse"];
1148 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1149 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1151 array_push($matches, array($filter["action"], $filter["action_param"]));
1156 if ($filters["content"]) {
1157 foreach ($filters["content"] as $filter) {
1158 $reg_exp = $filter["reg_exp"];
1159 $inverse = $filter["inverse"];
1161 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1162 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1164 array_push($matches, array($filter["action"], $filter["action_param"]));
1169 if ($filters["both"]) {
1170 foreach ($filters["both"] as $filter) {
1171 $reg_exp = $filter["reg_exp"];
1172 $inverse = $filter["inverse"];
1175 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1176 array_push($matches, array($filter["action"], $filter["action_param"]));
1179 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1180 array_push($matches, array($filter["action"], $filter["action_param"]));
1186 if ($filters["link"]) {
1187 $reg_exp = $filter["reg_exp"];
1188 foreach ($filters["link"] as $filter) {
1189 $reg_exp = $filter["reg_exp"];
1190 $inverse = $filter["inverse"];
1192 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1193 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1195 array_push($matches, array($filter["action"], $filter["action_param"]));
1203 function find_article_filter($filters, $filter_name) {
1204 foreach ($filters as $f) {
1205 if ($f[0] == $filter_name) {
1212 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1213 $rtl_content = false, $last_updated = false, $last_error = false) {
1215 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1216 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1218 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1222 $rtl_tag = "dir=\"rtl\"";
1224 $rtl_tag = "dir=\"ltr\"";
1227 $error_notify_msg = "";
1230 $link_title = "Error: $last_error ($last_updated)";
1231 $error_notify_msg = "(Error)";
1232 } else if ($last_updated) {
1233 $link_title = "Updated: $last_updated";
1236 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1237 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1239 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1240 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1244 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1249 $fctr_class = "class=\"invisible\"";
1252 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1253 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1255 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1256 print "<div class=\"feedExtInfo\">
1257 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1264 function getmicrotime() {
1265 list($usec, $sec) = explode(" ",microtime());
1266 return ((float)$usec +
(float)$sec);
1269 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1270 foreach ($values as $v) {
1277 if ($v == $true_is) {
1278 $sel .= " value=\"1\"";
1280 $sel .= " value=\"0\"";
1283 print "<input class=\"noborder\"
1284 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1289 function initialize_user_prefs($link, $uid) {
1291 $uid = db_escape_string($uid);
1293 db_query($link, "BEGIN");
1295 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1297 $u_result = db_query($link, "SELECT pref_name
1298 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1300 $active_prefs = array();
1302 while ($line = db_fetch_assoc($u_result)) {
1303 array_push($active_prefs, $line["pref_name"]);
1306 while ($line = db_fetch_assoc($result)) {
1307 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1308 // print "adding " . $line["pref_name"] . "<br>";
1310 db_query($link, "INSERT INTO ttrss_user_prefs
1311 (owner_uid,pref_name,value) VALUES
1312 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1317 db_query($link, "COMMIT");
1321 function lookup_user_id($link, $user) {
1323 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1326 if (db_num_rows($result) == 1) {
1327 return db_fetch_result($result, 0, "id");
1333 function http_authenticate_user($link) {
1335 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1337 if (!$_SERVER["PHP_AUTH_USER"]) {
1339 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1340 header('HTTP/1.0 401 Unauthorized');
1344 $auth_result = authenticate_user($link,
1345 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1347 if (!$auth_result) {
1348 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1349 header('HTTP/1.0 401 Unauthorized');
1357 function authenticate_user($link, $login, $password, $force_auth = false) {
1359 if (!SINGLE_USER_MODE
) {
1361 $pwd_hash = 'SHA1:' . sha1($password);
1363 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1364 $query = "SELECT id,login,access_level
1365 FROM ttrss_users WHERE
1368 $query = "SELECT id,login,access_level
1369 FROM ttrss_users WHERE
1370 login = '$login' AND pwd_hash = '$pwd_hash'";
1373 $result = db_query($link, $query);
1375 if (db_num_rows($result) == 1) {
1376 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1377 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1378 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1380 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1383 $user_theme = get_user_theme_path($link);
1385 $_SESSION["theme"] = $user_theme;
1386 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1388 initialize_user_prefs($link, $_SESSION["uid"]);
1397 $_SESSION["uid"] = 1;
1398 $_SESSION["name"] = "admin";
1400 $user_theme = get_user_theme_path($link);
1402 $_SESSION["theme"] = $user_theme;
1403 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1405 initialize_user_prefs($link, $_SESSION["uid"]);
1411 function make_password($length = 8) {
1414 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1418 while ($i < $length) {
1419 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1421 if (!strstr($password, $char)) {
1429 // this is called after user is created to initialize default feeds, labels
1432 // user preferences are checked on every login, not here
1434 function initialize_user($link, $uid) {
1436 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1437 values ('$uid','unread = true', 'Unread articles')");
1439 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1440 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1442 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1443 values ('$uid', 'Tiny Tiny RSS: New Releases',
1444 'http://tt-rss.spb.ru/releases.rss')");
1446 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1447 values ('$uid', 'Tiny Tiny RSS: Forum',
1448 'http://tt-rss.spb.ru/forum/rss.php')");
1451 function logout_user() {
1453 if (isset($_COOKIE[session_name()])) {
1454 setcookie(session_name(), '', time()-42000, '/');
1458 function get_script_urlpath() {
1459 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1462 function validate_session($link) {
1463 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1464 if ($_SESSION["ip_address"]) {
1465 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1466 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1472 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1474 //print_r($_SESSION);
1476 if (time() > $_SESSION["cookie_lifetime"]) {
1484 function login_sequence($link, $mobile = false) {
1485 if (!SINGLE_USER_MODE
) {
1487 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1488 $swu = db_escape_string($_REQUEST["swu"]);
1490 $_SESSION["prefs_cache"] = false;
1491 return authenticate_user($link, $swu, null, true);
1495 $login_action = $_POST["login_action"];
1497 # try to authenticate user if called from login form
1498 if ($login_action == "do_login") {
1499 $login = $_POST["login"];
1500 $password = $_POST["password"];
1501 $remember_me = $_POST["remember_me"];
1503 if (authenticate_user($link, $login, $password)) {
1504 $_POST["password"] = "";
1506 $_SESSION["language"] = $_POST["language"];
1508 header("Location: " . $_SERVER["REQUEST_URI"]);
1513 $_SESSION["login_error_msg"] = "Incorrect username or password";
1517 // print session_id();
1518 // print_r($_SESSION);
1520 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1521 render_login_form($link, $mobile);
1524 /* bump login timestamp */
1525 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1528 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME
> 0) {
1529 setcookie("ttrss_lang", $_SESSION["language"],
1530 time() + SESSION_COOKIE_LIFETIME
);
1535 return authenticate_user($link, "admin", null);
1539 function truncate_string($str, $max_len) {
1540 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1541 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1547 function get_user_theme_path($link) {
1548 $result = db_query($link, "SELECT theme_path
1550 ttrss_themes,ttrss_users
1551 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1552 if (db_num_rows($result) != 0) {
1553 return db_fetch_result($result, 0, "theme_path");
1559 function smart_date_time($timestamp) {
1560 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1561 return date("G:i", $timestamp);
1562 } else if (date("Y", $timestamp) == date("Y")) {
1563 return date("M d, G:i", $timestamp);
1565 return date("Y/m/d, G:i", $timestamp);
1569 function smart_date($timestamp) {
1570 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1572 } else if (date("Y", $timestamp) == date("Y")) {
1573 return date("D m", $timestamp);
1575 return date("Y/m/d", $timestamp);
1579 function sql_bool_to_string($s) {
1580 if ($s == "t" ||
$s == "1") {
1587 function sql_bool_to_bool($s) {
1588 if ($s == "t" ||
$s == "1") {
1596 function toggleEvenOdd($a) {
1603 function sanity_check($link) {
1608 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1609 $schema_version = db_fetch_result($result, 0, "schema_version");
1611 if ($schema_version != SCHEMA_VERSION
) {
1615 if (DB_TYPE
== "mysql") {
1616 $result = db_query($link, "SELECT true", false);
1617 if (db_num_rows($result) != 1) {
1622 error_reporting (DEFAULT_ERROR_LEVEL
);
1624 if ($error_code != 0) {
1625 print_error_xml($error_code);
1632 function file_is_locked($filename) {
1634 $fp = fopen($filename, "r");
1635 error_reporting(DEFAULT_ERROR_LEVEL
);
1637 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1638 flock($fp, LOCK_UN
);
1648 function make_lockfile($filename) {
1649 $fp = fopen($filename, "w");
1651 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1658 function make_stampfile($filename) {
1659 $fp = fopen($filename, "w");
1661 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1662 fwrite($fp, time() . "\n");
1663 flock($fp, LOCK_UN
);
1671 function read_stampfile($filename) {
1674 $fp = fopen($filename, "r");
1675 error_reporting (DEFAULT_ERROR_LEVEL
);
1677 if (flock($fp, LOCK_EX
)) {
1678 $stamp = fgets($fp);
1679 flock($fp, LOCK_UN
);
1687 function sql_random_function() {
1688 if (DB_TYPE
== "mysql") {
1695 function catchup_feed($link, $feed, $cat_view) {
1697 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1702 $cat_qpart = "cat_id = '$feed'";
1704 $cat_qpart = "cat_id IS NULL";
1707 $tmp_result = db_query($link, "SELECT id
1708 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1711 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1713 $tmp_feed = $tmp_line["id"];
1715 db_query($link, "UPDATE ttrss_user_entries
1716 SET unread = false,last_read = NOW()
1717 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1720 } else if ($feed > 0) {
1722 $tmp_result = db_query($link, "SELECT id
1723 FROM ttrss_feeds WHERE parent_feed = '$feed'
1724 ORDER BY cat_id,title");
1726 $parent_ids = array();
1728 if (db_num_rows($tmp_result) > 0) {
1729 while ($p = db_fetch_assoc($tmp_result)) {
1730 array_push($parent_ids, "feed_id = " . $p["id"]);
1733 $children_qpart = implode(" OR ", $parent_ids);
1735 db_query($link, "UPDATE ttrss_user_entries
1736 SET unread = false,last_read = NOW()
1737 WHERE (feed_id = '$feed' OR $children_qpart)
1738 AND owner_uid = " . $_SESSION["uid"]);
1741 db_query($link, "UPDATE ttrss_user_entries
1742 SET unread = false,last_read = NOW()
1743 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1746 } else if ($feed < 0 && $feed > -10) { // special, like starred
1749 db_query($link, "UPDATE ttrss_user_entries
1750 SET unread = false,last_read = NOW()
1751 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1755 db_query($link, "UPDATE ttrss_user_entries
1756 SET unread = false,last_read = NOW()
1757 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1762 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1764 if (DB_TYPE
== "pgsql") {
1765 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1767 $match_part .= " AND date_entered > DATE_SUB(NOW(),
1768 INTERVAL $intl HOUR) ";
1771 db_query($link, "UPDATE ttrss_user_entries
1772 SET unread = false,last_read = NOW()
1773 WHERE $match_part AND owner_uid = ".$_SESSION["uid"]);
1776 } else if ($feed < -10) { // label
1778 // TODO make this more efficient
1780 $label_id = -$feed - 11;
1782 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1783 WHERE id = '$label_id'");
1786 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1788 db_query($link, "BEGIN");
1790 $tmp2_result = db_query($link,
1794 ttrss_user_entries,ttrss_entries,ttrss_feeds
1796 ref_id = ttrss_entries.id AND
1797 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1799 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1801 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1802 db_query($link, "UPDATE
1805 unread = false, last_read = NOW()
1807 int_id = " . $tmp_line["int_id"]);
1810 db_query($link, "COMMIT");
1812 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1813 SET unread = false,last_read = NOW()
1816 AND owner_uid = ".$_SESSION["uid"]); */
1820 db_query($link, "BEGIN");
1822 $tag_name = db_escape_string($feed);
1824 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1825 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1827 while ($line = db_fetch_assoc($result)) {
1828 db_query($link, "UPDATE ttrss_user_entries SET
1829 unread = false, last_read = NOW()
1830 WHERE int_id = " . $line["post_int_id"]);
1832 db_query($link, "COMMIT");
1836 function update_generic_feed($link, $feed, $cat_view) {
1840 $cat_qpart = "cat_id = '$feed'";
1842 $cat_qpart = "cat_id IS NULL";
1845 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1846 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1848 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1849 $feed_url = $tmp_line["feed_url"];
1850 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1854 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1855 WHERE id = '$feed'");
1856 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1857 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1861 function getAllCounters($link, $omode = "flc") {
1862 /* getLabelCounters($link);
1863 getFeedCounters($link);
1864 getTagCounters($link);
1865 getGlobalCounters($link);
1866 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1867 getCategoryCounters($link);
1870 if (!$omode) $omode = "flc";
1872 getGlobalCounters($link);
1874 if (strchr($omode, "l")) getLabelCounters($link);
1875 if (strchr($omode, "f")) getFeedCounters($link);
1876 if (strchr($omode, "t")) getTagCounters($link);
1877 if (strchr($omode, "c")) {
1878 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1879 getCategoryCounters($link);
1884 function getCategoryCounters($link) {
1885 # two special categories are -1 and -2 (all virtuals; all labels)
1887 $ctr = getCategoryUnread($link, -1);
1889 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
1891 $ctr = getCategoryUnread($link, -2);
1893 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
1895 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1896 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1897 AND unread = true)) AS unread FROM ttrss_feeds
1899 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1901 while ($line = db_fetch_assoc($result)) {
1902 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1903 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1904 $line["unread"]."\"/>";
1908 function getCategoryUnread($link, $cat) {
1913 $cat_query = "cat_id = '$cat'";
1915 $cat_query = "cat_id IS NULL";
1918 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1920 AND owner_uid = " . $_SESSION["uid"]);
1922 $cat_feeds = array();
1923 while ($line = db_fetch_assoc($result)) {
1924 array_push($cat_feeds, "feed_id = " . $line["id"]);
1927 if (count($cat_feeds) == 0) return 0;
1929 $match_part = implode(" OR ", $cat_feeds);
1931 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1932 FROM ttrss_user_entries
1933 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1937 # this needs to be rewritten
1938 while ($line = db_fetch_assoc($result)) {
1939 $unread +
= $line["unread"];
1943 } else if ($cat == -1) {
1944 return getFeedUnread($link, -1) +
getFeedUnread($link, -2) +
getFeedUnread($link, -3);
1945 } else if ($cat == -2) {
1947 $rv = getLabelCounters($link, false, true);
1950 foreach (array_keys($rv) as $k) {
1952 $ctr +
= $rv[$k]["counter"];
1960 function getFeedUnread($link, $feed, $is_cat = false) {
1961 $n_feed = sprintf("%d", $feed);
1964 return getCategoryUnread($link, $n_feed);
1965 } else if ($n_feed == -1) {
1966 $match_part = "marked = true";
1967 } else if ($n_feed == -2) {
1968 $match_part = "published = true";
1969 } else if ($n_feed == -3) {
1970 $match_part = "unread = true";
1972 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1974 if (DB_TYPE
== "pgsql") {
1975 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1977 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1980 } else if ($n_feed > 0) {
1982 $result = db_query($link, "SELECT id FROM ttrss_feeds
1983 WHERE parent_feed = '$n_feed'
1985 AND owner_uid = " . $_SESSION["uid"]);
1987 if (db_num_rows($result) > 0) {
1989 $linked_feeds = array();
1990 while ($line = db_fetch_assoc($result)) {
1991 array_push($linked_feeds, "feed_id = " . $line["id"]);
1994 array_push($linked_feeds, "feed_id = $n_feed");
1996 $match_part = implode(" OR ", $linked_feeds);
1998 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1999 FROM ttrss_user_entries
2000 WHERE unread = true AND ($match_part)
2001 AND owner_uid = " . $_SESSION["uid"]);
2005 # this needs to be rewritten
2006 while ($line = db_fetch_assoc($result)) {
2007 $unread +
= $line["unread"];
2013 $match_part = "feed_id = '$n_feed'";
2015 } else if ($feed < -10) {
2017 $label_id = -$feed - 11;
2019 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2020 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2022 $match_part = db_fetch_result($result, 0, "sql_exp");
2027 $result = db_query($link, "SELECT count(int_id) AS unread
2028 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2029 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2030 ttrss_user_entries.ref_id = ttrss_entries.id AND
2031 ttrss_feeds.hidden = false AND
2032 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2036 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2037 FROM ttrss_tags,ttrss_user_entries
2038 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
2039 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2042 $unread = db_fetch_result($result, 0, "unread");
2047 /* FIXME this needs reworking */
2049 function getGlobalUnread($link, $user_id = false) {
2052 $user_id = $_SESSION["uid"];
2055 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2056 WHERE unread = true AND
2057 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2058 ttrss_user_entries.ref_id = ttrss_entries.id AND
2060 ttrss_user_entries.owner_uid = '$user_id'");
2061 $c_id = db_fetch_result($result, 0, "c_id");
2065 function getGlobalCounters($link, $global_unread = -1) {
2066 if ($global_unread == -1) {
2067 $global_unread = getGlobalUnread($link);
2069 print "<counter type=\"global\" id='global-unread'
2070 counter='$global_unread'/>";
2072 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2073 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2075 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2077 print "<counter type=\"global\" id='subscribed-feeds'
2078 counter='$subscribed_feeds'/>";
2082 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2085 if (!$_SESSION["tctr_last_value"]) {
2086 $_SESSION["tctr_last_value"] = array();
2090 $old_counters = $_SESSION["tctr_last_value"];
2092 $tctrs_modified = false;
2094 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2095 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2096 ttrss_user_entries.ref_id = ttrss_entries.id AND
2097 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2098 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2100 select tag_name,0 as count FROM ttrss_tags
2101 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2103 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2104 FROM ttrss_user_entries WHERE int_id = post_int_id
2105 AND unread = true)) AS count FROM ttrss_tags
2106 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2107 ORDER BY count DESC LIMIT 55");
2111 while ($line = db_fetch_assoc($result)) {
2112 $tags[$line["tag_name"]] +
= $line["count"];
2115 foreach (array_keys($tags) as $tag) {
2116 $unread = $tags[$tag];
2118 $tag = htmlspecialchars($tag);
2120 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
2121 $old_counters[$tag] = $unread;
2122 $tctrs_modified = true;
2123 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2128 if ($smart_mode && $tctrs_modified) {
2129 $_SESSION["tctr_last_value"] = $old_counters;
2134 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
2137 if (!$_SESSION["lctr_last_value"]) {
2138 $_SESSION["lctr_last_value"] = array();
2144 $old_counters = $_SESSION["lctr_last_value"];
2145 $lctrs_modified = false;
2147 $count = getFeedUnread($link, -1);
2150 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2152 $ret_arr["-1"]["counter"] = $count;
2153 $ret_arr["-1"]["description"] = __("Starred articles");
2156 $count = getFeedUnread($link, -2);
2159 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2161 $ret_arr["-2"]["counter"] = $count;
2162 $ret_arr["-2"]["description"] = __("Published articles");
2165 $count = getFeedUnread($link, -3);
2168 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2170 $ret_arr["-3"]["counter"] = $count;
2171 $ret_arr["-3"]["description"] = __("Fresh articles");
2175 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2176 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2178 while ($line = db_fetch_assoc($result)) {
2180 $id = -$line["id"] - 11;
2182 $label_name = $line["description"];
2184 error_reporting (0);
2186 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
2187 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
2188 ttrss_feeds.hidden = false AND
2189 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2190 ttrss_user_entries.ref_id = ttrss_entries.id AND
2191 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
2193 $count = db_fetch_result($tmp_result, 0, "count");
2195 if (!$smart_mode ||
$old_counters[$id] != $count) {
2196 $old_counters[$id] = $count;
2197 $lctrs_modified = true;
2199 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2201 $ret_arr[$id]["counter"] = $count;
2202 $ret_arr[$id]["description"] = $label_name;
2206 error_reporting (DEFAULT_ERROR_LEVEL
);
2209 if ($smart_mode && $lctrs_modified) {
2210 $_SESSION["lctr_last_value"] = $old_counters;
2216 /* function getFeedCounter($link, $id) {
2218 $result = db_query($link, "SELECT
2219 count(id) as count,last_error
2220 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2221 WHERE feed_id = '$id' AND unread = true
2222 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2223 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2225 $count = db_fetch_result($result, 0, "count");
2226 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2228 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2231 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2234 if (!$_SESSION["fctr_last_value"]) {
2235 $_SESSION["fctr_last_value"] = array();
2239 $old_counters = $_SESSION["fctr_last_value"];
2241 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2242 SUBSTRING(last_updated,1,19) AS last_updated,
2244 FROM ttrss_entries,ttrss_user_entries
2245 WHERE feed_id = ttrss_feeds.id AND
2246 ttrss_user_entries.ref_id = ttrss_entries.id
2247 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2248 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2249 AND parent_feed IS NULL"); */
2251 $result = db_query($link, "SELECT ttrss_feeds.id,
2252 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2254 COUNT(ttrss_entries.id) AS count
2256 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2257 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2258 AND ttrss_user_entries.unread = true)
2259 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2260 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2261 AND parent_feed IS NULL
2262 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error");
2264 $fctrs_modified = false;
2266 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2268 while ($line = db_fetch_assoc($result)) {
2271 $count = $line["count"];
2272 $last_error = htmlspecialchars($line["last_error"]);
2274 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2275 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2277 $last_updated = date($short_date, strtotime($line["last_updated"]));
2280 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2282 $tmp_result = db_query($link,
2283 "SELECT id,COUNT(unread) AS unread
2284 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2285 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2286 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2288 if (db_num_rows($tmp_result) > 0) {
2289 while ($l = db_fetch_assoc($tmp_result)) {
2290 $count +
= $l["unread"];
2294 if (!$smart_mode ||
$old_counters[$id] != $count) {
2295 $old_counters[$id] = $count;
2296 $fctrs_modified = true;
2299 $error_part = "error=\"$last_error\"";
2305 $has_img_part = "hi=\"$has_img\"";
2310 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
2314 if ($smart_mode && $fctrs_modified) {
2315 $_SESSION["fctr_last_value"] = $old_counters;
2319 function get_script_dt_add() {
2320 if (strpos(VERSION
, ".99") === false) {
2327 function get_pgsql_version($link) {
2328 $result = db_query($link, "SELECT version() AS version");
2329 $version = split(" ", db_fetch_result($result, 0, "version"));
2333 function print_error_xml($code, $add_msg = "") {
2336 $error_msg = $ERRORS[$code];
2339 $error_msg = "$error_msg; $add_msg";
2342 print "<rpc-reply>";
2343 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2344 print "</rpc-reply>";
2347 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2348 $auth_login = '', $auth_pass = '') {
2350 # check for feed:http://url
2351 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2353 # check for feed://URL
2354 if (strpos($feed_link, "//") === 0) {
2355 $feed_link = "http:$feed_link";
2358 if ($feed_link == "") return;
2360 if ($cat_id == "0" ||
!$cat_id) {
2361 $cat_qpart = "NULL";
2363 $cat_qpart = "'$cat_id'";
2366 $result = db_query($link,
2367 "SELECT id FROM ttrss_feeds
2368 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2370 if (db_num_rows($result) == 0) {
2372 $result = db_query($link,
2373 "INSERT INTO ttrss_feeds
2374 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2375 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2376 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2378 $result = db_query($link,
2379 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2380 AND owner_uid = " . $_SESSION["uid"]);
2382 $feed_id = db_fetch_result($result, 0, "id");
2385 update_rss_feed($link, $feed_link, $feed_id, true);
2394 function print_feed_select($link, $id, $default_id = "",
2395 $attributes = "", $include_all_feeds = true) {
2397 print "<select id=\"$id\" name=\"$id\" $attributes>";
2398 if ($include_all_feeds) {
2399 print "<option value=\"0\">".__('All feeds')."</option>";
2402 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2403 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2405 if (db_num_rows($result) > 0 && $include_all_feeds) {
2406 print "<option disabled>--------</option>";
2409 while ($line = db_fetch_assoc($result)) {
2410 if ($line["id"] == $default_id) {
2411 $is_selected = "selected";
2415 printf("<option $is_selected value='%d'>%s</option>",
2416 $line["id"], htmlspecialchars($line["title"]));
2422 function print_feed_cat_select($link, $id, $default_id = "",
2423 $attributes = "", $include_all_cats = true) {
2425 print "<select id=\"$id\" name=\"$id\" $attributes>";
2427 if ($include_all_cats) {
2428 print "<option value=\"0\">".__('Uncategorized')."</option>";
2431 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2432 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2434 if (db_num_rows($result) > 0 && $include_all_cats) {
2435 print "<option disabled>--------</option>";
2438 while ($line = db_fetch_assoc($result)) {
2439 if ($line["id"] == $default_id) {
2440 $is_selected = "selected";
2444 printf("<option $is_selected value='%d'>%s</option>",
2445 $line["id"], htmlspecialchars($line["title"]));
2451 function checkbox_to_sql_bool($val) {
2452 return ($val == "on") ?
"true" : "false";
2455 function getFeedCatTitle($link, $id) {
2457 return __("Special");
2458 } else if ($id < -10) {
2459 return __("Labels");
2460 } else if ($id > 0) {
2461 $result = db_query($link, "SELECT ttrss_feed_categories.title
2462 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2463 cat_id = ttrss_feed_categories.id");
2464 if (db_num_rows($result) == 1) {
2465 return db_fetch_result($result, 0, "title");
2467 return __("Uncategorized");
2470 return "getFeedCatTitle($id) failed";
2475 function getFeedTitle($link, $id) {
2477 return __("Starred articles");
2478 } else if ($id == -2) {
2479 return __("Published articles");
2480 } else if ($id == -3) {
2481 return __("Fresh articles");
2482 } else if ($id < -10) {
2483 $label_id = -10 - $id;
2484 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2485 if (db_num_rows($result) == 1) {
2486 return db_fetch_result($result, 0, "description");
2488 return "Unknown label ($label_id)";
2491 } else if ($id > 0) {
2492 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2493 if (db_num_rows($result) == 1) {
2494 return db_fetch_result($result, 0, "title");
2496 return "Unknown feed ($id)";
2499 return "getFeedTitle($id) failed";
2504 function get_session_cookie_name() {
2505 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2508 function print_init_params($link) {
2509 print "<init-params>";
2510 if ($_SESSION["stored-params"]) {
2511 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2513 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2514 print "<param key=\"$key\" value=\"$value\"/>";
2519 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2520 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2521 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2523 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2524 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2526 print "<param key=\"hide_read_feeds\" value=\"" .
2527 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2529 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2530 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2532 print "<param key=\"confirm_feed_catchup\" value=\"" .
2533 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2535 print "<param key=\"cdm_auto_catchup\" value=\"" .
2536 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2538 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2540 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2542 print "<param key=\"default_view_mode\" value=\"" .
2543 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2545 print "<param key=\"default_view_limit\" value=\"" .
2546 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2548 print "<param key=\"prefs_active_tab\" value=\"" .
2549 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2551 print "<param key=\"infobox_disable_overlay\" value=\"" .
2552 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2554 print "<param key=\"icons_location\" value=\"" .
2557 print "</init-params>";
2560 function print_runtime_info($link) {
2561 print "<runtime-info>";
2562 if (ENABLE_UPDATE_DAEMON
) {
2563 print "<param key=\"daemon_is_running\" value=\"".
2564 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2566 if ($_SESSION["daemon_stamp_check"] +
600 < time()) {
2568 $stamp = (int)read_stampfile("update_daemon.stamp");
2571 if ($stamp +
86400*3 < time()) {
2572 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2574 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2577 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2579 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2582 $_SESSION["daemon_stamp_check"] = time();
2586 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2588 if ($_SESSION["last_version_check"] +
7200 < time()) {
2589 $new_version_details = check_for_update($link);
2591 print "<param key=\"new_version_available\" value=\"".
2592 sprintf("%d", $new_version_details != ""). "\"/>";
2594 $_SESSION["last_version_check"] = time();
2598 print "</runtime-info>";
2601 function getSearchSql($search, $match_on) {
2603 $search_query_part = "";
2605 $keywords = split(" ", $search);
2606 $query_keywords = array();
2608 if ($match_on == "both") {
2610 foreach ($keywords as $k) {
2611 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2612 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2615 $search_query_part = implode("AND", $query_keywords) . " AND ";
2617 } else if ($match_on == "title") {
2619 foreach ($keywords as $k) {
2620 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2623 $search_query_part = implode("AND", $query_keywords) . " AND ";
2625 } else if ($match_on == "content") {
2627 foreach ($keywords as $k) {
2628 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2632 $search_query_part = implode("AND", $query_keywords);
2634 return $search_query_part;
2637 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2639 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2643 $search_query_part = getSearchSql($search, $match_on);
2644 $search_query_part .= " AND ";
2647 $search_query_part = "";
2650 $view_query_part = "";
2652 if ($view_mode == "adaptive") {
2654 $view_query_part = " ";
2655 } else if ($feed != -1) {
2656 $unread = getFeedUnread($link, $feed, $cat_view);
2658 $view_query_part = " unread = true AND ";
2663 if ($view_mode == "marked") {
2664 $view_query_part = " marked = true AND ";
2667 if ($view_mode == "unread") {
2668 $view_query_part = " unread = true AND ";
2672 $limit_query_part = "LIMIT " . $limit;
2675 $vfeed_query_part = "";
2677 // override query strategy and enable feed display when searching globally
2678 if ($search && $search_mode == "all_feeds") {
2679 $query_strategy_part = "ttrss_entries.id > 0";
2680 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2681 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2682 $query_strategy_part = "ttrss_entries.id > 0";
2683 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2684 id = feed_id) as feed_title,";
2685 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2687 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2689 $tmp_result = false;
2692 $tmp_result = db_query($link, "SELECT id
2693 FROM ttrss_feeds WHERE cat_id = '$feed'");
2695 $tmp_result = db_query($link, "SELECT id
2696 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2697 WHERE id = '$feed') AND id != '$feed'");
2700 $cat_siblings = array();
2702 if (db_num_rows($tmp_result) > 0) {
2703 while ($p = db_fetch_assoc($tmp_result)) {
2704 array_push($cat_siblings, "feed_id = " . $p["id"]);
2707 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2708 $feed, implode(" OR ", $cat_siblings));
2711 $query_strategy_part = "ttrss_entries.id > 0";
2714 } else if ($feed >= 0) {
2719 $query_strategy_part = "cat_id = '$feed'";
2721 $query_strategy_part = "cat_id IS NULL";
2724 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2727 $tmp_result = db_query($link, "SELECT id
2728 FROM ttrss_feeds WHERE parent_feed = '$feed'
2729 ORDER BY cat_id,title");
2731 $parent_ids = array();
2733 if (db_num_rows($tmp_result) > 0) {
2734 while ($p = db_fetch_assoc($tmp_result)) {
2735 array_push($parent_ids, "feed_id = " . $p["id"]);
2738 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2739 $feed, implode(" OR ", $parent_ids));
2741 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2743 $query_strategy_part = "feed_id = '$feed'";
2746 } else if ($feed == -1) { // starred virtual feed
2747 $query_strategy_part = "marked = true";
2748 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2749 } else if ($feed == -2) { // published virtual feed
2750 $query_strategy_part = "published = true";
2751 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2752 } else if ($feed == -3) { // fresh virtual feed
2753 $query_strategy_part = "unread = true";
2755 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2757 if (DB_TYPE
== "pgsql") {
2758 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2760 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2763 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2764 } else if ($feed <= -10) { // labels
2765 $label_id = -$feed - 11;
2767 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2768 WHERE id = '$label_id'");
2770 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2772 if (!$query_strategy_part) {
2776 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2778 $query_strategy_part = "id > 0"; // dumb
2781 if (get_pref($link, 'REVERSE_HEADLINES')) {
2782 $order_by = "updated";
2784 $order_by = "updated DESC";
2787 if ($override_order) {
2788 $order_by = $override_order;
2793 if ($search && $search_mode == "all_feeds") {
2794 $feed_title = __("Search results")." ($search)";
2795 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2796 $feed_title = __("Search results")." ($search, $feed)";
2797 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2798 $feed_title = $feed;
2799 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2804 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2805 WHERE id = '$feed' AND owner_uid = $owner_uid");
2806 $feed_title = db_fetch_result($result, 0, "title");
2808 $feed_title = __("Uncategorized");
2812 $feed_title = __("Searched for")." $search ($feed_title)";
2817 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2818 WHERE id = '$feed' AND owner_uid = $owner_uid");
2820 $feed_title = db_fetch_result($result, 0, "title");
2821 $feed_site_url = db_fetch_result($result, 0, "site_url");
2822 $last_error = db_fetch_result($result, 0, "last_error");
2825 $feed_title = __("Searched for") . " $search ($feed_title)";
2829 } else if ($feed == -1) {
2830 $feed_title = __("Starred articles");
2831 } else if ($feed == -2) {
2832 $feed_title = __("Published articles");
2833 } else if ($feed == -3) {
2834 $feed_title = __("Fresh articles");
2835 } else if ($feed < -10) {
2836 $label_id = -$feed - 11;
2837 $result = db_query($link, "SELECT description FROM ttrss_labels
2838 WHERE id = '$label_id'");
2839 $feed_title = db_fetch_result($result, 0, "description");
2842 $feed_title = __("Searched for") . " $search ($feed_title)";
2848 if ($feed < -10) error_reporting (0);
2850 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2853 $feed_kind = "Feeds";
2855 $feed_kind = "Labels";
2858 $content_query_part = "content as content_preview,";
2860 if ($limit_query_part) {
2861 $offset_query_part = "OFFSET $offset";
2866 ttrss_entries.id,ttrss_entries.title,
2868 unread,feed_id,marked,published,link,last_read,
2869 SUBSTRING(last_read,1,19) as last_read_noms,
2872 SUBSTRING(updated,1,19) as updated_noms,
2875 ttrss_entries,ttrss_user_entries,ttrss_feeds
2877 ttrss_feeds.hidden = false AND
2878 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2879 ttrss_user_entries.ref_id = ttrss_entries.id AND
2880 ttrss_user_entries.owner_uid = '$owner_uid' AND
2883 $query_strategy_part ORDER BY $order_by
2884 $limit_query_part $offset_query_part";
2886 $result = db_query($link, $query);
2888 if ($_GET["debug"]) print $query;
2893 $feed_kind = "Tags";
2895 $result = db_query($link, "SELECT
2897 ttrss_entries.id as id,title,
2900 marked,link,last_read,
2901 SUBSTRING(last_read,1,19) as last_read_noms,
2904 SUBSTRING(updated,1,19) as updated_noms
2906 ttrss_entries,ttrss_user_entries,ttrss_tags
2908 ref_id = ttrss_entries.id AND
2909 ttrss_user_entries.owner_uid = '$owner_uid' AND
2910 post_int_id = int_id AND tag_name = '$feed' AND
2913 $query_strategy_part ORDER BY $order_by
2914 $limit_query_part");
2917 return array($result, $feed_title, $feed_site_url, $last_error);
2921 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
2922 $search, $search_mode, $match_on) {
2924 $qfh_ret = queryFeedHeadlines($link, $feed,
2925 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
2928 $result = $qfh_ret[0];
2929 $feed_title = htmlspecialchars($qfh_ret[1]);
2930 $feed_site_url = $qfh_ret[2];
2931 $last_error = $qfh_ret[3];
2933 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
2935 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
2936 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
2937 <rss version=\"2.0\">
2939 <title>$feed_title</title>
2940 <link>$feed_site_url</link>
2941 <description>Feed generated by Tiny Tiny RSS</description>";
2943 while ($line = db_fetch_assoc($result)) {
2945 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
2946 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2948 $tags = get_article_tags($link, $line["id"], $owner_uid);
2950 foreach ($tags as $tag) {
2951 print "<category>" . htmlspecialchars($tag) . "</category>";
2954 $rfc822_date = date('r', strtotime($line["updated"]));
2956 print "<pubDate>$rfc822_date</pubDate>";
2959 htmlspecialchars($line["title"]) . "</title>";
2961 print "<description><![CDATA[" .
2962 $line["content_preview"] . "]]></description>";
2967 print "</channel></rss>";
2971 function getCategoryTitle($link, $cat_id) {
2973 if ($cat_id == -1) {
2974 return __("Special");
2975 } else if ($cat_id == -2) {
2976 return __("Labels");
2979 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2982 if (db_num_rows($result) == 1) {
2983 return db_fetch_result($result, 0, "title");
2985 return "Uncategorized";
2990 // http://ru2.php.net/strip-tags
2992 function strip_tags_long($textstring, $allowed){
2993 while($textstring != strip_tags($textstring, $allowed))
2995 while (strlen($textstring) != 0)
2997 if (strlen($textstring) > 1024) {
3000 $otherlen = strlen($textstring);
3002 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3003 $safetext .= $temptext;
3004 $textstring = substr_replace($textstring,'',0,$otherlen);
3006 $textstring = $safetext;
3012 function sanitize_rss($link, $str, $force_strip_tags = false) {
3015 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
3017 global $tw_paranoya_setup;
3019 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
3021 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
3022 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3028 function send_headlines_digests($link, $limit = 100) {
3030 if (!DIGEST_ENABLE
) return false;
3032 $user_limit = DIGEST_EMAIL_LIMIT
;
3035 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3037 if (DB_TYPE
== "pgsql") {
3038 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3039 } else if (DB_TYPE
== "mysql") {
3040 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3043 $result = db_query($link, "SELECT id,email FROM ttrss_users
3044 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3046 while ($line = db_fetch_assoc($result)) {
3047 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3048 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3050 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3051 $digest = $tuple[0];
3052 $headlines_count = $tuple[1];
3054 if ($headlines_count > 0) {
3055 $rc = mail($line["login"] . " <" . $line["email"] . ">",
3056 "[tt-rss] New headlines for last 24 hours", $digest,
3057 "From: " . MAIL_FROM
. "\n".
3058 "Content-Type: text/plain; charset=\"utf-8\"\n".
3059 "Content-Transfer-Encoding: 8bit\n");
3061 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3062 WHERE id = " . $line["id"]);
3064 print "No headlines\n";
3069 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
3073 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3074 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
3075 $tmp .= "=======================================================\n\n";
3077 if (DB_TYPE
== "pgsql") {
3078 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3079 } else if (DB_TYPE
== "mysql") {
3080 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3083 $result = db_query($link, "SELECT ttrss_entries.title,
3084 ttrss_feeds.title AS feed_title,
3087 SUBSTRING(last_updated,1,19) AS last_updated
3089 ttrss_user_entries,ttrss_entries,ttrss_feeds
3091 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3092 AND include_in_digest = true
3094 AND ttrss_user_entries.owner_uid = $user_id
3095 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
3098 $cur_feed_title = "";
3100 $headlines_count = db_num_rows($result);
3102 while ($line = db_fetch_assoc($result)) {
3103 $updated = smart_date_time(strtotime($line["last_updated"]));
3104 $feed_title = $line["feed_title"];
3106 if ($cur_feed_title != $feed_title) {
3107 $cur_feed_title = $feed_title;
3109 $tmp .= "$feed_title\n\n";
3112 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
3113 $tmp .= " " . trim($line["link"]) . "\n";
3118 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
3119 DIGEST_HOSTNAME
. "\n".
3120 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
3123 return array($tmp, $headlines_count);
3126 function check_for_update($link, $brief_fmt = true) {
3127 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3129 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
3134 if (ENABLE_SIMPLEPIE
) {
3135 $rss = new SimplePie();
3136 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
3137 $rss->set_timeout(MAGPIE_FETCH_TIME_OUT
);
3138 $rss->set_feed_url($fetch_url);
3139 $rss->set_output_encoding('UTF-8');
3142 $rss = fetch_rss($releases_feed);
3144 error_reporting (DEFAULT_ERROR_LEVEL
);
3148 if (ENABLE_SIMPLEPIE
) {
3149 $items = $rss->get_items();
3151 $items = $rss->items
;
3153 if (!$items ||
!is_array($items)) $items = $rss->entries
;
3154 if (!$items ||
!is_array($items)) $items = $rss;
3157 if (!is_array($items) ||
count($items) == 0) {
3161 $latest_item = $items[0];
3163 if (ENABLE_SIMPLEPIE
) {
3164 $last_title = $latest_item->get_title();
3166 $last_title = $latest_item["title"];
3169 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3171 if (ENABLE_SIMPLEPIE
) {
3172 $release_url = sanitize_rss($latest_item->get_link());
3173 $content = sanitize_rss($latest_item->get_description());
3175 $release_url = sanitize_rss($link, $latest_item["link"]);
3176 $content = sanitize_rss($link, $latest_item["description"]);
3179 if (version_compare(VERSION
, $latest_version) == -1) {
3181 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3182 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3183 <div id=\"milestoneDetails\">$content</div>");
3185 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3186 <div class='milestoneDetails'>$content</div>
3187 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3188 download and update information.";
3195 function markArticlesById($link, $ids, $cmode) {
3199 foreach ($ids as $id) {
3200 array_push($tmp_ids, "ref_id = '$id'");
3203 $ids_qpart = join(" OR ", $tmp_ids);
3206 db_query($link, "UPDATE ttrss_user_entries SET
3207 marked = false,last_read = NOW()
3208 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3209 } else if ($cmode == 1) {
3210 db_query($link, "UPDATE ttrss_user_entries SET
3212 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3214 db_query($link, "UPDATE ttrss_user_entries SET
3215 marked = NOT marked,last_read = NOW()
3216 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3220 function publishArticlesById($link, $ids, $cmode) {
3224 foreach ($ids as $id) {
3225 array_push($tmp_ids, "ref_id = '$id'");
3228 $ids_qpart = join(" OR ", $tmp_ids);
3231 db_query($link, "UPDATE ttrss_user_entries SET
3232 published = false,last_read = NOW()
3233 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3234 } else if ($cmode == 1) {
3235 db_query($link, "UPDATE ttrss_user_entries SET
3237 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3239 db_query($link, "UPDATE ttrss_user_entries SET
3240 published = NOT published,last_read = NOW()
3241 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3245 function catchupArticlesById($link, $ids, $cmode) {
3249 foreach ($ids as $id) {
3250 array_push($tmp_ids, "ref_id = '$id'");
3253 $ids_qpart = join(" OR ", $tmp_ids);
3256 db_query($link, "UPDATE ttrss_user_entries SET
3257 unread = false,last_read = NOW()
3258 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3259 } else if ($cmode == 1) {
3260 db_query($link, "UPDATE ttrss_user_entries SET
3262 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3264 db_query($link, "UPDATE ttrss_user_entries SET
3265 unread = NOT unread,last_read = NOW()
3266 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3270 function catchupArticleById($link, $id, $cmode) {
3273 db_query($link, "UPDATE ttrss_user_entries SET
3274 unread = false,last_read = NOW()
3275 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3276 } else if ($cmode == 1) {
3277 db_query($link, "UPDATE ttrss_user_entries SET
3279 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3281 db_query($link, "UPDATE ttrss_user_entries SET
3282 unread = NOT unread,last_read = NOW()
3283 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3287 function make_guid_from_title($title) {
3288 return preg_replace("/[ \"\',.:;]/", "-",
3289 mb_strtolower(strip_tags($title), 'utf-8'));
3292 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3293 $bottom = false, $rtl_content = false, $feed_id = 0,
3294 $is_cat = false, $search = false, $match_on = false,
3295 $search_mode = false, $offset = 0, $limit = 0) {
3297 $user_page_offset = $offset +
1;
3300 $class = "headlinesSubToolbar";
3301 $tid = "headlineActionsTop";
3303 $class = "headlinesSubToolbar";
3304 $tid = "headlineActionsBottom";
3307 print "<table class=\"$class\" id=\"$tid\"
3308 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3316 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3317 $page_next_link = "javascript:viewFeedGoPage(1)";
3318 $page_first_link = "javascript:viewFeedGoPage(0)";
3320 $catchup_page_link = "javascript:catchupPage()";
3321 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3322 $catchup_sel_link = "javascript:catchupSelection()";
3324 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3326 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3327 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3328 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3330 $tog_unread_link = "javascript:selectionToggleUnread()";
3331 $tog_marked_link = "javascript:selectionToggleMarked()";
3332 $tog_published_link = "javascript:selectionTogglePublished()";
3336 $sel_all_link = "javascript:cdmSelectArticles('all')";
3337 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3338 $sel_none_link = "javascript:cdmSelectArticles('none')";
3340 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3341 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3342 $tog_published_link = "javascript:selectionTogglePublished(true)";
3346 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3348 print "<td class=\"headlineActions$rtl_cpart\">
3349 <ul class=\"headlineDropdownMenu\">
3352 <a href=\"$sel_all_link\">".__('All')."</a>,
3353 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3354 <a href=\"$sel_none_link\">".__('None')."</a></li>
3355 <li class=\"vsep\"> </li>
3356 <li class=\"top\">".__('Toggle')."<ul>
3357 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3358 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3359 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3361 <li class=\"vsep\"> </li>
3362 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
3363 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
3364 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3365 <li><span class=\"insensitive\">--------</span></li>
3366 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3367 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3368 <li><span class=\"insensitive\">--------</span></li>
3369 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3372 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3374 if ($limit != 0 && !$search && $enable_pagination) {
3376 <li class=\"vsep\"> </li>
3377 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3378 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3379 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3383 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3385 <li class=\"vsep\"> </li>
3387 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3388 '$match_on', '$feed_id', '$is_cat');\">
3389 ".__('Convert to label')."</a></td>";
3395 // old style subtoolbar:
3397 print "<td class=\"headlineActions$rtl_cpart\">".
3399 <a href=\"$sel_all_link\">".__('All')."</a>,
3400 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3401 <a href=\"$sel_none_link\">".__('None')."</a>
3403 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3404 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3406 __('Mark as read:')."
3407 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3408 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3410 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3413 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3414 '$match_on', '$feed_id', '$is_cat');\">
3415 ".__('Convert to label')."</a>";
3422 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3423 print "<td class=\"headlineActions$rtl_cpart\">
3424 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3425 '$match_on', '$feed_id', '$is_cat');\">
3426 ".__('Convert to Label')."</a></td>";
3429 print "<td class=\"headlineTitle$rtl_cpart\">";
3431 if ($feed_site_url) {
3433 $target = "target=\"_new\"";
3435 print "<a $target href=\"$feed_site_url\">".
3436 truncate_string($feed_title,30)."</a>";
3442 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3445 if ($user_page_offset > 1) {
3446 print " [$user_page_offset] ";
3452 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3453 <img class=\"noborder\"
3454 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3459 print "</tr></table>";
3463 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3465 $tmp_category = getCategoryTitle($link, $cat_id);
3466 $cat_unread = getCategoryUnread($link, $cat_id);
3469 $holder_style = "display:none;";
3476 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3478 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3479 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3482 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3484 print "<span id=\"FCAP-$cat_id\">";
3487 print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3488 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3498 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3501 function outputFeedList($link, $tags = false) {
3503 print "<ul class=\"feedList\" id=\"feedList\">";
3505 $owner_uid = $_SESSION["uid"];
3509 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3511 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
3514 $cat_hidden = false;
3517 # print "<li class=\"feedCat\">".__('Special')."</li>";
3518 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3519 # print "<li class=\"feedCat\">".
3520 # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3521 # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3523 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3524 # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3526 # $cat_unread = getCategoryUnread($link, -1);
3527 # $tmp_category = __("Special");
3528 # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3530 printCategoryHeader($link, -1, $cat_hidden, false);
3533 $num_starred = getFeedUnread($link, -1);
3534 $num_published = getFeedUnread($link, -2);
3535 $num_fresh = getFeedUnread($link, -3);
3539 if ($num_fresh > 0) $class .= "Unread";
3541 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3542 "images/fresh.png", $link);
3546 if ($num_starred > 0) $class .= "Unread";
3548 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
3551 $mark_img_ext = "gif";
3553 $mark_img_ext = "png";
3556 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3557 "images/mark_set.$mark_img_ext", $link);
3561 if ($num_published > 0) $class .= "Unread";
3563 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3564 "images/pub_set.gif", $link);
3566 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3572 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3574 $result = db_query($link, "SELECT id,sql_exp,description FROM
3575 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3577 if (db_num_rows($result) > 0) {
3578 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3580 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
3583 $cat_hidden = false;
3586 printCategoryHeader($link, -2, $cat_hidden, false);
3588 # print "<li class=\"feedCat\">".
3589 # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3590 # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3592 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
3594 print "<li><hr></li>";
3598 while ($line = db_fetch_assoc($result)) {
3600 error_reporting (0);
3602 $label_id = -$line['id'] - 11;
3603 $count = getFeedUnread($link, $label_id);
3611 error_reporting (DEFAULT_ERROR_LEVEL
);
3613 printFeedEntry($label_id,
3614 $class, $line["description"],
3615 $count, "images/label.png", $link);
3619 if (db_num_rows($result) > 0) {
3620 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3627 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3628 print "<li><hr></li>";
3631 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3632 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3633 $order_by_qpart = "category,unread DESC,title";
3635 $order_by_qpart = "category,title";
3638 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3639 $order_by_qpart = "unread DESC,title";
3641 $order_by_qpart = "title";
3645 $result = db_query($link, "SELECT ttrss_feeds.*,
3646 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3647 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3648 WHERE feed_id = ttrss_feeds.id AND unread = true
3649 AND ttrss_user_entries.ref_id = ttrss_entries.id
3650 AND owner_uid = '$owner_uid') as unread,
3652 ttrss_feed_categories.title AS category,
3653 ttrss_feed_categories.collapsed
3654 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3655 ON (ttrss_feed_categories.id = cat_id)
3657 ttrss_feeds.hidden = false AND
3658 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3659 ORDER BY $order_by_qpart");
3661 $actid = $_GET["actid"];
3671 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3673 while ($line = db_fetch_assoc($result)) {
3675 $feed = trim($line["title"]);
3677 if (!$feed) $feed = "[Untitled]";
3679 $feed_id = $line["id"];
3681 $subop = $_GET["subop"];
3683 $unread = $line["unread"];
3685 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3686 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3688 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3691 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3694 $rtl_tag = "dir=\"RTL\"";
3699 $tmp_result = db_query($link,
3700 "SELECT id,COUNT(unread) AS unread
3701 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3702 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3703 WHERE parent_feed = '$feed_id' AND unread = true
3704 GROUP BY ttrss_feeds.id");
3706 if (db_num_rows($tmp_result) > 0) {
3707 while ($l = db_fetch_assoc($tmp_result)) {
3708 $unread +
= $l["unread"];
3712 $cat_id = $line["cat_id"];
3714 $tmp_category = $line["category"];
3716 if (!$tmp_category) {
3717 $tmp_category = __("Uncategorized");
3720 // $class = ($lnum % 2) ? "even" : "odd";
3722 if ($line["last_error"]) {
3728 if ($unread > 0) $class .= "Unread";
3730 if ($actid == $feed_id) {
3731 $class .= "Selected";
3734 $total_unread +
= $unread;
3736 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3742 $category = $tmp_category;
3744 $collapsed = $line["collapsed"];
3746 // workaround for NULL category
3747 if ($category == __("Uncategorized")) {
3748 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3753 if ($collapsed == "t" ||
$collapsed == "1") {
3754 $holder_class = "feedCatHolder";
3755 $holder_style = "display:none;";
3758 $holder_class = "feedCatHolder";
3763 $cat_id = sprintf("%d", $cat_id);
3765 $cat_unread = getCategoryUnread($link, $cat_id);
3767 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3769 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3770 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3771 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3772 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3773 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3776 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3779 printFeedEntry($feed_id, $class, $feed, $unread,
3780 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
3781 $last_updated, $line["last_error"]);
3786 if (db_num_rows($result) == 0) {
3787 print "<li>".__('No feeds to display.')."</li>";
3794 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3795 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3796 post_int_id = ttrss_user_entries.int_id AND
3797 unread = true AND ref_id = ttrss_entries.id
3798 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3800 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3801 ORDER BY tag_name"); */
3803 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3804 print "<li class=\"feedCat\">".__('Tags')."</li>";
3805 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3808 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3809 FROM ttrss_user_entries WHERE int_id = post_int_id
3810 AND unread = true)) AS count FROM ttrss_tags
3811 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
3812 ORDER BY count DESC LIMIT 50");
3816 while ($line = db_fetch_assoc($result)) {
3817 $tags[$line["tag_name"]] +
= $line["count"];
3820 foreach (array_keys($tags) as $tag) {
3822 $unread = $tags[$tag];
3830 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3834 if (db_num_rows($result) == 0) {
3835 print "<li>No tags to display.</li>";
3838 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3848 function get_article_tags($link, $id, $owner_uid = 0) {
3850 $a_id = db_escape_string($id);
3852 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3854 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3855 owner_uid as owner FROM
3856 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3857 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
3861 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3862 array_push($tags, $tmp_line["tag_name"]);
3868 function trim_value(&$value) {
3869 $value = trim($value);
3872 function trim_array($array) {
3874 array_walk($tmp, 'trim_value');
3878 function tag_is_valid($tag) {
3879 if ($tag == '') return false;
3880 if (preg_match("/^[0-9]*$/", $tag)) return false;
3882 $tag = iconv("utf-8", "utf-8", $tag);
3883 if (!$tag) return false;
3888 function render_login_form($link, $mobile = false) {
3890 require_once "login_form.php";
3892 require_once "mobile/login_form.php";
3896 // from http://developer.apple.com/internet/safari/faq.html
3897 function no_cache_incantation() {
3898 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3899 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3900 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3901 header("Cache-Control: post-check=0, pre-check=0", false);
3902 header("Pragma: no-cache"); // HTTP/1.0
3905 function format_warning($msg, $id = "") {
3906 return "<div class=\"warning\" id=\"$id\">
3907 <img src=\"images/sign_excl.gif\">$msg</div>";
3910 function format_notice($msg) {
3911 return "<div class=\"notice\">
3912 <img src=\"images/sign_info.gif\">$msg</div>";
3915 function format_error($msg) {
3916 return "<div class=\"error\">
3917 <img src=\"images/sign_excl.gif\">$msg</div>";
3920 function print_notice($msg) {
3921 return print format_notice($msg);
3924 function print_warning($msg) {
3925 return print format_warning($msg);
3928 function print_error($msg) {
3929 return print format_error($msg);
3933 function T_sprintf() {
3934 $args = func_get_args();
3935 return vsprintf(__(array_shift($args)), $args);
3938 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3940 /* we can figure out feed_id from article id anyway, why do we
3941 * pass feed_id here? */
3943 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3944 WHERE ref_id = '$id'");
3946 $feed_id = db_fetch_result($result, 0, "feed_id");
3948 print "<article id='$id'><![CDATA[";
3950 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3951 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3953 if (db_num_rows($result) == 1) {
3954 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3956 $rtl_content = false;
3960 $rtl_tag = "dir=\"RTL\"";
3967 if ($mark_as_read) {
3968 $result = db_query($link, "UPDATE ttrss_user_entries
3969 SET unread = false,last_read = NOW()
3970 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3973 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3974 SUBSTRING(updated,1,16) as updated,
3975 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3978 FROM ttrss_entries,ttrss_user_entries
3979 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3985 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3986 $link_target = "target=\"_new\"";
3989 $line = db_fetch_assoc($result);
3991 if ($line["icon_url"]) {
3992 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3994 $feed_icon = " ";
3997 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
3998 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4000 $entry_comments = "";
4003 $num_comments = $line["num_comments"];
4004 $entry_comments = "";
4006 if ($num_comments > 0) {
4007 if ($line["comments"]) {
4008 $comments_url = $line["comments"];
4010 $comments_url = $line["link"];
4012 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4014 if ($line["comments"] && $line["link"] != $line["comments"]) {
4015 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4019 print "<div class=\"postReply\">";
4021 print "<div class=\"postHeader\">";
4023 $entry_author = $line["author"];
4025 if ($entry_author) {
4026 $entry_author = __(" - by ") . $entry_author;
4029 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4030 strtotime($line["updated"]));
4032 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4034 if ($line["link"]) {
4035 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
4036 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
4038 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4041 /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
4042 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
4043 ORDER BY tag_name"); */
4045 $tags = get_article_tags($link, $id);
4052 foreach ($tags as $tag) {
4054 $tag_escaped = str_replace("'", "\\'", $tag);
4056 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4058 if ($num_tags == 6) {
4061 } else if ($num_tags < 6) {
4062 $tags_str .= $tag_str;
4064 $f_tags_str .= $tag_str;
4067 $tags_str = preg_replace("/, $/", "", $tags_str);
4068 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4070 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4071 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4073 if (!$entry_comments) $entry_comments = " "; # placeholder
4075 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4077 print "<div style='float : right'>
4078 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4080 <a title=\"Edit tags for this article\"
4081 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4082 <div clear='both'>$entry_comments</div>";
4086 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4087 print "<div class=\"postContent\">";
4089 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4091 $line["content"] = sanitize_rss($link, $line["content"]);
4093 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4094 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4097 print $line["content"] . "</div>";
4103 print "]]></article>";
4107 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4108 $next_unread_feed, $offset) {
4110 $timing_info = getmicrotime();
4112 $topmost_article_ids = array();
4118 if ($subop == "undefined") $subop = "";
4120 if ($subop == "CatchupSelected") {
4121 $ids = split(",", db_escape_string($_GET["ids"]));
4122 $cmode = sprintf("%d", $_GET["cmode"]);
4124 catchupArticlesById($link, $ids, $cmode);
4127 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4128 update_generic_feed($link, $feed, $cat_view);
4131 if ($subop == "MarkAllRead") {
4132 catchup_feed($link, $feed, $cat_view);
4134 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4135 if ($next_unread_feed) {
4136 $feed = $next_unread_feed;
4142 $result = db_query($link,
4143 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4145 if (db_num_rows($result) == 0) {
4146 print "<div align='center'>".__('Feed not found.')."</div>";
4151 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4153 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4154 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4156 if (db_num_rows($result) == 1) {
4157 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4159 $rtl_content = false;
4163 $rtl_tag = "dir=\"RTL\"";
4169 $rtl_content = false;
4172 $script_dt_add = get_script_dt_add();
4174 /// START /////////////////////////////////////////////////////////////////////////////////
4176 $search = db_escape_string($_GET["query"]);
4177 $search_mode = db_escape_string($_GET["search_mode"]);
4178 $match_on = db_escape_string($_GET["match_on"]);
4184 $real_offset = $offset * $limit;
4186 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4188 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4189 $search, $search_mode, $match_on, false, $real_offset);
4191 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4193 $result = $qfh_ret[0];
4194 $feed_title = $qfh_ret[1];
4195 $feed_site_url = $qfh_ret[2];
4196 $last_error = $qfh_ret[3];
4199 $feed_site_url = article_publish_url($link);
4202 /// STOP //////////////////////////////////////////////////////////////////////////////////
4205 print "<div id=\"headlinesContainer\" $rtl_tag>";
4208 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4212 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4213 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4216 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4219 if (db_num_rows($result) > 0) {
4221 # print "\{$offset}";
4223 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4224 print "<table class=\"headlinesList\" id=\"headlinesList\"
4225 cellspacing=\"0\">";
4230 error_reporting (DEFAULT_ERROR_LEVEL
);
4234 while ($line = db_fetch_assoc($result)) {
4236 $class = ($lnum %
2) ?
"even" : "odd";
4239 $feed_id = $line["feed_id"];
4241 if (count($topmost_article_ids) < 5) {
4242 array_push($topmost_article_ids, $id);
4245 if ($line["last_read"] == "" &&
4246 ($line["unread"] != "t" && $line["unread"] != "1")) {
4248 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4251 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4255 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
4263 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4266 $mark_img_ext = "gif";
4268 $mark_img_ext = "png";
4271 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
4272 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
4274 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
4276 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
4278 alt=\"Star article\" onclick='javascript:tMark($id)'>";
4281 if ($line["published"] == "t" ||
$line["published"] == "1") {
4282 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
4284 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
4286 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
4288 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
4291 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4292 # $line["title"] . "</a>";
4294 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4295 $line["title"] . "</a>";
4297 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4298 # $line["title"] . "</a>";
4300 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4301 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
4303 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4304 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
4307 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4308 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4312 $entry_author = $line["author"];
4314 if ($entry_author) {
4315 $entry_author = " - by $entry_author";
4318 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4320 print "<tr class='$class' id='RROW-$id'>";
4322 print "<td class='hlUpdPic'>$update_pic</td>";
4324 print "<td class='hlSelectRow'>
4325 <input type=\"checkbox\" onclick=\"tSR(this)\"
4329 print "<td class='hlMarkedPic'>$marked_pic</td>";
4330 print "<td class='hlMarkedPic'>$published_pic</td>";
4332 if ($line["feed_title"]) {
4333 print "<td class='hlContent'>$content_link</td>";
4334 print "<td class='hlFeed'>
4335 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4336 truncate_string($line["feed_title"],30)."</a> </td>";
4338 print "<td class='hlContent' valign='middle'>";
4340 print "<a href=\"javascript:view($id,$feed_id);\">" .
4343 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4344 if ($content_preview) {
4345 print "<span class=\"contentPreview\"> - $content_preview</span>";
4353 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
4360 $add_class = "Unread";
4365 print "<div class=\"cdmArticle$add_class\"
4366 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4367 onmouseout='cdmMouseOut(this)'>";
4369 print "<div class=\"cdmHeader\">";
4371 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4373 print "<a class=\"title\"
4374 onclick=\"javascript:toggleUnread($id, 0)\"
4375 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4377 print $entry_author;
4379 if ($line["feed_title"]) {
4380 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4385 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4386 $line["content_preview"] = preg_replace("/href=/i",
4387 "target=\"_new\" href=", $line["content_preview"]);
4390 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4392 print "<div class=\"cdmFooter\"><span class='s0'>";
4394 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4396 print __("Select:").
4397 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4398 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4400 print "</span><span class='s1'>$marked_pic</span> ";
4401 print "<span class='s1'>$published_pic</span> ";
4403 $tags = get_article_tags($link, $id);
4406 $full_tags_str = "";
4409 foreach ($tags as $tag) {
4411 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4412 if ($num_tags < 5) {
4413 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4414 } else if ($num_tags == 5) {
4419 $tags_str = preg_replace("/, $/", "", $tags_str);
4420 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4422 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4424 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4427 if ($tags_str == "") $tags_str = "no tags";
4429 // print "<img src='images/tag.png' class='markedPic'>";
4431 print "<span class='s1'>
4432 <img class='tagsPic' src='images/tag.png' alt='Tags'
4433 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
4434 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4438 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4439 href=\"javascript:toggleUnread($id)\">
4450 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4454 // print_headline_subtoolbar($link,
4455 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4459 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
4467 return $topmost_article_ids;
4470 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
4472 function printTagCloud($link) {
4474 /* get first ref_id to count from */
4480 if (DB_TYPE == "pgsql") {
4481 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4482 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4483 AND date_entered > NOW() - INTERVAL '30 days'";
4485 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4486 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4487 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4490 $result = db_query($link, $query);
4491 $first_id = db_fetch_result($result, 0, "id"); */
4493 //AND post_int_id >= '$first_id'
4494 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4495 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
4496 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
4498 $result = db_query($link, $query);
4502 while ($line = db_fetch_assoc($result)) {
4503 $tags[$line["tag_name"]] = $line["count"];
4508 $max_size = 32; // max font size in pixels
4509 $min_size = 11; // min font size in pixels
4511 // largest and smallest array values
4512 $max_qty = max(array_values($tags));
4513 $min_qty = min(array_values($tags));
4515 // find the range of values
4516 $spread = $max_qty - $min_qty;
4517 if ($spread == 0) { // we don't want to divide by zero
4521 // set the font-size increment
4522 $step = ($max_size - $min_size) / ($spread);
4524 // loop through the tag array
4525 foreach ($tags as $key => $value) {
4526 // calculate font-size
4527 // find the $value in excess of $min_qty
4528 // multiply by the font-size increment ($size)
4529 // and add the $min_size set above
4530 $size = round($min_size +
(($value - $min_qty) * $step));
4532 $key_escaped = str_replace("'", "\\'", $key);
4534 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
4535 $size . "px\" title=\"$value articles tagged with " .
4536 $key . '">' . $key . '</a> ';
4540 function print_checkpoint($n, $s) {
4541 $ts = getmicrotime();
4542 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4546 function sanitize_tag($tag) {
4549 $tag = mb_strtolower($tag, 'utf-8');
4551 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4553 // $tag = str_replace('"', "", $tag);
4554 // $tag = str_replace("+", " ", $tag);
4555 $tag = str_replace("technorati tag: ", "", $tag);
4560 function generate_publish_key() {
4561 return sha1(uniqid(rand(), true));
4564 function article_publish_url($link) {
4566 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
4568 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");