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...");
437 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
441 if (!ENABLE_SIMPLEPIE
) {
442 $rss = fetch_rss($fetch_url);
444 if (!is_dir(SIMPLEPIE_CACHE_DIR
)) {
445 mkdir(SIMPLEPIE_CACHE_DIR
);
448 $rss = new SimplePie($fetch_url, SIMPLEPIE_CACHE_DIR
);
450 $rss->handle_content_type();
455 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
456 _debug("update_rss_feed: fetch done, parsing...");
458 error_reporting (DEFAULT_ERROR_LEVEL
);
461 $feed = db_escape_string($feed);
465 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
466 _debug("update_rss_feed: processing feed data...");
469 // db_query($link, "BEGIN");
471 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
472 FROM ttrss_feeds WHERE id = '$feed'");
474 $registered_title = db_fetch_result($result, 0, "title");
475 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
476 $orig_site_url = db_fetch_result($result, 0, "site_url");
478 $owner_uid = db_fetch_result($result, 0, "owner_uid");
480 if (ENABLE_SIMPLEPIE
) {
481 $site_url = $rss->get_link();
483 $site_url = $rss->channel
["link"];
486 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
487 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
488 _debug("update_rss_feed: checking favicon...");
491 check_feed_favicon($site_url, $feed, $link);
494 if (!$registered_title ||
$registered_title == "[Unknown]") {
496 $feed_title = db_escape_string($rss->channel
["title"]);
498 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
499 _debug("update_rss_feed: registering title: $feed_title");
502 db_query($link, "UPDATE ttrss_feeds SET
503 title = '$feed_title' WHERE id = '$feed'");
506 // weird, weird Magpie
507 if (!ENABLE_SIMPLEPIE
) {
508 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
511 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
512 db_query($link, "UPDATE ttrss_feeds SET
513 site_url = '$site_url' WHERE id = '$feed'");
516 // print "I: " . $rss->channel["image"]["url"];
518 if (!ENABLE_SIMPLEPIE
) {
519 $icon_url = $rss->image
["url"];
521 $icon_url = $rss->get_image_url();
524 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
525 $icon_url = db_escape_string($icon_url);
526 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
529 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
530 _debug("update_rss_feed: loading filters...");
535 $result = db_query($link, "SELECT reg_exp,
536 ttrss_filter_types.name AS name,
537 ttrss_filter_actions.name AS action,
540 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
542 owner_uid = $owner_uid AND
543 ttrss_filter_types.id = filter_type AND
544 ttrss_filter_actions.id = action_id AND
545 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
547 while ($line = db_fetch_assoc($result)) {
548 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
550 $filter["reg_exp"] = $line["reg_exp"];
551 $filter["action"] = $line["action"];
552 $filter["action_param"] = $line["action_param"];
553 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
555 array_push($filters[$line["name"]], $filter);
558 if (ENABLE_SIMPLEPIE
) {
559 $iterator = $rss->get_items();
561 $iterator = $rss->items
;
562 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
563 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
566 if (!is_array($iterator)) {
567 /* db_query($link, "UPDATE ttrss_feeds
568 SET last_error = 'Parse error: can\'t find any articles.'
569 WHERE id = '$feed'"); */
571 // clear any errors and mark feed as updated if fetched okay
572 // even if it's blank
574 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
575 _debug("update_rss_feed: entry iterator is not an array, no articles?");
578 db_query($link, "UPDATE ttrss_feeds
579 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
581 return; // no articles
584 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
585 _debug("update_rss_feed: processing articles...");
588 foreach ($iterator as $item) {
590 if (ENABLE_SIMPLEPIE
) {
591 $entry_guid = $item->get_id();
592 if (!$entry_guid) $entry_guid = $item->get_link();
593 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
597 $entry_guid = $item["id"];
599 if (!$entry_guid) $entry_guid = $item["guid"];
600 if (!$entry_guid) $entry_guid = $item["link"];
601 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
604 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
605 _debug("update_rss_feed: guid $entry_guid");
608 if (!$entry_guid) continue;
610 $entry_timestamp = "";
612 if (ENABLE_SIMPLEPIE
) {
613 $entry_timestamp = strtotime($item->get_date());
615 $rss_2_date = $item['pubdate'];
616 $rss_1_date = $item['dc']['date'];
617 $atom_date = $item['issued'];
618 if (!$atom_date) $atom_date = $item['updated'];
620 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
621 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
622 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
625 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
626 $entry_timestamp = time();
627 $no_orig_date = 'true';
629 $no_orig_date = 'false';
632 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
634 if (ENABLE_SIMPLEPIE
) {
635 $entry_title = $item->get_title();
637 $entry_title = trim(strip_tags($item["title"]));
640 if (ENABLE_SIMPLEPIE
) {
641 $entry_link = $item->get_link();
643 // strange Magpie workaround
644 $entry_link = $item["link_"];
645 if (!$entry_link) $entry_link = $item["link"];
648 if (!$entry_title) continue;
650 $entry_link = strip_tags($entry_link);
652 if (ENABLE_SIMPLEPIE
) {
653 $entry_content = $item->get_description();
655 $entry_content = $item["content:escaped"];
657 if (!$entry_content) $entry_content = $item["content:encoded"];
658 if (!$entry_content) $entry_content = $item["content"];
659 if (!$entry_content) $entry_content = $item["atom_content"];
660 if (!$entry_content) $entry_content = $item["summary"];
661 if (!$entry_content) $entry_content = $item["description"];
664 if (is_array($entry_content)) {
665 $entry_content = $entry_content["encoded"];
666 if (!$entry_content) $entry_content = $entry_content["escaped"];
671 // print_r(htmlspecialchars($entry_content));
674 $entry_content_unescaped = $entry_content;
676 if (ENABLE_SIMPLEPIE
) {
677 $entry_comments = strip_tags($item->data
["comments"]);
678 $entry_author = $item->get_author();
680 $entry_comments = strip_tags($item["comments"]);
682 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
684 if ($item['author']) {
686 if (is_array($item['author'])) {
688 if (!$entry_author) {
689 $entry_author = db_escape_string(strip_tags($item['author']['name']));
692 if (!$entry_author) {
693 $entry_author = db_escape_string(strip_tags($item['author']['email']));
697 if (!$entry_author) {
698 $entry_author = db_escape_string(strip_tags($item['author']));
703 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
705 $entry_guid = db_escape_string(strip_tags($entry_guid));
707 $result = db_query($link, "SELECT id FROM ttrss_entries
708 WHERE guid = '$entry_guid'");
710 $entry_content = db_escape_string($entry_content);
712 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
714 $entry_title = db_escape_string($entry_title);
715 $entry_link = db_escape_string($entry_link);
716 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
717 $entry_author = mb_substr($entry_author, 0, 250);
719 if (ENABLE_SIMPLEPIE
) {
720 $num_comments = 0; #FIXME#
722 $num_comments = db_escape_string($item["slash"]["comments"]);
725 if (!$num_comments) $num_comments = 0;
727 // parse <category> entries into tags
729 if (ENABLE_SIMPLEPIE
) {
731 $additional_tags = array();
732 $additional_tags_src = $item->get_categories();
734 foreach ($additional_tags_src as $tobj) {
735 array_push($additional_tags, $tobj->get_term());
740 $t_ctr = $item['category#'];
742 $additional_tags = false;
745 $additional_tags = false;
746 } else if ($t_ctr == 1) {
747 $additional_tags = array($item['category']);
749 $additional_tags = array();
750 for ($i = 0; $i <= $t_ctr; $i++
) {
751 if ($item["category#$i"]) {
752 array_push($additional_tags, $item["category#$i"]);
757 // parse <dc:subject> elements
759 $t_ctr = $item['dc']['subject#'];
762 $additional_tags = array($item['dc']['subject']);
763 } else if ($t_ctr > 1) {
764 $additional_tags = array();
765 for ($i = 0; $i <= $t_ctr; $i++
) {
766 if ($item['dc']["subject#$i"]) {
767 array_push($additional_tags, $item['dc']["subject#$i"]);
775 // $entry_content = sanitize_rss($entry_content);
777 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
778 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
781 db_query($link, "BEGIN");
783 if (db_num_rows($result) == 0) {
785 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
786 _debug("update_rss_feed: base guid not found");
789 // base post entry does not exist, create it
791 $result = db_query($link,
792 "INSERT INTO ttrss_entries
808 '$entry_timestamp_fmt',
817 // we keep encountering the entry in feeds, so we need to
818 // update date_entered column so that we don't get horrible
819 // dupes when the entry gets purged and reinserted again e.g.
820 // in the case of SLOW SLOW OMG SLOW updating feeds
822 $base_entry_id = db_fetch_result($result, 0, "id");
824 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
825 WHERE id = '$base_entry_id'");
828 // now it should exist, if not - bad luck then
830 $result = db_query($link, "SELECT
831 id,content_hash,no_orig_date,title,
832 substring(date_entered,1,19) as date_entered,
833 substring(updated,1,19) as updated,
837 WHERE guid = '$entry_guid'");
839 if (db_num_rows($result) == 1) {
841 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
842 _debug("update_rss_feed: base guid found, checking for user record");
845 // this will be used below in update handler
846 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
847 $orig_title = db_fetch_result($result, 0, "title");
848 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
849 $orig_date_entered = strtotime(db_fetch_result($result,
852 $ref_id = db_fetch_result($result, 0, "id");
854 // check for user post link to main table
856 // do we allow duplicate posts with same GUID in different feeds?
857 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
858 $dupcheck_qpart = "AND feed_id = '$feed'";
860 $dupcheck_qpart = "";
863 // error_reporting(0);
865 $article_filters = get_article_filters($filters, $entry_title,
866 $entry_content, $entry_link);
868 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
869 _debug("update_rss_feed: article filters: ");
870 if (count($article_filters) != 0) {
871 print_r($article_filters);
875 if (find_article_filter($article_filters, "filter")) {
879 // error_reporting (DEFAULT_ERROR_LEVEL);
881 $result = db_query($link,
882 "SELECT ref_id FROM ttrss_user_entries WHERE
883 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
886 // okay it doesn't exist - create user entry
887 if (db_num_rows($result) == 0) {
889 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
890 _debug("update_rss_feed: user record not found, creating...");
893 if (!find_article_filter($article_filters, 'catchup')) {
895 $last_read_qpart = 'NULL';
898 $last_read_qpart = 'NOW()';
901 if (find_article_filter($article_filters, 'mark')) {
907 if (find_article_filter($article_filters, 'publish')) {
910 $published = 'false';
913 $result = db_query($link,
914 "INSERT INTO ttrss_user_entries
915 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
916 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
917 $last_read_qpart, $marked, $published)");
920 $post_needs_update = false;
922 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
923 ($content_hash != $orig_content_hash)) {
924 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
925 $post_needs_update = true;
928 if (db_escape_string($orig_title) != $entry_title) {
929 $post_needs_update = true;
932 if ($orig_num_comments != $num_comments) {
933 $post_needs_update = true;
936 // this doesn't seem to be very reliable
938 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
939 // $post_needs_update = true;
942 // if post needs update, update it and mark all user entries
943 // linking to this post as updated
944 if ($post_needs_update) {
946 if (defined('DAEMON_EXTENDED_DEBUG')) {
947 _debug("update_rss_feed: post $entry_guid needs update...");
950 // print "<!-- post $orig_title needs update : $post_needs_update -->";
952 db_query($link, "UPDATE ttrss_entries
953 SET title = '$entry_title', content = '$entry_content',
954 content_hash = '$content_hash',
955 num_comments = '$num_comments'
956 WHERE id = '$ref_id'");
958 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
959 db_query($link, "UPDATE ttrss_user_entries
960 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
962 db_query($link, "UPDATE ttrss_user_entries
963 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
969 db_query($link, "COMMIT");
971 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
972 _debug("update_rss_feed: looking for tags...");
976 // <a href="..." rel="tag">Xorg</a>, //
980 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
981 $entry_content_unescaped, $entry_tags);
983 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
984 print_r($entry_tags);
985 print "<br/></p>"; */
987 $entry_tags = $entry_tags[1];
989 # check for manual tags
991 $tag_filter = find_article_filter($article_filters, "tag");
995 $manual_tags = trim_array(split(",", $tag_filter[1]));
997 foreach ($manual_tags as $tag) {
998 if (tag_is_valid($tag)) {
999 array_push($entry_tags, $tag);
1004 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1005 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1007 if ($additional_tags && is_array($additional_tags)) {
1008 foreach ($additional_tags as $tag) {
1009 if (tag_is_valid($tag) &&
1010 array_search($tag, $boring_tags) === FALSE) {
1011 array_push($entry_tags, $tag);
1016 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1018 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1019 print_r($entry_tags);
1022 if (count($entry_tags) > 0) {
1024 db_query($link, "BEGIN");
1026 $result = db_query($link, "SELECT id,int_id
1027 FROM ttrss_entries,ttrss_user_entries
1028 WHERE guid = '$entry_guid'
1029 AND feed_id = '$feed' AND ref_id = id
1030 AND owner_uid = '$owner_uid'");
1032 if (db_num_rows($result) == 1) {
1034 $entry_id = db_fetch_result($result, 0, "id");
1035 $entry_int_id = db_fetch_result($result, 0, "int_id");
1037 foreach ($entry_tags as $tag) {
1039 $tag = sanitize_tag($tag);
1040 $tag = db_escape_string($tag);
1042 if (!tag_is_valid($tag)) continue;
1044 $result = db_query($link, "SELECT id FROM ttrss_tags
1045 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1046 owner_uid = '$owner_uid' LIMIT 1");
1048 // print db_fetch_result($result, 0, "id");
1050 if ($result && db_num_rows($result) == 0) {
1052 db_query($link, "INSERT INTO ttrss_tags
1053 (owner_uid,tag_name,post_int_id)
1054 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1058 db_query($link, "COMMIT");
1061 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1062 _debug("update_rss_feed: article processed");
1066 db_query($link, "UPDATE ttrss_feeds
1067 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1069 // db_query($link, "COMMIT");
1072 if (ENABLE_SIMPLEPIE
) {
1073 $error_msg = mb_substr(db_escape_string($rss->error()), 0, 250);
1075 $error_msg = mb_substr(db_escape_string(magpie_error()), 0, 250);
1078 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1079 last_updated = NOW() WHERE id = '$feed'");
1082 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1083 _debug("update_rss_feed: done");
1088 function print_select($id, $default, $values, $attributes = "") {
1089 print "<select name=\"$id\" id=\"$id\" $attributes>";
1090 foreach ($values as $v) {
1096 print "<option$sel>$v</option>";
1101 function print_select_hash($id, $default, $values, $attributes = "") {
1102 print "<select name=\"$id\" id='$id' $attributes>";
1103 foreach (array_keys($values) as $v) {
1109 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1115 function get_article_filters($filters, $title, $content, $link) {
1118 if ($filters["title"]) {
1119 foreach ($filters["title"] as $filter) {
1120 $reg_exp = $filter["reg_exp"];
1121 $inverse = $filter["inverse"];
1122 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1123 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1125 array_push($matches, array($filter["action"], $filter["action_param"]));
1130 if ($filters["content"]) {
1131 foreach ($filters["content"] as $filter) {
1132 $reg_exp = $filter["reg_exp"];
1133 $inverse = $filter["inverse"];
1135 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1136 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1138 array_push($matches, array($filter["action"], $filter["action_param"]));
1143 if ($filters["both"]) {
1144 foreach ($filters["both"] as $filter) {
1145 $reg_exp = $filter["reg_exp"];
1146 $inverse = $filter["inverse"];
1149 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1150 array_push($matches, array($filter["action"], $filter["action_param"]));
1153 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1154 array_push($matches, array($filter["action"], $filter["action_param"]));
1160 if ($filters["link"]) {
1161 $reg_exp = $filter["reg_exp"];
1162 foreach ($filters["link"] as $filter) {
1163 $reg_exp = $filter["reg_exp"];
1164 $inverse = $filter["inverse"];
1166 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1167 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1169 array_push($matches, array($filter["action"], $filter["action_param"]));
1177 function find_article_filter($filters, $filter_name) {
1178 foreach ($filters as $f) {
1179 if ($f[0] == $filter_name) {
1186 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1187 $rtl_content = false, $last_updated = false, $last_error = false) {
1189 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1190 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1192 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1196 $rtl_tag = "dir=\"rtl\"";
1198 $rtl_tag = "dir=\"ltr\"";
1201 $error_notify_msg = "";
1204 $link_title = "Error: $last_error ($last_updated)";
1205 $error_notify_msg = "(Error)";
1206 } else if ($last_updated) {
1207 $link_title = "Updated: $last_updated";
1210 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1211 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1213 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1214 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1218 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1223 $fctr_class = "class=\"invisible\"";
1226 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1227 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1229 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1230 print "<div class=\"feedExtInfo\">
1231 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1238 function getmicrotime() {
1239 list($usec, $sec) = explode(" ",microtime());
1240 return ((float)$usec +
(float)$sec);
1243 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1244 foreach ($values as $v) {
1251 if ($v == $true_is) {
1252 $sel .= " value=\"1\"";
1254 $sel .= " value=\"0\"";
1257 print "<input class=\"noborder\"
1258 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1263 function initialize_user_prefs($link, $uid) {
1265 $uid = db_escape_string($uid);
1267 db_query($link, "BEGIN");
1269 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1271 $u_result = db_query($link, "SELECT pref_name
1272 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1274 $active_prefs = array();
1276 while ($line = db_fetch_assoc($u_result)) {
1277 array_push($active_prefs, $line["pref_name"]);
1280 while ($line = db_fetch_assoc($result)) {
1281 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1282 // print "adding " . $line["pref_name"] . "<br>";
1284 db_query($link, "INSERT INTO ttrss_user_prefs
1285 (owner_uid,pref_name,value) VALUES
1286 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1291 db_query($link, "COMMIT");
1295 function lookup_user_id($link, $user) {
1297 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1300 if (db_num_rows($result) == 1) {
1301 return db_fetch_result($result, 0, "id");
1307 function http_authenticate_user($link) {
1309 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1311 if (!$_SERVER["PHP_AUTH_USER"]) {
1313 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1314 header('HTTP/1.0 401 Unauthorized');
1318 $auth_result = authenticate_user($link,
1319 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1321 if (!$auth_result) {
1322 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1323 header('HTTP/1.0 401 Unauthorized');
1331 function authenticate_user($link, $login, $password, $force_auth = false) {
1333 if (!SINGLE_USER_MODE
) {
1335 $pwd_hash = 'SHA1:' . sha1($password);
1337 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1338 $query = "SELECT id,login,access_level
1339 FROM ttrss_users WHERE
1342 $query = "SELECT id,login,access_level
1343 FROM ttrss_users WHERE
1344 login = '$login' AND pwd_hash = '$pwd_hash'";
1347 $result = db_query($link, $query);
1349 if (db_num_rows($result) == 1) {
1350 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1351 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1352 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1354 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1357 $user_theme = get_user_theme_path($link);
1359 $_SESSION["theme"] = $user_theme;
1360 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1362 initialize_user_prefs($link, $_SESSION["uid"]);
1371 $_SESSION["uid"] = 1;
1372 $_SESSION["name"] = "admin";
1374 $user_theme = get_user_theme_path($link);
1376 $_SESSION["theme"] = $user_theme;
1377 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1379 initialize_user_prefs($link, $_SESSION["uid"]);
1385 function make_password($length = 8) {
1388 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1392 while ($i < $length) {
1393 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1395 if (!strstr($password, $char)) {
1403 // this is called after user is created to initialize default feeds, labels
1406 // user preferences are checked on every login, not here
1408 function initialize_user($link, $uid) {
1410 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1411 values ('$uid','unread = true', 'Unread articles')");
1413 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1414 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1416 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1417 values ('$uid', 'Tiny Tiny RSS: New Releases',
1418 'http://tt-rss.spb.ru/releases.rss')");
1420 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1421 values ('$uid', 'Tiny Tiny RSS: Forum',
1422 'http://tt-rss.spb.ru/forum/rss.php')");
1425 function logout_user() {
1427 if (isset($_COOKIE[session_name()])) {
1428 setcookie(session_name(), '', time()-42000, '/');
1432 function get_script_urlpath() {
1433 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1436 function validate_session($link) {
1437 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1438 if ($_SESSION["ip_address"]) {
1439 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1440 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1446 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1448 //print_r($_SESSION);
1450 if (time() > $_SESSION["cookie_lifetime"]) {
1458 function login_sequence($link, $mobile = false) {
1459 if (!SINGLE_USER_MODE
) {
1461 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1462 $swu = db_escape_string($_REQUEST["swu"]);
1464 $_SESSION["prefs_cache"] = false;
1465 return authenticate_user($link, $swu, null, true);
1469 $login_action = $_POST["login_action"];
1471 # try to authenticate user if called from login form
1472 if ($login_action == "do_login") {
1473 $login = $_POST["login"];
1474 $password = $_POST["password"];
1475 $remember_me = $_POST["remember_me"];
1477 if (authenticate_user($link, $login, $password)) {
1478 $_POST["password"] = "";
1480 $_SESSION["language"] = $_POST["language"];
1482 header("Location: " . $_SERVER["REQUEST_URI"]);
1487 $_SESSION["login_error_msg"] = "Incorrect username or password";
1491 // print session_id();
1492 // print_r($_SESSION);
1494 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1495 render_login_form($link, $mobile);
1498 /* bump login timestamp */
1499 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1502 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME
> 0) {
1503 setcookie("ttrss_lang", $_SESSION["language"],
1504 time() + SESSION_COOKIE_LIFETIME
);
1509 return authenticate_user($link, "admin", null);
1513 function truncate_string($str, $max_len) {
1514 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1515 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1521 function get_user_theme_path($link) {
1522 $result = db_query($link, "SELECT theme_path
1524 ttrss_themes,ttrss_users
1525 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1526 if (db_num_rows($result) != 0) {
1527 return db_fetch_result($result, 0, "theme_path");
1533 function smart_date_time($timestamp) {
1534 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1535 return date("G:i", $timestamp);
1536 } else if (date("Y", $timestamp) == date("Y")) {
1537 return date("M d, G:i", $timestamp);
1539 return date("Y/m/d, G:i", $timestamp);
1543 function smart_date($timestamp) {
1544 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1546 } else if (date("Y", $timestamp) == date("Y")) {
1547 return date("D m", $timestamp);
1549 return date("Y/m/d", $timestamp);
1553 function sql_bool_to_string($s) {
1554 if ($s == "t" ||
$s == "1") {
1561 function sql_bool_to_bool($s) {
1562 if ($s == "t" ||
$s == "1") {
1570 function toggleEvenOdd($a) {
1577 function sanity_check($link) {
1582 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1583 $schema_version = db_fetch_result($result, 0, "schema_version");
1585 if ($schema_version != SCHEMA_VERSION
) {
1589 if (DB_TYPE
== "mysql") {
1590 $result = db_query($link, "SELECT true", false);
1591 if (db_num_rows($result) != 1) {
1596 error_reporting (DEFAULT_ERROR_LEVEL
);
1598 if ($error_code != 0) {
1599 print_error_xml($error_code);
1606 function file_is_locked($filename) {
1608 $fp = fopen($filename, "r");
1609 error_reporting(DEFAULT_ERROR_LEVEL
);
1611 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1612 flock($fp, LOCK_UN
);
1622 function make_lockfile($filename) {
1623 $fp = fopen($filename, "w");
1625 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1632 function make_stampfile($filename) {
1633 $fp = fopen($filename, "w");
1635 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1636 fwrite($fp, time() . "\n");
1637 flock($fp, LOCK_UN
);
1645 function read_stampfile($filename) {
1648 $fp = fopen($filename, "r");
1649 error_reporting (DEFAULT_ERROR_LEVEL
);
1651 if (flock($fp, LOCK_EX
)) {
1652 $stamp = fgets($fp);
1653 flock($fp, LOCK_UN
);
1661 function sql_random_function() {
1662 if (DB_TYPE
== "mysql") {
1669 function catchup_feed($link, $feed, $cat_view) {
1671 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1676 $cat_qpart = "cat_id = '$feed'";
1678 $cat_qpart = "cat_id IS NULL";
1681 $tmp_result = db_query($link, "SELECT id
1682 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1685 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1687 $tmp_feed = $tmp_line["id"];
1689 db_query($link, "UPDATE ttrss_user_entries
1690 SET unread = false,last_read = NOW()
1691 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1694 } else if ($feed > 0) {
1696 $tmp_result = db_query($link, "SELECT id
1697 FROM ttrss_feeds WHERE parent_feed = '$feed'
1698 ORDER BY cat_id,title");
1700 $parent_ids = array();
1702 if (db_num_rows($tmp_result) > 0) {
1703 while ($p = db_fetch_assoc($tmp_result)) {
1704 array_push($parent_ids, "feed_id = " . $p["id"]);
1707 $children_qpart = implode(" OR ", $parent_ids);
1709 db_query($link, "UPDATE ttrss_user_entries
1710 SET unread = false,last_read = NOW()
1711 WHERE (feed_id = '$feed' OR $children_qpart)
1712 AND owner_uid = " . $_SESSION["uid"]);
1715 db_query($link, "UPDATE ttrss_user_entries
1716 SET unread = false,last_read = NOW()
1717 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1720 } else if ($feed < 0 && $feed > -10) { // special, like starred
1723 db_query($link, "UPDATE ttrss_user_entries
1724 SET unread = false,last_read = NOW()
1725 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1729 db_query($link, "UPDATE ttrss_user_entries
1730 SET unread = false,last_read = NOW()
1731 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1736 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1738 if (DB_TYPE
== "pgsql") {
1739 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1741 $match_part .= " AND date_entered > DATE_SUB(NOW(),
1742 INTERVAL $intl HOUR) ";
1745 db_query($link, "UPDATE ttrss_user_entries
1746 SET unread = false,last_read = NOW()
1747 WHERE $match_part AND owner_uid = ".$_SESSION["uid"]);
1750 } else if ($feed < -10) { // label
1752 // TODO make this more efficient
1754 $label_id = -$feed - 11;
1756 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1757 WHERE id = '$label_id'");
1760 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1762 db_query($link, "BEGIN");
1764 $tmp2_result = db_query($link,
1768 ttrss_user_entries,ttrss_entries,ttrss_feeds
1770 ref_id = ttrss_entries.id AND
1771 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1773 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1775 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1776 db_query($link, "UPDATE
1779 unread = false, last_read = NOW()
1781 int_id = " . $tmp_line["int_id"]);
1784 db_query($link, "COMMIT");
1786 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1787 SET unread = false,last_read = NOW()
1790 AND owner_uid = ".$_SESSION["uid"]); */
1794 db_query($link, "BEGIN");
1796 $tag_name = db_escape_string($feed);
1798 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1799 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1801 while ($line = db_fetch_assoc($result)) {
1802 db_query($link, "UPDATE ttrss_user_entries SET
1803 unread = false, last_read = NOW()
1804 WHERE int_id = " . $line["post_int_id"]);
1806 db_query($link, "COMMIT");
1810 function update_generic_feed($link, $feed, $cat_view) {
1814 $cat_qpart = "cat_id = '$feed'";
1816 $cat_qpart = "cat_id IS NULL";
1819 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1820 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1822 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1823 $feed_url = $tmp_line["feed_url"];
1824 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1828 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1829 WHERE id = '$feed'");
1830 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1831 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1835 function getAllCounters($link, $omode = "flc") {
1836 /* getLabelCounters($link);
1837 getFeedCounters($link);
1838 getTagCounters($link);
1839 getGlobalCounters($link);
1840 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1841 getCategoryCounters($link);
1844 if (!$omode) $omode = "flc";
1846 getGlobalCounters($link);
1848 if (strchr($omode, "l")) getLabelCounters($link);
1849 if (strchr($omode, "f")) getFeedCounters($link);
1850 if (strchr($omode, "t")) getTagCounters($link);
1851 if (strchr($omode, "c")) {
1852 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1853 getCategoryCounters($link);
1858 function getCategoryCounters($link) {
1859 # two special categories are -1 and -2 (all virtuals; all labels)
1861 $ctr = getCategoryUnread($link, -1);
1863 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
1865 $ctr = getCategoryUnread($link, -2);
1867 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
1869 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1870 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1871 AND unread = true)) AS unread FROM ttrss_feeds
1873 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1875 while ($line = db_fetch_assoc($result)) {
1876 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1877 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1878 $line["unread"]."\"/>";
1882 function getCategoryUnread($link, $cat) {
1887 $cat_query = "cat_id = '$cat'";
1889 $cat_query = "cat_id IS NULL";
1892 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1894 AND owner_uid = " . $_SESSION["uid"]);
1896 $cat_feeds = array();
1897 while ($line = db_fetch_assoc($result)) {
1898 array_push($cat_feeds, "feed_id = " . $line["id"]);
1901 if (count($cat_feeds) == 0) return 0;
1903 $match_part = implode(" OR ", $cat_feeds);
1905 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1906 FROM ttrss_user_entries
1907 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1911 # this needs to be rewritten
1912 while ($line = db_fetch_assoc($result)) {
1913 $unread +
= $line["unread"];
1917 } else if ($cat == -1) {
1918 return getFeedUnread($link, -1) +
getFeedUnread($link, -2) +
getFeedUnread($link, -3);
1919 } else if ($cat == -2) {
1921 $rv = getLabelCounters($link, false, true);
1924 foreach (array_keys($rv) as $k) {
1926 $ctr +
= $rv[$k]["counter"];
1934 function getFeedUnread($link, $feed, $is_cat = false) {
1935 $n_feed = sprintf("%d", $feed);
1938 return getCategoryUnread($link, $n_feed);
1939 } else if ($n_feed == -1) {
1940 $match_part = "marked = true";
1941 } else if ($n_feed == -2) {
1942 $match_part = "published = true";
1943 } else if ($n_feed == -3) {
1944 $match_part = "unread = true";
1946 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1948 if (DB_TYPE
== "pgsql") {
1949 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1951 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1954 } else if ($n_feed > 0) {
1956 $result = db_query($link, "SELECT id FROM ttrss_feeds
1957 WHERE parent_feed = '$n_feed'
1959 AND owner_uid = " . $_SESSION["uid"]);
1961 if (db_num_rows($result) > 0) {
1963 $linked_feeds = array();
1964 while ($line = db_fetch_assoc($result)) {
1965 array_push($linked_feeds, "feed_id = " . $line["id"]);
1968 array_push($linked_feeds, "feed_id = $n_feed");
1970 $match_part = implode(" OR ", $linked_feeds);
1972 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1973 FROM ttrss_user_entries
1974 WHERE unread = true AND ($match_part)
1975 AND owner_uid = " . $_SESSION["uid"]);
1979 # this needs to be rewritten
1980 while ($line = db_fetch_assoc($result)) {
1981 $unread +
= $line["unread"];
1987 $match_part = "feed_id = '$n_feed'";
1989 } else if ($feed < -10) {
1991 $label_id = -$feed - 11;
1993 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1994 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1996 $match_part = db_fetch_result($result, 0, "sql_exp");
2001 $result = db_query($link, "SELECT count(int_id) AS unread
2002 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2003 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2004 ttrss_user_entries.ref_id = ttrss_entries.id AND
2005 ttrss_feeds.hidden = false AND
2006 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2010 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2011 FROM ttrss_tags,ttrss_user_entries
2012 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
2013 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2016 $unread = db_fetch_result($result, 0, "unread");
2021 /* FIXME this needs reworking */
2023 function getGlobalUnread($link, $user_id = false) {
2026 $user_id = $_SESSION["uid"];
2029 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2030 WHERE unread = true AND
2031 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2032 ttrss_user_entries.ref_id = ttrss_entries.id AND
2034 ttrss_user_entries.owner_uid = '$user_id'");
2035 $c_id = db_fetch_result($result, 0, "c_id");
2039 function getGlobalCounters($link, $global_unread = -1) {
2040 if ($global_unread == -1) {
2041 $global_unread = getGlobalUnread($link);
2043 print "<counter type=\"global\" id='global-unread'
2044 counter='$global_unread'/>";
2046 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2047 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2049 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2051 print "<counter type=\"global\" id='subscribed-feeds'
2052 counter='$subscribed_feeds'/>";
2056 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2059 if (!$_SESSION["tctr_last_value"]) {
2060 $_SESSION["tctr_last_value"] = array();
2064 $old_counters = $_SESSION["tctr_last_value"];
2066 $tctrs_modified = false;
2068 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2069 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2070 ttrss_user_entries.ref_id = ttrss_entries.id AND
2071 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2072 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2074 select tag_name,0 as count FROM ttrss_tags
2075 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2077 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2078 FROM ttrss_user_entries WHERE int_id = post_int_id
2079 AND unread = true)) AS count FROM ttrss_tags
2080 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2081 ORDER BY count DESC LIMIT 55");
2085 while ($line = db_fetch_assoc($result)) {
2086 $tags[$line["tag_name"]] +
= $line["count"];
2089 foreach (array_keys($tags) as $tag) {
2090 $unread = $tags[$tag];
2092 $tag = htmlspecialchars($tag);
2094 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
2095 $old_counters[$tag] = $unread;
2096 $tctrs_modified = true;
2097 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2102 if ($smart_mode && $tctrs_modified) {
2103 $_SESSION["tctr_last_value"] = $old_counters;
2108 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
2111 if (!$_SESSION["lctr_last_value"]) {
2112 $_SESSION["lctr_last_value"] = array();
2118 $old_counters = $_SESSION["lctr_last_value"];
2119 $lctrs_modified = false;
2121 $count = getFeedUnread($link, -1);
2124 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2126 $ret_arr["-1"]["counter"] = $count;
2127 $ret_arr["-1"]["description"] = __("Starred articles");
2130 $count = getFeedUnread($link, -2);
2133 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2135 $ret_arr["-2"]["counter"] = $count;
2136 $ret_arr["-2"]["description"] = __("Published articles");
2139 $count = getFeedUnread($link, -3);
2142 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2144 $ret_arr["-3"]["counter"] = $count;
2145 $ret_arr["-3"]["description"] = __("Fresh articles");
2149 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2150 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2152 while ($line = db_fetch_assoc($result)) {
2154 $id = -$line["id"] - 11;
2156 $label_name = $line["description"];
2158 error_reporting (0);
2160 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
2161 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
2162 ttrss_feeds.hidden = false AND
2163 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2164 ttrss_user_entries.ref_id = ttrss_entries.id AND
2165 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
2167 $count = db_fetch_result($tmp_result, 0, "count");
2169 if (!$smart_mode ||
$old_counters[$id] != $count) {
2170 $old_counters[$id] = $count;
2171 $lctrs_modified = true;
2173 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2175 $ret_arr[$id]["counter"] = $count;
2176 $ret_arr[$id]["description"] = $label_name;
2180 error_reporting (DEFAULT_ERROR_LEVEL
);
2183 if ($smart_mode && $lctrs_modified) {
2184 $_SESSION["lctr_last_value"] = $old_counters;
2190 /* function getFeedCounter($link, $id) {
2192 $result = db_query($link, "SELECT
2193 count(id) as count,last_error
2194 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2195 WHERE feed_id = '$id' AND unread = true
2196 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2197 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2199 $count = db_fetch_result($result, 0, "count");
2200 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2202 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2205 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2208 if (!$_SESSION["fctr_last_value"]) {
2209 $_SESSION["fctr_last_value"] = array();
2213 $old_counters = $_SESSION["fctr_last_value"];
2215 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2216 SUBSTRING(last_updated,1,19) AS last_updated,
2218 FROM ttrss_entries,ttrss_user_entries
2219 WHERE feed_id = ttrss_feeds.id AND
2220 ttrss_user_entries.ref_id = ttrss_entries.id
2221 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2222 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2223 AND parent_feed IS NULL"); */
2225 $result = db_query($link, "SELECT ttrss_feeds.id,
2226 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2228 COUNT(ttrss_entries.id) AS count
2230 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2231 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2232 AND ttrss_user_entries.unread = true)
2233 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2234 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2235 AND parent_feed IS NULL
2236 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error");
2238 $fctrs_modified = false;
2240 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2242 while ($line = db_fetch_assoc($result)) {
2245 $count = $line["count"];
2246 $last_error = htmlspecialchars($line["last_error"]);
2248 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2249 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2251 $last_updated = date($short_date, strtotime($line["last_updated"]));
2254 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2256 $tmp_result = db_query($link,
2257 "SELECT id,COUNT(unread) AS unread
2258 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2259 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2260 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2262 if (db_num_rows($tmp_result) > 0) {
2263 while ($l = db_fetch_assoc($tmp_result)) {
2264 $count +
= $l["unread"];
2268 if (!$smart_mode ||
$old_counters[$id] != $count) {
2269 $old_counters[$id] = $count;
2270 $fctrs_modified = true;
2273 $error_part = "error=\"$last_error\"";
2279 $has_img_part = "hi=\"$has_img\"";
2284 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
2288 if ($smart_mode && $fctrs_modified) {
2289 $_SESSION["fctr_last_value"] = $old_counters;
2293 function get_script_dt_add() {
2294 if (strpos(VERSION
, ".99") === false) {
2301 function get_pgsql_version($link) {
2302 $result = db_query($link, "SELECT version() AS version");
2303 $version = split(" ", db_fetch_result($result, 0, "version"));
2307 function print_error_xml($code, $add_msg = "") {
2310 $error_msg = $ERRORS[$code];
2313 $error_msg = "$error_msg; $add_msg";
2316 print "<rpc-reply>";
2317 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2318 print "</rpc-reply>";
2321 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2322 $auth_login = '', $auth_pass = '') {
2324 # check for feed:http://url
2325 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2327 # check for feed://URL
2328 if (strpos($feed_link, "//") === 0) {
2329 $feed_link = "http:$feed_link";
2332 if ($feed_link == "") return;
2334 if ($cat_id == "0" ||
!$cat_id) {
2335 $cat_qpart = "NULL";
2337 $cat_qpart = "'$cat_id'";
2340 $result = db_query($link,
2341 "SELECT id FROM ttrss_feeds
2342 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2344 if (db_num_rows($result) == 0) {
2346 $result = db_query($link,
2347 "INSERT INTO ttrss_feeds
2348 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2349 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2350 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2352 $result = db_query($link,
2353 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2354 AND owner_uid = " . $_SESSION["uid"]);
2356 $feed_id = db_fetch_result($result, 0, "id");
2359 update_rss_feed($link, $feed_link, $feed_id, true);
2368 function print_feed_select($link, $id, $default_id = "",
2369 $attributes = "", $include_all_feeds = true) {
2371 print "<select id=\"$id\" name=\"$id\" $attributes>";
2372 if ($include_all_feeds) {
2373 print "<option value=\"0\">".__('All feeds')."</option>";
2376 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2377 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2379 if (db_num_rows($result) > 0 && $include_all_feeds) {
2380 print "<option disabled>--------</option>";
2383 while ($line = db_fetch_assoc($result)) {
2384 if ($line["id"] == $default_id) {
2385 $is_selected = "selected";
2389 printf("<option $is_selected value='%d'>%s</option>",
2390 $line["id"], htmlspecialchars($line["title"]));
2396 function print_feed_cat_select($link, $id, $default_id = "",
2397 $attributes = "", $include_all_cats = true) {
2399 print "<select id=\"$id\" name=\"$id\" $attributes>";
2401 if ($include_all_cats) {
2402 print "<option value=\"0\">".__('Uncategorized')."</option>";
2405 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2406 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2408 if (db_num_rows($result) > 0 && $include_all_cats) {
2409 print "<option disabled>--------</option>";
2412 while ($line = db_fetch_assoc($result)) {
2413 if ($line["id"] == $default_id) {
2414 $is_selected = "selected";
2418 printf("<option $is_selected value='%d'>%s</option>",
2419 $line["id"], htmlspecialchars($line["title"]));
2425 function checkbox_to_sql_bool($val) {
2426 return ($val == "on") ?
"true" : "false";
2429 function getFeedCatTitle($link, $id) {
2431 return __("Special");
2432 } else if ($id < -10) {
2433 return __("Labels");
2434 } else if ($id > 0) {
2435 $result = db_query($link, "SELECT ttrss_feed_categories.title
2436 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2437 cat_id = ttrss_feed_categories.id");
2438 if (db_num_rows($result) == 1) {
2439 return db_fetch_result($result, 0, "title");
2441 return __("Uncategorized");
2444 return "getFeedCatTitle($id) failed";
2449 function getFeedTitle($link, $id) {
2451 return __("Starred articles");
2452 } else if ($id == -2) {
2453 return __("Published articles");
2454 } else if ($id == -3) {
2455 return __("Fresh articles");
2456 } else if ($id < -10) {
2457 $label_id = -10 - $id;
2458 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2459 if (db_num_rows($result) == 1) {
2460 return db_fetch_result($result, 0, "description");
2462 return "Unknown label ($label_id)";
2465 } else if ($id > 0) {
2466 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2467 if (db_num_rows($result) == 1) {
2468 return db_fetch_result($result, 0, "title");
2470 return "Unknown feed ($id)";
2473 return "getFeedTitle($id) failed";
2478 function get_session_cookie_name() {
2479 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2482 function print_init_params($link) {
2483 print "<init-params>";
2484 if ($_SESSION["stored-params"]) {
2485 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2487 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2488 print "<param key=\"$key\" value=\"$value\"/>";
2493 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2494 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2495 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2497 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2498 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2500 print "<param key=\"hide_read_feeds\" value=\"" .
2501 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2503 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2504 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2506 print "<param key=\"confirm_feed_catchup\" value=\"" .
2507 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2509 print "<param key=\"cdm_auto_catchup\" value=\"" .
2510 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2512 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2514 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2516 print "<param key=\"default_view_mode\" value=\"" .
2517 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2519 print "<param key=\"default_view_limit\" value=\"" .
2520 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2522 print "<param key=\"prefs_active_tab\" value=\"" .
2523 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2525 print "<param key=\"infobox_disable_overlay\" value=\"" .
2526 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2528 print "<param key=\"icons_location\" value=\"" .
2531 print "</init-params>";
2534 function print_runtime_info($link) {
2535 print "<runtime-info>";
2536 if (ENABLE_UPDATE_DAEMON
) {
2537 print "<param key=\"daemon_is_running\" value=\"".
2538 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2540 if ($_SESSION["daemon_stamp_check"] +
600 < time()) {
2542 $stamp = (int)read_stampfile("update_daemon.stamp");
2545 if ($stamp +
86400*3 < time()) {
2546 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2548 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2551 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2553 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2556 $_SESSION["daemon_stamp_check"] = time();
2560 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2562 if ($_SESSION["last_version_check"] +
7200 < time()) {
2563 $new_version_details = check_for_update($link);
2565 print "<param key=\"new_version_available\" value=\"".
2566 sprintf("%d", $new_version_details != ""). "\"/>";
2568 $_SESSION["last_version_check"] = time();
2572 print "</runtime-info>";
2575 function getSearchSql($search, $match_on) {
2577 $search_query_part = "";
2579 $keywords = split(" ", $search);
2580 $query_keywords = array();
2582 if ($match_on == "both") {
2584 foreach ($keywords as $k) {
2585 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2586 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2589 $search_query_part = implode("AND", $query_keywords) . " AND ";
2591 } else if ($match_on == "title") {
2593 foreach ($keywords as $k) {
2594 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2597 $search_query_part = implode("AND", $query_keywords) . " AND ";
2599 } else if ($match_on == "content") {
2601 foreach ($keywords as $k) {
2602 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2606 $search_query_part = implode("AND", $query_keywords);
2608 return $search_query_part;
2611 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2613 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2617 $search_query_part = getSearchSql($search, $match_on);
2618 $search_query_part .= " AND ";
2621 $search_query_part = "";
2624 $view_query_part = "";
2626 if ($view_mode == "adaptive") {
2628 $view_query_part = " ";
2629 } else if ($feed != -1) {
2630 $unread = getFeedUnread($link, $feed, $cat_view);
2632 $view_query_part = " unread = true AND ";
2637 if ($view_mode == "marked") {
2638 $view_query_part = " marked = true AND ";
2641 if ($view_mode == "unread") {
2642 $view_query_part = " unread = true AND ";
2646 $limit_query_part = "LIMIT " . $limit;
2649 $vfeed_query_part = "";
2651 // override query strategy and enable feed display when searching globally
2652 if ($search && $search_mode == "all_feeds") {
2653 $query_strategy_part = "ttrss_entries.id > 0";
2654 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2655 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2656 $query_strategy_part = "ttrss_entries.id > 0";
2657 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2658 id = feed_id) as feed_title,";
2659 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2661 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2663 $tmp_result = false;
2666 $tmp_result = db_query($link, "SELECT id
2667 FROM ttrss_feeds WHERE cat_id = '$feed'");
2669 $tmp_result = db_query($link, "SELECT id
2670 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2671 WHERE id = '$feed') AND id != '$feed'");
2674 $cat_siblings = array();
2676 if (db_num_rows($tmp_result) > 0) {
2677 while ($p = db_fetch_assoc($tmp_result)) {
2678 array_push($cat_siblings, "feed_id = " . $p["id"]);
2681 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2682 $feed, implode(" OR ", $cat_siblings));
2685 $query_strategy_part = "ttrss_entries.id > 0";
2688 } else if ($feed >= 0) {
2693 $query_strategy_part = "cat_id = '$feed'";
2695 $query_strategy_part = "cat_id IS NULL";
2698 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2701 $tmp_result = db_query($link, "SELECT id
2702 FROM ttrss_feeds WHERE parent_feed = '$feed'
2703 ORDER BY cat_id,title");
2705 $parent_ids = array();
2707 if (db_num_rows($tmp_result) > 0) {
2708 while ($p = db_fetch_assoc($tmp_result)) {
2709 array_push($parent_ids, "feed_id = " . $p["id"]);
2712 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2713 $feed, implode(" OR ", $parent_ids));
2715 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2717 $query_strategy_part = "feed_id = '$feed'";
2720 } else if ($feed == -1) { // starred virtual feed
2721 $query_strategy_part = "marked = true";
2722 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2723 } else if ($feed == -2) { // published virtual feed
2724 $query_strategy_part = "published = true";
2725 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2726 } else if ($feed == -3) { // fresh virtual feed
2727 $query_strategy_part = "unread = true";
2729 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2731 if (DB_TYPE
== "pgsql") {
2732 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2734 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2737 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2738 } else if ($feed <= -10) { // labels
2739 $label_id = -$feed - 11;
2741 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2742 WHERE id = '$label_id'");
2744 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2746 if (!$query_strategy_part) {
2750 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2752 $query_strategy_part = "id > 0"; // dumb
2755 if (get_pref($link, 'REVERSE_HEADLINES')) {
2756 $order_by = "updated";
2758 $order_by = "updated DESC";
2761 if ($override_order) {
2762 $order_by = $override_order;
2767 if ($search && $search_mode == "all_feeds") {
2768 $feed_title = __("Search results")." ($search)";
2769 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2770 $feed_title = __("Search results")." ($search, $feed)";
2771 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2772 $feed_title = $feed;
2773 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2778 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2779 WHERE id = '$feed' AND owner_uid = $owner_uid");
2780 $feed_title = db_fetch_result($result, 0, "title");
2782 $feed_title = __("Uncategorized");
2786 $feed_title = __("Searched for")." $search ($feed_title)";
2791 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2792 WHERE id = '$feed' AND owner_uid = $owner_uid");
2794 $feed_title = db_fetch_result($result, 0, "title");
2795 $feed_site_url = db_fetch_result($result, 0, "site_url");
2796 $last_error = db_fetch_result($result, 0, "last_error");
2799 $feed_title = __("Searched for") . " $search ($feed_title)";
2803 } else if ($feed == -1) {
2804 $feed_title = __("Starred articles");
2805 } else if ($feed == -2) {
2806 $feed_title = __("Published articles");
2807 } else if ($feed == -3) {
2808 $feed_title = __("Fresh articles");
2809 } else if ($feed < -10) {
2810 $label_id = -$feed - 11;
2811 $result = db_query($link, "SELECT description FROM ttrss_labels
2812 WHERE id = '$label_id'");
2813 $feed_title = db_fetch_result($result, 0, "description");
2816 $feed_title = __("Searched for") . " $search ($feed_title)";
2822 if ($feed < -10) error_reporting (0);
2824 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2827 $feed_kind = "Feeds";
2829 $feed_kind = "Labels";
2832 $content_query_part = "content as content_preview,";
2834 if ($limit_query_part) {
2835 $offset_query_part = "OFFSET $offset";
2840 ttrss_entries.id,ttrss_entries.title,
2842 unread,feed_id,marked,published,link,last_read,
2843 SUBSTRING(last_read,1,19) as last_read_noms,
2846 SUBSTRING(updated,1,19) as updated_noms,
2849 ttrss_entries,ttrss_user_entries,ttrss_feeds
2851 ttrss_feeds.hidden = false AND
2852 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2853 ttrss_user_entries.ref_id = ttrss_entries.id AND
2854 ttrss_user_entries.owner_uid = '$owner_uid' AND
2857 $query_strategy_part ORDER BY $order_by
2858 $limit_query_part $offset_query_part";
2860 $result = db_query($link, $query);
2862 if ($_GET["debug"]) print $query;
2867 $feed_kind = "Tags";
2869 $result = db_query($link, "SELECT
2871 ttrss_entries.id as id,title,
2874 marked,link,last_read,
2875 SUBSTRING(last_read,1,19) as last_read_noms,
2878 SUBSTRING(updated,1,19) as updated_noms
2880 ttrss_entries,ttrss_user_entries,ttrss_tags
2882 ref_id = ttrss_entries.id AND
2883 ttrss_user_entries.owner_uid = '$owner_uid' AND
2884 post_int_id = int_id AND tag_name = '$feed' AND
2887 $query_strategy_part ORDER BY $order_by
2888 $limit_query_part");
2891 return array($result, $feed_title, $feed_site_url, $last_error);
2895 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
2896 $search, $search_mode, $match_on) {
2898 $qfh_ret = queryFeedHeadlines($link, $feed,
2899 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
2902 $result = $qfh_ret[0];
2903 $feed_title = htmlspecialchars($qfh_ret[1]);
2904 $feed_site_url = $qfh_ret[2];
2905 $last_error = $qfh_ret[3];
2907 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
2909 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
2910 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
2911 <rss version=\"2.0\">
2913 <title>$feed_title</title>
2914 <link>$feed_site_url</link>
2915 <description>Feed generated by Tiny Tiny RSS</description>";
2917 while ($line = db_fetch_assoc($result)) {
2919 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
2920 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2922 $tags = get_article_tags($link, $line["id"], $owner_uid);
2924 foreach ($tags as $tag) {
2925 print "<category>" . htmlspecialchars($tag) . "</category>";
2928 $rfc822_date = date('r', strtotime($line["updated"]));
2930 print "<pubDate>$rfc822_date</pubDate>";
2933 htmlspecialchars($line["title"]) . "</title>";
2935 print "<description><![CDATA[" .
2936 $line["content_preview"] . "]]></description>";
2941 print "</channel></rss>";
2945 function getCategoryTitle($link, $cat_id) {
2947 if ($cat_id == -1) {
2948 return __("Special");
2949 } else if ($cat_id == -2) {
2950 return __("Labels");
2953 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2956 if (db_num_rows($result) == 1) {
2957 return db_fetch_result($result, 0, "title");
2959 return "Uncategorized";
2964 // http://ru2.php.net/strip-tags
2966 function strip_tags_long($textstring, $allowed){
2967 while($textstring != strip_tags($textstring, $allowed))
2969 while (strlen($textstring) != 0)
2971 if (strlen($textstring) > 1024) {
2974 $otherlen = strlen($textstring);
2976 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
2977 $safetext .= $temptext;
2978 $textstring = substr_replace($textstring,'',0,$otherlen);
2980 $textstring = $safetext;
2986 function sanitize_rss($link, $str, $force_strip_tags = false) {
2989 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
2991 global $tw_paranoya_setup;
2993 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
2995 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
2996 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3002 function send_headlines_digests($link, $limit = 100) {
3004 if (!DIGEST_ENABLE
) return false;
3006 $user_limit = DIGEST_EMAIL_LIMIT
;
3009 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3011 if (DB_TYPE
== "pgsql") {
3012 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3013 } else if (DB_TYPE
== "mysql") {
3014 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3017 $result = db_query($link, "SELECT id,email FROM ttrss_users
3018 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3020 while ($line = db_fetch_assoc($result)) {
3021 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3022 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3024 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3025 $digest = $tuple[0];
3026 $headlines_count = $tuple[1];
3028 if ($headlines_count > 0) {
3029 $rc = mail($line["login"] . " <" . $line["email"] . ">",
3030 "[tt-rss] New headlines for last 24 hours", $digest,
3031 "From: " . MAIL_FROM
. "\n".
3032 "Content-Type: text/plain; charset=\"utf-8\"\n".
3033 "Content-Transfer-Encoding: 8bit\n");
3035 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3036 WHERE id = " . $line["id"]);
3038 print "No headlines\n";
3043 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
3047 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3048 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
3049 $tmp .= "=======================================================\n\n";
3051 if (DB_TYPE
== "pgsql") {
3052 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3053 } else if (DB_TYPE
== "mysql") {
3054 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3057 $result = db_query($link, "SELECT ttrss_entries.title,
3058 ttrss_feeds.title AS feed_title,
3061 SUBSTRING(last_updated,1,19) AS last_updated
3063 ttrss_user_entries,ttrss_entries,ttrss_feeds
3065 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3066 AND include_in_digest = true
3068 AND ttrss_user_entries.owner_uid = $user_id
3069 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
3072 $cur_feed_title = "";
3074 $headlines_count = db_num_rows($result);
3076 while ($line = db_fetch_assoc($result)) {
3077 $updated = smart_date_time(strtotime($line["last_updated"]));
3078 $feed_title = $line["feed_title"];
3080 if ($cur_feed_title != $feed_title) {
3081 $cur_feed_title = $feed_title;
3083 $tmp .= "$feed_title\n\n";
3086 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
3087 $tmp .= " " . trim($line["link"]) . "\n";
3092 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
3093 DIGEST_HOSTNAME
. "\n".
3094 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
3097 return array($tmp, $headlines_count);
3100 function check_for_update($link, $brief_fmt = true) {
3101 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3103 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
3108 $rss = fetch_rss($releases_feed);
3109 error_reporting (DEFAULT_ERROR_LEVEL
);
3113 $items = $rss->items
;
3115 if (!$items ||
!is_array($items)) $items = $rss->entries
;
3116 if (!$items ||
!is_array($items)) $items = $rss;
3118 if (!is_array($items) ||
count($items) == 0) {
3122 $latest_item = $items[0];
3124 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
3126 $release_url = sanitize_rss($link, $latest_item["link"]);
3127 $content = sanitize_rss($link, $latest_item["description"]);
3129 if (version_compare(VERSION
, $latest_version) == -1) {
3131 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3132 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3133 <div id=\"milestoneDetails\">$content</div>");
3135 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3136 <div class='milestoneDetails'>$content</div>
3137 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3138 download and update information.";
3145 function markArticlesById($link, $ids, $cmode) {
3149 foreach ($ids as $id) {
3150 array_push($tmp_ids, "ref_id = '$id'");
3153 $ids_qpart = join(" OR ", $tmp_ids);
3156 db_query($link, "UPDATE ttrss_user_entries SET
3157 marked = false,last_read = NOW()
3158 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3159 } else if ($cmode == 1) {
3160 db_query($link, "UPDATE ttrss_user_entries SET
3162 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3164 db_query($link, "UPDATE ttrss_user_entries SET
3165 marked = NOT marked,last_read = NOW()
3166 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3170 function publishArticlesById($link, $ids, $cmode) {
3174 foreach ($ids as $id) {
3175 array_push($tmp_ids, "ref_id = '$id'");
3178 $ids_qpart = join(" OR ", $tmp_ids);
3181 db_query($link, "UPDATE ttrss_user_entries SET
3182 published = false,last_read = NOW()
3183 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3184 } else if ($cmode == 1) {
3185 db_query($link, "UPDATE ttrss_user_entries SET
3187 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3189 db_query($link, "UPDATE ttrss_user_entries SET
3190 published = NOT published,last_read = NOW()
3191 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3195 function catchupArticlesById($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 unread = 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 unread = NOT unread,last_read = NOW()
3216 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3220 function catchupArticleById($link, $id, $cmode) {
3223 db_query($link, "UPDATE ttrss_user_entries SET
3224 unread = false,last_read = NOW()
3225 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3226 } else if ($cmode == 1) {
3227 db_query($link, "UPDATE ttrss_user_entries SET
3229 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3231 db_query($link, "UPDATE ttrss_user_entries SET
3232 unread = NOT unread,last_read = NOW()
3233 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3237 function make_guid_from_title($title) {
3238 return preg_replace("/[ \"\',.:;]/", "-",
3239 mb_strtolower(strip_tags($title), 'utf-8'));
3242 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3243 $bottom = false, $rtl_content = false, $feed_id = 0,
3244 $is_cat = false, $search = false, $match_on = false,
3245 $search_mode = false, $offset = 0, $limit = 0) {
3247 $user_page_offset = $offset +
1;
3250 $class = "headlinesSubToolbar";
3251 $tid = "headlineActionsTop";
3253 $class = "headlinesSubToolbar";
3254 $tid = "headlineActionsBottom";
3257 print "<table class=\"$class\" id=\"$tid\"
3258 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3266 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3267 $page_next_link = "javascript:viewFeedGoPage(1)";
3268 $page_first_link = "javascript:viewFeedGoPage(0)";
3270 $catchup_page_link = "javascript:catchupPage()";
3271 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3272 $catchup_sel_link = "javascript:catchupSelection()";
3274 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3276 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3277 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3278 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3280 $tog_unread_link = "javascript:selectionToggleUnread()";
3281 $tog_marked_link = "javascript:selectionToggleMarked()";
3282 $tog_published_link = "javascript:selectionTogglePublished()";
3286 $sel_all_link = "javascript:cdmSelectArticles('all')";
3287 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3288 $sel_none_link = "javascript:cdmSelectArticles('none')";
3290 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3291 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3292 $tog_published_link = "javascript:selectionTogglePublished(true)";
3296 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3298 print "<td class=\"headlineActions$rtl_cpart\">
3299 <ul class=\"headlineDropdownMenu\">
3302 <a href=\"$sel_all_link\">".__('All')."</a>,
3303 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3304 <a href=\"$sel_none_link\">".__('None')."</a></li>
3305 <li class=\"vsep\"> </li>
3306 <li class=\"top\">".__('Toggle')."<ul>
3307 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3308 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3309 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3311 <li class=\"vsep\"> </li>
3312 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
3313 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
3314 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3315 <li><span class=\"insensitive\">--------</span></li>
3316 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3317 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3318 <li><span class=\"insensitive\">--------</span></li>
3319 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3322 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3324 if ($limit != 0 && !$search && $enable_pagination) {
3326 <li class=\"vsep\"> </li>
3327 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3328 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3329 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3333 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3335 <li class=\"vsep\"> </li>
3337 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3338 '$match_on', '$feed_id', '$is_cat');\">
3339 ".__('Convert to label')."</a></td>";
3345 // old style subtoolbar:
3347 print "<td class=\"headlineActions$rtl_cpart\">".
3349 <a href=\"$sel_all_link\">".__('All')."</a>,
3350 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3351 <a href=\"$sel_none_link\">".__('None')."</a>
3353 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3354 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3356 __('Mark as read:')."
3357 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3358 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3360 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3363 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3364 '$match_on', '$feed_id', '$is_cat');\">
3365 ".__('Convert to label')."</a>";
3372 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3373 print "<td class=\"headlineActions$rtl_cpart\">
3374 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3375 '$match_on', '$feed_id', '$is_cat');\">
3376 ".__('Convert to Label')."</a></td>";
3379 print "<td class=\"headlineTitle$rtl_cpart\">";
3381 if ($feed_site_url) {
3383 $target = "target=\"_new\"";
3385 print "<a $target href=\"$feed_site_url\">".
3386 truncate_string($feed_title,30)."</a>";
3392 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3395 if ($user_page_offset > 1) {
3396 print " [$user_page_offset] ";
3402 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3403 <img class=\"noborder\"
3404 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3409 print "</tr></table>";
3413 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3415 $tmp_category = getCategoryTitle($link, $cat_id);
3416 $cat_unread = getCategoryUnread($link, $cat_id);
3419 $holder_style = "display:none;";
3426 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3428 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3429 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3432 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3434 print "<span id=\"FCAP-$cat_id\">";
3437 print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3438 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3448 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3451 function outputFeedList($link, $tags = false) {
3453 print "<ul class=\"feedList\" id=\"feedList\">";
3455 $owner_uid = $_SESSION["uid"];
3459 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3461 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
3464 $cat_hidden = false;
3467 # print "<li class=\"feedCat\">".__('Special')."</li>";
3468 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3469 # print "<li class=\"feedCat\">".
3470 # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3471 # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3473 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3474 # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3476 # $cat_unread = getCategoryUnread($link, -1);
3477 # $tmp_category = __("Special");
3478 # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3480 printCategoryHeader($link, -1, $cat_hidden, false);
3483 $num_starred = getFeedUnread($link, -1);
3484 $num_published = getFeedUnread($link, -2);
3485 $num_fresh = getFeedUnread($link, -3);
3489 if ($num_fresh > 0) $class .= "Unread";
3491 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3492 "images/fresh.png", $link);
3496 if ($num_starred > 0) $class .= "Unread";
3498 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3499 "images/mark_set.png", $link);
3503 if ($num_published > 0) $class .= "Unread";
3505 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3506 "images/pub_set.png", $link);
3508 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3514 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3516 $result = db_query($link, "SELECT id,sql_exp,description FROM
3517 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3519 if (db_num_rows($result) > 0) {
3520 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3522 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
3525 $cat_hidden = false;
3528 printCategoryHeader($link, -2, $cat_hidden, false);
3530 # print "<li class=\"feedCat\">".
3531 # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3532 # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3534 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
3536 print "<li><hr></li>";
3540 while ($line = db_fetch_assoc($result)) {
3542 error_reporting (0);
3544 $label_id = -$line['id'] - 11;
3545 $count = getFeedUnread($link, $label_id);
3553 error_reporting (DEFAULT_ERROR_LEVEL
);
3555 printFeedEntry($label_id,
3556 $class, $line["description"],
3557 $count, "images/label.png", $link);
3561 if (db_num_rows($result) > 0) {
3562 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3569 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3570 print "<li><hr></li>";
3573 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3574 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3575 $order_by_qpart = "category,unread DESC,title";
3577 $order_by_qpart = "category,title";
3580 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3581 $order_by_qpart = "unread DESC,title";
3583 $order_by_qpart = "title";
3587 $result = db_query($link, "SELECT ttrss_feeds.*,
3588 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3589 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3590 WHERE feed_id = ttrss_feeds.id AND unread = true
3591 AND ttrss_user_entries.ref_id = ttrss_entries.id
3592 AND owner_uid = '$owner_uid') as unread,
3594 ttrss_feed_categories.title AS category,
3595 ttrss_feed_categories.collapsed
3596 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3597 ON (ttrss_feed_categories.id = cat_id)
3599 ttrss_feeds.hidden = false AND
3600 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3601 ORDER BY $order_by_qpart");
3603 $actid = $_GET["actid"];
3613 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3615 while ($line = db_fetch_assoc($result)) {
3617 $feed = trim($line["title"]);
3619 if (!$feed) $feed = "[Untitled]";
3621 $feed_id = $line["id"];
3623 $subop = $_GET["subop"];
3625 $unread = $line["unread"];
3627 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3628 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3630 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3633 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3636 $rtl_tag = "dir=\"RTL\"";
3641 $tmp_result = db_query($link,
3642 "SELECT id,COUNT(unread) AS unread
3643 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3644 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3645 WHERE parent_feed = '$feed_id' AND unread = true
3646 GROUP BY ttrss_feeds.id");
3648 if (db_num_rows($tmp_result) > 0) {
3649 while ($l = db_fetch_assoc($tmp_result)) {
3650 $unread +
= $l["unread"];
3654 $cat_id = $line["cat_id"];
3656 $tmp_category = $line["category"];
3658 if (!$tmp_category) {
3659 $tmp_category = __("Uncategorized");
3662 // $class = ($lnum % 2) ? "even" : "odd";
3664 if ($line["last_error"]) {
3670 if ($unread > 0) $class .= "Unread";
3672 if ($actid == $feed_id) {
3673 $class .= "Selected";
3676 $total_unread +
= $unread;
3678 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3684 $category = $tmp_category;
3686 $collapsed = $line["collapsed"];
3688 // workaround for NULL category
3689 if ($category == __("Uncategorized")) {
3690 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3695 if ($collapsed == "t" ||
$collapsed == "1") {
3696 $holder_class = "feedCatHolder";
3697 $holder_style = "display:none;";
3700 $holder_class = "feedCatHolder";
3705 $cat_id = sprintf("%d", $cat_id);
3707 $cat_unread = getCategoryUnread($link, $cat_id);
3709 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3711 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3712 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3713 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3714 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3715 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3718 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3721 printFeedEntry($feed_id, $class, $feed, $unread,
3722 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
3723 $last_updated, $line["last_error"]);
3728 if (db_num_rows($result) == 0) {
3729 print "<li>".__('No feeds to display.')."</li>";
3736 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3737 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3738 post_int_id = ttrss_user_entries.int_id AND
3739 unread = true AND ref_id = ttrss_entries.id
3740 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3742 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3743 ORDER BY tag_name"); */
3745 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3746 print "<li class=\"feedCat\">".__('Tags')."</li>";
3747 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3750 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3751 FROM ttrss_user_entries WHERE int_id = post_int_id
3752 AND unread = true)) AS count FROM ttrss_tags
3753 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
3754 ORDER BY count DESC LIMIT 50");
3758 while ($line = db_fetch_assoc($result)) {
3759 $tags[$line["tag_name"]] +
= $line["count"];
3762 foreach (array_keys($tags) as $tag) {
3764 $unread = $tags[$tag];
3772 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3776 if (db_num_rows($result) == 0) {
3777 print "<li>No tags to display.</li>";
3780 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3790 function get_article_tags($link, $id, $owner_uid = 0) {
3792 $a_id = db_escape_string($id);
3794 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3796 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3797 owner_uid as owner FROM
3798 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3799 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
3803 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3804 array_push($tags, $tmp_line["tag_name"]);
3810 function trim_value(&$value) {
3811 $value = trim($value);
3814 function trim_array($array) {
3816 array_walk($tmp, 'trim_value');
3820 function tag_is_valid($tag) {
3821 if ($tag == '') return false;
3822 if (preg_match("/^[0-9]*$/", $tag)) return false;
3824 $tag = iconv("utf-8", "utf-8", $tag);
3825 if (!$tag) return false;
3830 function render_login_form($link, $mobile = false) {
3832 require_once "login_form.php";
3834 require_once "mobile/login_form.php";
3838 // from http://developer.apple.com/internet/safari/faq.html
3839 function no_cache_incantation() {
3840 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3841 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3842 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3843 header("Cache-Control: post-check=0, pre-check=0", false);
3844 header("Pragma: no-cache"); // HTTP/1.0
3847 function format_warning($msg, $id = "") {
3848 return "<div class=\"warning\" id=\"$id\">
3849 <img src=\"images/sign_excl.png\">$msg</div>";
3852 function format_notice($msg) {
3853 return "<div class=\"notice\">
3854 <img src=\"images/sign_info.png\">$msg</div>";
3857 function format_error($msg) {
3858 return "<div class=\"error\">
3859 <img src=\"images/sign_excl.png\">$msg</div>";
3862 function print_notice($msg) {
3863 return print format_notice($msg);
3866 function print_warning($msg) {
3867 return print format_warning($msg);
3870 function print_error($msg) {
3871 return print format_error($msg);
3875 function T_sprintf() {
3876 $args = func_get_args();
3877 return vsprintf(__(array_shift($args)), $args);
3880 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3882 /* we can figure out feed_id from article id anyway, why do we
3883 * pass feed_id here? */
3885 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3886 WHERE ref_id = '$id'");
3888 $feed_id = db_fetch_result($result, 0, "feed_id");
3890 print "<article id='$id'><![CDATA[";
3892 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3893 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3895 if (db_num_rows($result) == 1) {
3896 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3898 $rtl_content = false;
3902 $rtl_tag = "dir=\"RTL\"";
3909 if ($mark_as_read) {
3910 $result = db_query($link, "UPDATE ttrss_user_entries
3911 SET unread = false,last_read = NOW()
3912 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3915 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3916 SUBSTRING(updated,1,16) as updated,
3917 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3920 FROM ttrss_entries,ttrss_user_entries
3921 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3927 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3928 $link_target = "target=\"_new\"";
3931 $line = db_fetch_assoc($result);
3933 if ($line["icon_url"]) {
3934 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3936 $feed_icon = " ";
3939 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
3940 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
3942 $entry_comments = "";
3945 $num_comments = $line["num_comments"];
3946 $entry_comments = "";
3948 if ($num_comments > 0) {
3949 if ($line["comments"]) {
3950 $comments_url = $line["comments"];
3952 $comments_url = $line["link"];
3954 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
3956 if ($line["comments"] && $line["link"] != $line["comments"]) {
3957 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
3961 print "<div class=\"postReply\">";
3963 print "<div class=\"postHeader\">";
3965 $entry_author = $line["author"];
3967 if ($entry_author) {
3968 $entry_author = __(" - by ") . $entry_author;
3971 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
3972 strtotime($line["updated"]));
3974 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3976 if ($line["link"]) {
3977 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
3978 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
3980 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3983 /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3984 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
3985 ORDER BY tag_name"); */
3987 $tags = get_article_tags($link, $id);
3994 foreach ($tags as $tag) {
3996 $tag_escaped = str_replace("'", "\\'", $tag);
3998 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4000 if ($num_tags == 6) {
4003 } else if ($num_tags < 6) {
4004 $tags_str .= $tag_str;
4006 $f_tags_str .= $tag_str;
4009 $tags_str = preg_replace("/, $/", "", $tags_str);
4010 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4012 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4013 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4015 if (!$entry_comments) $entry_comments = " "; # placeholder
4017 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4019 print "<div style='float : right'>
4020 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4022 <a title=\"Edit tags for this article\"
4023 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4024 <div clear='both'>$entry_comments</div>";
4028 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4029 print "<div class=\"postContent\">";
4031 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4033 $line["content"] = sanitize_rss($link, $line["content"]);
4035 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4036 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4039 print $line["content"] . "</div>";
4045 print "]]></article>";
4049 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4050 $next_unread_feed, $offset) {
4052 $timing_info = getmicrotime();
4054 $topmost_article_ids = array();
4060 if ($subop == "undefined") $subop = "";
4062 if ($subop == "CatchupSelected") {
4063 $ids = split(",", db_escape_string($_GET["ids"]));
4064 $cmode = sprintf("%d", $_GET["cmode"]);
4066 catchupArticlesById($link, $ids, $cmode);
4069 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4070 update_generic_feed($link, $feed, $cat_view);
4073 if ($subop == "MarkAllRead") {
4074 catchup_feed($link, $feed, $cat_view);
4076 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4077 if ($next_unread_feed) {
4078 $feed = $next_unread_feed;
4084 $result = db_query($link,
4085 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4087 if (db_num_rows($result) == 0) {
4088 print "<div align='center'>".__('Feed not found.')."</div>";
4093 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4095 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4096 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4098 if (db_num_rows($result) == 1) {
4099 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4101 $rtl_content = false;
4105 $rtl_tag = "dir=\"RTL\"";
4111 $rtl_content = false;
4114 $script_dt_add = get_script_dt_add();
4116 /// START /////////////////////////////////////////////////////////////////////////////////
4118 $search = db_escape_string($_GET["query"]);
4119 $search_mode = db_escape_string($_GET["search_mode"]);
4120 $match_on = db_escape_string($_GET["match_on"]);
4126 $real_offset = $offset * $limit;
4128 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4130 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4131 $search, $search_mode, $match_on, false, $real_offset);
4133 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4135 $result = $qfh_ret[0];
4136 $feed_title = $qfh_ret[1];
4137 $feed_site_url = $qfh_ret[2];
4138 $last_error = $qfh_ret[3];
4141 $feed_site_url = article_publish_url($link);
4144 /// STOP //////////////////////////////////////////////////////////////////////////////////
4147 print "<div id=\"headlinesContainer\" $rtl_tag>";
4150 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4154 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4155 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4158 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4161 if (db_num_rows($result) > 0) {
4163 # print "\{$offset}";
4165 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4166 print "<table class=\"headlinesList\" id=\"headlinesList\"
4167 cellspacing=\"0\">";
4172 error_reporting (DEFAULT_ERROR_LEVEL
);
4176 while ($line = db_fetch_assoc($result)) {
4178 $class = ($lnum %
2) ?
"even" : "odd";
4181 $feed_id = $line["feed_id"];
4183 if (count($topmost_article_ids) < 5) {
4184 array_push($topmost_article_ids, $id);
4187 if ($line["last_read"] == "" &&
4188 ($line["unread"] != "t" && $line["unread"] != "1")) {
4190 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4193 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4197 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
4205 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
4206 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\"
4208 alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
4210 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\"
4212 alt=\"Set mark\" onclick='javascript:tMark($id)'>";
4215 if ($line["published"] == "t" ||
$line["published"] == "1") {
4216 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\"
4218 alt=\"Unpublish\" onclick='javascript:tPub($id)'>";
4220 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\"
4222 alt=\"Publish\" onclick='javascript:tPub($id)'>";
4225 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4226 # $line["title"] . "</a>";
4228 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4229 $line["title"] . "</a>";
4231 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4232 # $line["title"] . "</a>";
4234 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4235 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
4237 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4238 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
4241 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4242 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4246 $entry_author = $line["author"];
4248 if ($entry_author) {
4249 $entry_author = " - by $entry_author";
4252 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4254 print "<tr class='$class' id='RROW-$id'>";
4256 print "<td class='hlUpdPic'>$update_pic</td>";
4258 print "<td class='hlSelectRow'>
4259 <input type=\"checkbox\" onclick=\"tSR(this)\"
4263 print "<td class='hlMarkedPic'>$marked_pic</td>";
4264 print "<td class='hlMarkedPic'>$published_pic</td>";
4266 if ($line["feed_title"]) {
4267 print "<td class='hlContent'>$content_link</td>";
4268 print "<td class='hlFeed'>
4269 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4270 truncate_string($line["feed_title"],30)."</a> </td>";
4272 print "<td class='hlContent' valign='middle'>";
4274 print "<a href=\"javascript:view($id,$feed_id);\">" .
4277 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4278 if ($content_preview) {
4279 print "<span class=\"contentPreview\"> - $content_preview</span>";
4287 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
4294 $add_class = "Unread";
4299 print "<div class=\"cdmArticle$add_class\"
4300 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4301 onmouseout='cdmMouseOut(this)'>";
4303 print "<div class=\"cdmHeader\">";
4305 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4307 print "<a class=\"title\"
4308 onclick=\"javascript:toggleUnread($id, 0)\"
4309 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4311 print $entry_author;
4313 if ($line["feed_title"]) {
4314 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4319 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4320 $line["content_preview"] = preg_replace("/href=/i",
4321 "target=\"_new\" href=", $line["content_preview"]);
4324 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4326 print "<div class=\"cdmFooter\"><span class='s0'>";
4328 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4330 print __("Select:").
4331 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4332 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4334 print "</span><span class='s1'>$marked_pic</span> ";
4335 print "<span class='s1'>$published_pic</span> ";
4337 $tags = get_article_tags($link, $id);
4340 $full_tags_str = "";
4343 foreach ($tags as $tag) {
4345 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4346 if ($num_tags < 5) {
4347 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4348 } else if ($num_tags == 5) {
4353 $tags_str = preg_replace("/, $/", "", $tags_str);
4354 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4356 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4358 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4361 if ($tags_str == "") $tags_str = "no tags";
4363 // print "<img src='images/tag.png' class='markedPic'>";
4365 print "<span class='s1'>
4366 <img class='tagsPic' src='images/tag.png' alt='Tags'
4367 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
4368 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4372 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4373 href=\"javascript:toggleUnread($id)\">
4384 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4388 // print_headline_subtoolbar($link,
4389 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4393 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
4401 return $topmost_article_ids;
4404 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
4406 function printTagCloud($link) {
4408 /* get first ref_id to count from */
4414 if (DB_TYPE == "pgsql") {
4415 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4416 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4417 AND date_entered > NOW() - INTERVAL '30 days'";
4419 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4420 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4421 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4424 $result = db_query($link, $query);
4425 $first_id = db_fetch_result($result, 0, "id"); */
4427 //AND post_int_id >= '$first_id'
4428 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4429 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
4430 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
4432 $result = db_query($link, $query);
4436 while ($line = db_fetch_assoc($result)) {
4437 $tags[$line["tag_name"]] = $line["count"];
4442 $max_size = 32; // max font size in pixels
4443 $min_size = 11; // min font size in pixels
4445 // largest and smallest array values
4446 $max_qty = max(array_values($tags));
4447 $min_qty = min(array_values($tags));
4449 // find the range of values
4450 $spread = $max_qty - $min_qty;
4451 if ($spread == 0) { // we don't want to divide by zero
4455 // set the font-size increment
4456 $step = ($max_size - $min_size) / ($spread);
4458 // loop through the tag array
4459 foreach ($tags as $key => $value) {
4460 // calculate font-size
4461 // find the $value in excess of $min_qty
4462 // multiply by the font-size increment ($size)
4463 // and add the $min_size set above
4464 $size = round($min_size +
(($value - $min_qty) * $step));
4466 $key_escaped = str_replace("'", "\\'", $key);
4468 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
4469 $size . "px\" title=\"$value articles tagged with " .
4470 $key . '">' . $key . '</a> ';
4474 function print_checkpoint($n, $s) {
4475 $ts = getmicrotime();
4476 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4480 function sanitize_tag($tag) {
4483 $tag = mb_strtolower($tag, 'utf-8');
4485 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4487 // $tag = str_replace('"', "", $tag);
4488 // $tag = str_replace("+", " ", $tag);
4489 $tag = str_replace("technorati tag: ", "", $tag);
4494 function generate_publish_key() {
4495 return sha1(uniqid(rand(), true));
4498 function article_publish_url($link) {
4500 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
4502 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");