3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
9 require_once 'config.php';
11 function get_translations() {
13 "auto" => "Detect automatically",
15 "fr_FR" => "Français",
17 "zh_CN" => "Simplified Chinese");
22 if (ENABLE_TRANSLATIONS
== true) {
23 require_once "accept-to-gettext.php";
24 require_once "gettext/gettext.inc";
26 function startup_gettext() {
28 # Get locale from Accept-Language header
29 $lang = al2gt(array_keys(get_translations()), "text/html");
31 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
32 $lang = _TRANSLATION_OVERRIDE_DEFAULT
;
35 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
36 $lang = $_COOKIE["ttrss_lang"];
40 _setlocale(LC_MESSAGES
, $lang);
41 _bindtextdomain("messages", "locale");
42 _textdomain("messages");
43 _bind_textdomain_codeset("messages", "UTF-8");
53 function startup_gettext() {
59 require_once 'db-prefs.php';
60 require_once 'compat.php';
61 require_once 'errors.php';
62 require_once 'version.php';
64 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
65 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
67 if (ENABLE_SIMPLEPIE
) {
68 require_once "simplepie/simplepie.inc";
70 require_once "magpierss/rss_fetch.inc";
71 require_once 'magpierss/rss_utils.inc';
74 include_once "tw/tw-config.php";
75 include_once "tw/tw.php";
76 include_once TW_SETUP
. "paranoya.php";
78 $tw_parser = new twParser();
80 function _debug($msg) {
81 $ts = strftime("%H:%M:%S", time());
85 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
89 if (DB_TYPE
== "pgsql") {
90 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
91 marked = false AND feed_id = '$feed_id' AND
92 (SELECT date_entered FROM ttrss_entries WHERE
93 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
95 $pg_version = get_pgsql_version($link);
97 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
99 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
100 ttrss_entries.id = ref_id AND
102 feed_id = '$feed_id' AND
103 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
107 $result = db_query($link, "DELETE FROM ttrss_user_entries
109 WHERE ttrss_entries.id = ref_id AND
111 feed_id = '$feed_id' AND
112 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
115 $rows = pg_affected_rows($result);
119 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
120 marked = false AND feed_id = '$feed_id' AND
121 (SELECT date_entered FROM ttrss_entries WHERE
122 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
124 $result = db_query($link, "DELETE FROM ttrss_user_entries
125 USING ttrss_user_entries, ttrss_entries
126 WHERE ttrss_entries.id = ref_id AND
128 feed_id = '$feed_id' AND
129 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
131 $rows = mysql_affected_rows($link);
136 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
140 function global_purge_old_posts($link, $do_output = false, $limit = false) {
142 $random_qpart = sql_random_function();
145 $limit_qpart = "LIMIT $limit";
150 $result = db_query($link,
151 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
152 ORDER BY $random_qpart $limit_qpart");
154 while ($line = db_fetch_assoc($result)) {
156 $feed_id = $line["id"];
157 $purge_interval = $line["purge_interval"];
158 $owner_uid = $line["owner_uid"];
160 if ($purge_interval == 0) {
162 $tmp_result = db_query($link,
163 "SELECT value FROM ttrss_user_prefs WHERE
164 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
166 if (db_num_rows($tmp_result) != 0) {
167 $purge_interval = db_fetch_result($tmp_result, 0, "value");
172 // print "Feed $feed_id: purge interval = $purge_interval\n";
175 if ($purge_interval > 0) {
176 purge_feed($link, $feed_id, $purge_interval, $do_output);
180 // purge orphaned posts in main content table
181 db_query($link, "DELETE FROM ttrss_entries WHERE
182 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
186 function purge_old_posts($link) {
188 $user_id = $_SESSION["uid"];
190 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
191 WHERE owner_uid = '$user_id'");
193 while ($line = db_fetch_assoc($result)) {
195 $feed_id = $line["id"];
196 $purge_interval = $line["purge_interval"];
198 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
200 if ($purge_interval > 0) {
201 purge_feed($link, $feed_id, $purge_interval);
205 // purge orphaned posts in main content table
206 db_query($link, "DELETE FROM ttrss_entries WHERE
207 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
210 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
212 if (WEB_DEMO_MODE
) return;
215 $user_id = $_SESSION["uid"];
216 purge_old_posts($link);
219 // db_query($link, "BEGIN");
221 if (MAX_UPDATE_TIME
> 0) {
222 if (DB_TYPE
== "mysql") {
225 $q_order = "RANDOM()";
228 $q_order = "last_updated DESC";
231 $result = db_query($link, "SELECT feed_url,id,
232 SUBSTRING(last_updated,1,19) AS last_updated,
233 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
238 while ($line = db_fetch_assoc($result)) {
239 $upd_intl = $line["update_interval"];
241 if (!$upd_intl ||
$upd_intl == 0) {
242 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
246 // Updates for this feed are disabled
250 if ($fetch ||
(!$line["last_updated"] ||
251 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
253 // print "<!-- feed: ".$line["feed_url"]." -->";
255 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
257 $upd_elapsed = time() - $upd_start;
259 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
265 // db_query($link, "COMMIT");
269 function fetch_file_contents($url) {
270 if (USE_CURL_FOR_ICONS
) {
271 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
273 $ch = curl_init($url);
274 $fp = fopen($tmpfile, "w");
277 curl_setopt($ch, CURLOPT_FILE
, $fp);
278 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
279 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
285 $contents = file_get_contents($tmpfile);
291 return file_get_contents($url);
296 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
297 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
299 function get_favicon_url($url) {
301 if ($html = @fetch_file_contents
($url)) {
303 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
304 // Attempt to grab a favicon link from their webpage url
305 $linkUrl = html_entity_decode($matches[1]);
307 if (substr($linkUrl, 0, 1) == '/') {
308 $urlParts = parse_url($url);
309 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
310 } else if (substr($linkUrl, 0, 7) == 'http://') {
311 $faviconURL = $linkUrl;
312 } else if (substr($url, -1, 1) == '/') {
313 $faviconURL = $url.$linkUrl;
315 $faviconURL = $url.'/'.$linkUrl;
319 // If unsuccessful, attempt to "guess" the favicon location
320 $urlParts = parse_url($url);
321 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
325 // Run a test to see if what we have attempted to get actually exists.
326 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
333 function url_validate($link) {
335 $url_parts = @parse_url
($link);
337 if ( empty( $url_parts["host"] ) )
340 if ( !empty( $url_parts["path"] ) ) {
341 $documentpath = $url_parts["path"];
346 if ( !empty( $url_parts["query"] ) )
347 $documentpath .= "?" . $url_parts["query"];
349 $host = $url_parts["host"];
350 $port = $url_parts["port"];
355 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
360 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
362 $http_response = fgets( $socket, 22 );
364 $responses = "/(200 OK)|(30[0-9] Moved)/";
365 if ( preg_match($responses, $http_response) ) {
374 function check_feed_favicon($site_url, $feed, $link) {
375 $favicon_url = get_favicon_url($site_url);
377 # print "FAVICON [$site_url]: $favicon_url\n";
381 $icon_file = ICONS_DIR
. "/$feed.ico";
383 if ($favicon_url && !file_exists($icon_file)) {
384 $contents = fetch_file_contents($favicon_url);
386 $fp = fopen($icon_file, "w");
389 fwrite($fp, $contents);
391 chmod($icon_file, 0644);
395 error_reporting(DEFAULT_ERROR_LEVEL
);
399 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
401 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
405 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
406 _debug("update_rss_feed: start");
409 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
410 FROM ttrss_feeds WHERE id = '$feed'");
412 $auth_login = db_fetch_result($result, 0, "auth_login");
413 $auth_pass = db_fetch_result($result, 0, "auth_pass");
415 $update_interval = db_fetch_result($result, 0, "update_interval");
417 if ($update_interval < 0) { return; }
419 $feed = db_escape_string($feed);
421 $fetch_url = $feed_url;
423 if ($auth_login && $auth_pass) {
424 $url_parts = array();
425 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
427 if ($url_parts[1] && $url_parts[2]) {
428 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
433 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
434 _debug("update_rss_feed: fetching [$fetch_url]...");
437 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
441 if (!ENABLE_SIMPLEPIE
) {
442 $rss = fetch_rss($fetch_url);
444 $rss = new SimplePie();
445 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
446 $rss->set_timeout(MAGPIE_FETCH_TIME_OUT
);
447 $rss->set_feed_url($fetch_url);
448 $rss->set_output_encoding('UTF-8');
454 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
455 _debug("update_rss_feed: fetch done, parsing...");
457 error_reporting (DEFAULT_ERROR_LEVEL
);
460 $feed = db_escape_string($feed);
462 if (ENABLE_SIMPLEPIE
) {
463 $fetch_ok = !$rss->error();
470 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
471 _debug("update_rss_feed: processing feed data...");
474 // db_query($link, "BEGIN");
476 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
477 FROM ttrss_feeds WHERE id = '$feed'");
479 $registered_title = db_fetch_result($result, 0, "title");
480 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
481 $orig_site_url = db_fetch_result($result, 0, "site_url");
483 $owner_uid = db_fetch_result($result, 0, "owner_uid");
485 if (ENABLE_SIMPLEPIE
) {
486 $site_url = $rss->get_link();
488 $site_url = $rss->channel
["link"];
491 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
492 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
493 _debug("update_rss_feed: checking favicon...");
496 check_feed_favicon($site_url, $feed, $link);
499 if (!$registered_title ||
$registered_title == "[Unknown]") {
501 if (ENABLE_SIMPLEPIE
) {
502 $feed_title = $rss->get_title();
504 $feed_title = db_escape_string($rss->channel
["title"]);
507 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
508 _debug("update_rss_feed: registering title: $feed_title");
511 db_query($link, "UPDATE ttrss_feeds SET
512 title = '$feed_title' WHERE id = '$feed'");
515 // weird, weird Magpie
516 if (!ENABLE_SIMPLEPIE
) {
517 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
520 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
521 db_query($link, "UPDATE ttrss_feeds SET
522 site_url = '$site_url' WHERE id = '$feed'");
525 // print "I: " . $rss->channel["image"]["url"];
527 if (!ENABLE_SIMPLEPIE
) {
528 $icon_url = $rss->image
["url"];
530 $icon_url = $rss->get_image_url();
533 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
534 $icon_url = db_escape_string($icon_url);
535 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
538 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
539 _debug("update_rss_feed: loading filters...");
544 $result = db_query($link, "SELECT reg_exp,
545 ttrss_filter_types.name AS name,
546 ttrss_filter_actions.name AS action,
549 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
551 owner_uid = $owner_uid AND
552 ttrss_filter_types.id = filter_type AND
553 ttrss_filter_actions.id = action_id AND
554 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
556 while ($line = db_fetch_assoc($result)) {
557 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
559 $filter["reg_exp"] = $line["reg_exp"];
560 $filter["action"] = $line["action"];
561 $filter["action_param"] = $line["action_param"];
562 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
564 array_push($filters[$line["name"]], $filter);
567 if (ENABLE_SIMPLEPIE
) {
568 $iterator = $rss->get_items();
570 $iterator = $rss->items
;
571 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
572 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
575 if (!is_array($iterator)) {
576 /* db_query($link, "UPDATE ttrss_feeds
577 SET last_error = 'Parse error: can\'t find any articles.'
578 WHERE id = '$feed'"); */
580 // clear any errors and mark feed as updated if fetched okay
581 // even if it's blank
583 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
584 _debug("update_rss_feed: entry iterator is not an array, no articles?");
587 db_query($link, "UPDATE ttrss_feeds
588 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
590 return; // no articles
593 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
594 _debug("update_rss_feed: processing articles...");
597 foreach ($iterator as $item) {
599 if (ENABLE_SIMPLEPIE
) {
600 $entry_guid = $item->get_id();
601 if (!$entry_guid) $entry_guid = $item->get_link();
602 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
606 $entry_guid = $item["id"];
608 if (!$entry_guid) $entry_guid = $item["guid"];
609 if (!$entry_guid) $entry_guid = $item["link"];
610 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
613 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
614 _debug("update_rss_feed: guid $entry_guid");
617 if (!$entry_guid) continue;
619 $entry_timestamp = "";
621 if (ENABLE_SIMPLEPIE
) {
622 $entry_timestamp = strtotime($item->get_date());
624 $rss_2_date = $item['pubdate'];
625 $rss_1_date = $item['dc']['date'];
626 $atom_date = $item['issued'];
627 if (!$atom_date) $atom_date = $item['updated'];
629 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
630 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
631 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
634 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
635 _debug("update_rss_feed: date $entry_timestamp");
638 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
639 $entry_timestamp = time();
640 $no_orig_date = 'true';
642 $no_orig_date = 'false';
645 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
647 if (ENABLE_SIMPLEPIE
) {
648 $entry_title = $item->get_title();
650 $entry_title = trim(strip_tags($item["title"]));
653 if (ENABLE_SIMPLEPIE
) {
654 $entry_link = $item->get_link();
656 // strange Magpie workaround
657 $entry_link = $item["link_"];
658 if (!$entry_link) $entry_link = $item["link"];
661 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
662 _debug("update_rss_feed: title $entry_title");
665 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
667 $entry_link = strip_tags($entry_link);
669 if (ENABLE_SIMPLEPIE
) {
670 $entry_content = $item->get_description();
672 $entry_content = $item["content:escaped"];
674 if (!$entry_content) $entry_content = $item["content:encoded"];
675 if (!$entry_content) $entry_content = $item["content"];
676 if (!$entry_content) $entry_content = $item["atom_content"];
677 if (!$entry_content) $entry_content = $item["summary"];
678 if (!$entry_content) $entry_content = $item["description"];
681 if (is_array($entry_content)) {
682 $entry_content = $entry_content["encoded"];
683 if (!$entry_content) $entry_content = $entry_content["escaped"];
688 // print_r(htmlspecialchars($entry_content));
691 $entry_content_unescaped = $entry_content;
693 if (ENABLE_SIMPLEPIE
) {
694 $entry_comments = strip_tags($item->data
["comments"]);
695 if ($item->get_author()) {
696 $entry_author = $item->get_author()->get_name();
699 $entry_comments = strip_tags($item["comments"]);
701 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
703 if ($item['author']) {
705 if (is_array($item['author'])) {
707 if (!$entry_author) {
708 $entry_author = db_escape_string(strip_tags($item['author']['name']));
711 if (!$entry_author) {
712 $entry_author = db_escape_string(strip_tags($item['author']['email']));
716 if (!$entry_author) {
717 $entry_author = db_escape_string(strip_tags($item['author']));
722 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
724 $entry_guid = db_escape_string(strip_tags($entry_guid));
726 $result = db_query($link, "SELECT id FROM ttrss_entries
727 WHERE guid = '$entry_guid'");
729 $entry_content = db_escape_string($entry_content);
731 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
733 $entry_title = db_escape_string($entry_title);
734 $entry_link = db_escape_string($entry_link);
735 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
736 $entry_author = mb_substr($entry_author, 0, 250);
738 if (ENABLE_SIMPLEPIE
) {
739 $num_comments = 0; #FIXME#
741 $num_comments = db_escape_string($item["slash"]["comments"]);
744 if (!$num_comments) $num_comments = 0;
746 // parse <category> entries into tags
748 if (ENABLE_SIMPLEPIE
) {
750 $additional_tags = array();
751 $additional_tags_src = $item->get_categories();
753 if (is_array($additional_tags_src)) {
754 foreach ($additional_tags_src as $tobj) {
755 array_push($additional_tags, $tobj->get_term());
759 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
760 _debug("update_rss_feed: category tags:");
761 print_r($additional_tags);
766 $t_ctr = $item['category#'];
768 $additional_tags = false;
771 $additional_tags = false;
772 } else if ($t_ctr == 1) {
773 $additional_tags = array($item['category']);
775 $additional_tags = array();
776 for ($i = 0; $i <= $t_ctr; $i++
) {
777 if ($item["category#$i"]) {
778 array_push($additional_tags, $item["category#$i"]);
783 // parse <dc:subject> elements
785 $t_ctr = $item['dc']['subject#'];
788 $additional_tags = array($item['dc']['subject']);
789 } else if ($t_ctr > 1) {
790 $additional_tags = array();
791 for ($i = 0; $i <= $t_ctr; $i++
) {
792 if ($item['dc']["subject#$i"]) {
793 array_push($additional_tags, $item['dc']["subject#$i"]);
801 // $entry_content = sanitize_rss($entry_content);
803 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
804 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
807 db_query($link, "BEGIN");
809 if (db_num_rows($result) == 0) {
811 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
812 _debug("update_rss_feed: base guid not found");
815 // base post entry does not exist, create it
817 $result = db_query($link,
818 "INSERT INTO ttrss_entries
834 '$entry_timestamp_fmt',
843 // we keep encountering the entry in feeds, so we need to
844 // update date_entered column so that we don't get horrible
845 // dupes when the entry gets purged and reinserted again e.g.
846 // in the case of SLOW SLOW OMG SLOW updating feeds
848 $base_entry_id = db_fetch_result($result, 0, "id");
850 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
851 WHERE id = '$base_entry_id'");
854 // now it should exist, if not - bad luck then
856 $result = db_query($link, "SELECT
857 id,content_hash,no_orig_date,title,
858 substring(date_entered,1,19) as date_entered,
859 substring(updated,1,19) as updated,
863 WHERE guid = '$entry_guid'");
865 if (db_num_rows($result) == 1) {
867 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
868 _debug("update_rss_feed: base guid found, checking for user record");
871 // this will be used below in update handler
872 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
873 $orig_title = db_fetch_result($result, 0, "title");
874 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
875 $orig_date_entered = strtotime(db_fetch_result($result,
878 $ref_id = db_fetch_result($result, 0, "id");
880 // check for user post link to main table
882 // do we allow duplicate posts with same GUID in different feeds?
883 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
884 $dupcheck_qpart = "AND feed_id = '$feed'";
886 $dupcheck_qpart = "";
889 // error_reporting(0);
891 $article_filters = get_article_filters($filters, $entry_title,
892 $entry_content, $entry_link);
894 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
895 _debug("update_rss_feed: article filters: ");
896 if (count($article_filters) != 0) {
897 print_r($article_filters);
901 if (find_article_filter($article_filters, "filter")) {
905 // error_reporting (DEFAULT_ERROR_LEVEL);
907 $result = db_query($link,
908 "SELECT ref_id FROM ttrss_user_entries WHERE
909 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
912 // okay it doesn't exist - create user entry
913 if (db_num_rows($result) == 0) {
915 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
916 _debug("update_rss_feed: user record not found, creating...");
919 if (!find_article_filter($article_filters, 'catchup')) {
921 $last_read_qpart = 'NULL';
924 $last_read_qpart = 'NOW()';
927 if (find_article_filter($article_filters, 'mark')) {
933 if (find_article_filter($article_filters, 'publish')) {
936 $published = 'false';
939 $result = db_query($link,
940 "INSERT INTO ttrss_user_entries
941 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
942 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
943 $last_read_qpart, $marked, $published)");
946 $post_needs_update = false;
948 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
949 ($content_hash != $orig_content_hash)) {
950 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
951 $post_needs_update = true;
954 if (db_escape_string($orig_title) != $entry_title) {
955 $post_needs_update = true;
958 if ($orig_num_comments != $num_comments) {
959 $post_needs_update = true;
962 // this doesn't seem to be very reliable
964 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
965 // $post_needs_update = true;
968 // if post needs update, update it and mark all user entries
969 // linking to this post as updated
970 if ($post_needs_update) {
972 if (defined('DAEMON_EXTENDED_DEBUG')) {
973 _debug("update_rss_feed: post $entry_guid needs update...");
976 // print "<!-- post $orig_title needs update : $post_needs_update -->";
978 db_query($link, "UPDATE ttrss_entries
979 SET title = '$entry_title', content = '$entry_content',
980 content_hash = '$content_hash',
981 num_comments = '$num_comments'
982 WHERE id = '$ref_id'");
984 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
985 db_query($link, "UPDATE ttrss_user_entries
986 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
988 db_query($link, "UPDATE ttrss_user_entries
989 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
995 db_query($link, "COMMIT");
997 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
998 _debug("update_rss_feed: looking for tags...");
1002 // <a href="..." rel="tag">Xorg</a>, //
1006 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
1007 $entry_content_unescaped, $entry_tags);
1009 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1010 print_r($entry_tags);
1011 print "<br/></p>"; */
1013 $entry_tags = $entry_tags[1];
1015 # check for manual tags
1017 $tag_filter = find_article_filter($article_filters, "tag");
1021 $manual_tags = trim_array(split(",", $tag_filter[1]));
1023 foreach ($manual_tags as $tag) {
1024 if (tag_is_valid($tag)) {
1025 array_push($entry_tags, $tag);
1030 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1031 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1033 if ($additional_tags && is_array($additional_tags)) {
1034 foreach ($additional_tags as $tag) {
1035 if (tag_is_valid($tag) &&
1036 array_search($tag, $boring_tags) === FALSE) {
1037 array_push($entry_tags, $tag);
1042 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1044 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1045 print_r($entry_tags);
1048 if (count($entry_tags) > 0) {
1050 db_query($link, "BEGIN");
1052 $result = db_query($link, "SELECT id,int_id
1053 FROM ttrss_entries,ttrss_user_entries
1054 WHERE guid = '$entry_guid'
1055 AND feed_id = '$feed' AND ref_id = id
1056 AND owner_uid = '$owner_uid'");
1058 if (db_num_rows($result) == 1) {
1060 $entry_id = db_fetch_result($result, 0, "id");
1061 $entry_int_id = db_fetch_result($result, 0, "int_id");
1063 foreach ($entry_tags as $tag) {
1065 $tag = sanitize_tag($tag);
1066 $tag = db_escape_string($tag);
1068 if (!tag_is_valid($tag)) continue;
1070 $result = db_query($link, "SELECT id FROM ttrss_tags
1071 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1072 owner_uid = '$owner_uid' LIMIT 1");
1074 // print db_fetch_result($result, 0, "id");
1076 if ($result && db_num_rows($result) == 0) {
1078 db_query($link, "INSERT INTO ttrss_tags
1079 (owner_uid,tag_name,post_int_id)
1080 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1084 db_query($link, "COMMIT");
1087 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1088 _debug("update_rss_feed: article processed");
1092 db_query($link, "UPDATE ttrss_feeds
1093 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1095 // db_query($link, "COMMIT");
1099 if (ENABLE_SIMPLEPIE
) {
1100 $error_msg = mb_substr($rss->error(), 0, 250);
1102 $error_msg = mb_substr(magpie_error(), 0, 250);
1105 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1106 _debug("update_rss_feed: error fetching feed: $error_msg");
1109 $error_msg = db_escape_string($error_msg);
1112 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1113 last_updated = NOW() WHERE id = '$feed'");
1116 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1117 _debug("update_rss_feed: done");
1122 function print_select($id, $default, $values, $attributes = "") {
1123 print "<select name=\"$id\" id=\"$id\" $attributes>";
1124 foreach ($values as $v) {
1130 print "<option$sel>$v</option>";
1135 function print_select_hash($id, $default, $values, $attributes = "") {
1136 print "<select name=\"$id\" id='$id' $attributes>";
1137 foreach (array_keys($values) as $v) {
1143 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1149 function get_article_filters($filters, $title, $content, $link) {
1152 if ($filters["title"]) {
1153 foreach ($filters["title"] as $filter) {
1154 $reg_exp = $filter["reg_exp"];
1155 $inverse = $filter["inverse"];
1156 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1157 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1159 array_push($matches, array($filter["action"], $filter["action_param"]));
1164 if ($filters["content"]) {
1165 foreach ($filters["content"] as $filter) {
1166 $reg_exp = $filter["reg_exp"];
1167 $inverse = $filter["inverse"];
1169 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1170 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1172 array_push($matches, array($filter["action"], $filter["action_param"]));
1177 if ($filters["both"]) {
1178 foreach ($filters["both"] as $filter) {
1179 $reg_exp = $filter["reg_exp"];
1180 $inverse = $filter["inverse"];
1183 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1184 array_push($matches, array($filter["action"], $filter["action_param"]));
1187 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1188 array_push($matches, array($filter["action"], $filter["action_param"]));
1194 if ($filters["link"]) {
1195 $reg_exp = $filter["reg_exp"];
1196 foreach ($filters["link"] as $filter) {
1197 $reg_exp = $filter["reg_exp"];
1198 $inverse = $filter["inverse"];
1200 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1201 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1203 array_push($matches, array($filter["action"], $filter["action_param"]));
1211 function find_article_filter($filters, $filter_name) {
1212 foreach ($filters as $f) {
1213 if ($f[0] == $filter_name) {
1220 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1221 $rtl_content = false, $last_updated = false, $last_error = false) {
1223 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1224 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1226 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1230 $rtl_tag = "dir=\"rtl\"";
1232 $rtl_tag = "dir=\"ltr\"";
1235 $error_notify_msg = "";
1238 $link_title = "Error: $last_error ($last_updated)";
1239 $error_notify_msg = "(Error)";
1240 } else if ($last_updated) {
1241 $link_title = "Updated: $last_updated";
1244 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1245 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1247 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1248 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1252 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1257 $fctr_class = "class=\"invisible\"";
1260 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1261 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1263 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1264 print "<div class=\"feedExtInfo\">
1265 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1272 function getmicrotime() {
1273 list($usec, $sec) = explode(" ",microtime());
1274 return ((float)$usec +
(float)$sec);
1277 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1278 foreach ($values as $v) {
1285 if ($v == $true_is) {
1286 $sel .= " value=\"1\"";
1288 $sel .= " value=\"0\"";
1291 print "<input class=\"noborder\"
1292 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1297 function initialize_user_prefs($link, $uid) {
1299 $uid = db_escape_string($uid);
1301 db_query($link, "BEGIN");
1303 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1305 $u_result = db_query($link, "SELECT pref_name
1306 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1308 $active_prefs = array();
1310 while ($line = db_fetch_assoc($u_result)) {
1311 array_push($active_prefs, $line["pref_name"]);
1314 while ($line = db_fetch_assoc($result)) {
1315 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1316 // print "adding " . $line["pref_name"] . "<br>";
1318 db_query($link, "INSERT INTO ttrss_user_prefs
1319 (owner_uid,pref_name,value) VALUES
1320 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1325 db_query($link, "COMMIT");
1329 function lookup_user_id($link, $user) {
1331 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1334 if (db_num_rows($result) == 1) {
1335 return db_fetch_result($result, 0, "id");
1341 function http_authenticate_user($link) {
1343 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1345 if (!$_SERVER["PHP_AUTH_USER"]) {
1347 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1348 header('HTTP/1.0 401 Unauthorized');
1352 $auth_result = authenticate_user($link,
1353 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1355 if (!$auth_result) {
1356 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1357 header('HTTP/1.0 401 Unauthorized');
1365 function authenticate_user($link, $login, $password, $force_auth = false) {
1367 if (!SINGLE_USER_MODE
) {
1369 $pwd_hash = 'SHA1:' . sha1($password);
1371 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1372 $query = "SELECT id,login,access_level
1373 FROM ttrss_users WHERE
1376 $query = "SELECT id,login,access_level
1377 FROM ttrss_users WHERE
1378 login = '$login' AND pwd_hash = '$pwd_hash'";
1381 $result = db_query($link, $query);
1383 if (db_num_rows($result) == 1) {
1384 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1385 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1386 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1388 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1391 $user_theme = get_user_theme_path($link);
1393 $_SESSION["theme"] = $user_theme;
1394 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1396 initialize_user_prefs($link, $_SESSION["uid"]);
1405 $_SESSION["uid"] = 1;
1406 $_SESSION["name"] = "admin";
1408 $user_theme = get_user_theme_path($link);
1410 $_SESSION["theme"] = $user_theme;
1411 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1413 initialize_user_prefs($link, $_SESSION["uid"]);
1419 function make_password($length = 8) {
1422 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1426 while ($i < $length) {
1427 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1429 if (!strstr($password, $char)) {
1437 // this is called after user is created to initialize default feeds, labels
1440 // user preferences are checked on every login, not here
1442 function initialize_user($link, $uid) {
1444 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1445 values ('$uid','unread = true', 'Unread articles')");
1447 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1448 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1450 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1451 values ('$uid', 'Tiny Tiny RSS: New Releases',
1452 'http://tt-rss.spb.ru/releases.rss')");
1454 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1455 values ('$uid', 'Tiny Tiny RSS: Forum',
1456 'http://tt-rss.spb.ru/forum/rss.php')");
1459 function logout_user() {
1461 if (isset($_COOKIE[session_name()])) {
1462 setcookie(session_name(), '', time()-42000, '/');
1466 function get_script_urlpath() {
1467 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1470 function validate_session($link) {
1471 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1472 if ($_SESSION["ip_address"]) {
1473 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1474 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1480 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1482 //print_r($_SESSION);
1484 if (time() > $_SESSION["cookie_lifetime"]) {
1492 function login_sequence($link, $mobile = false) {
1493 if (!SINGLE_USER_MODE
) {
1495 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1496 $swu = db_escape_string($_REQUEST["swu"]);
1498 $_SESSION["prefs_cache"] = false;
1499 return authenticate_user($link, $swu, null, true);
1503 $login_action = $_POST["login_action"];
1505 # try to authenticate user if called from login form
1506 if ($login_action == "do_login") {
1507 $login = $_POST["login"];
1508 $password = $_POST["password"];
1509 $remember_me = $_POST["remember_me"];
1511 if (authenticate_user($link, $login, $password)) {
1512 $_POST["password"] = "";
1514 $_SESSION["language"] = $_POST["language"];
1516 header("Location: " . $_SERVER["REQUEST_URI"]);
1521 $_SESSION["login_error_msg"] = "Incorrect username or password";
1525 // print session_id();
1526 // print_r($_SESSION);
1528 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1529 render_login_form($link, $mobile);
1532 /* bump login timestamp */
1533 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1536 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME
> 0) {
1537 setcookie("ttrss_lang", $_SESSION["language"],
1538 time() + SESSION_COOKIE_LIFETIME
);
1543 return authenticate_user($link, "admin", null);
1547 function truncate_string($str, $max_len) {
1548 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1549 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1555 function get_user_theme_path($link) {
1556 $result = db_query($link, "SELECT theme_path
1558 ttrss_themes,ttrss_users
1559 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1560 if (db_num_rows($result) != 0) {
1561 return db_fetch_result($result, 0, "theme_path");
1567 function smart_date_time($timestamp) {
1568 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1569 return date("G:i", $timestamp);
1570 } else if (date("Y", $timestamp) == date("Y")) {
1571 return date("M d, G:i", $timestamp);
1573 return date("Y/m/d, G:i", $timestamp);
1577 function smart_date($timestamp) {
1578 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1580 } else if (date("Y", $timestamp) == date("Y")) {
1581 return date("D m", $timestamp);
1583 return date("Y/m/d", $timestamp);
1587 function sql_bool_to_string($s) {
1588 if ($s == "t" ||
$s == "1") {
1595 function sql_bool_to_bool($s) {
1596 if ($s == "t" ||
$s == "1") {
1604 function toggleEvenOdd($a) {
1611 function sanity_check($link) {
1616 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1617 $schema_version = db_fetch_result($result, 0, "schema_version");
1619 if ($schema_version != SCHEMA_VERSION
) {
1623 if (DB_TYPE
== "mysql") {
1624 $result = db_query($link, "SELECT true", false);
1625 if (db_num_rows($result) != 1) {
1630 error_reporting (DEFAULT_ERROR_LEVEL
);
1632 if ($error_code != 0) {
1633 print_error_xml($error_code);
1640 function file_is_locked($filename) {
1642 $fp = fopen($filename, "r");
1643 error_reporting(DEFAULT_ERROR_LEVEL
);
1645 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1646 flock($fp, LOCK_UN
);
1656 function make_lockfile($filename) {
1657 $fp = fopen($filename, "w");
1659 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1666 function make_stampfile($filename) {
1667 $fp = fopen($filename, "w");
1669 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1670 fwrite($fp, time() . "\n");
1671 flock($fp, LOCK_UN
);
1679 function read_stampfile($filename) {
1682 $fp = fopen($filename, "r");
1683 error_reporting (DEFAULT_ERROR_LEVEL
);
1685 if (flock($fp, LOCK_EX
)) {
1686 $stamp = fgets($fp);
1687 flock($fp, LOCK_UN
);
1695 function sql_random_function() {
1696 if (DB_TYPE
== "mysql") {
1703 function catchup_feed($link, $feed, $cat_view) {
1705 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1710 $cat_qpart = "cat_id = '$feed'";
1712 $cat_qpart = "cat_id IS NULL";
1715 $tmp_result = db_query($link, "SELECT id
1716 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1719 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1721 $tmp_feed = $tmp_line["id"];
1723 db_query($link, "UPDATE ttrss_user_entries
1724 SET unread = false,last_read = NOW()
1725 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1728 } else if ($feed > 0) {
1730 $tmp_result = db_query($link, "SELECT id
1731 FROM ttrss_feeds WHERE parent_feed = '$feed'
1732 ORDER BY cat_id,title");
1734 $parent_ids = array();
1736 if (db_num_rows($tmp_result) > 0) {
1737 while ($p = db_fetch_assoc($tmp_result)) {
1738 array_push($parent_ids, "feed_id = " . $p["id"]);
1741 $children_qpart = implode(" OR ", $parent_ids);
1743 db_query($link, "UPDATE ttrss_user_entries
1744 SET unread = false,last_read = NOW()
1745 WHERE (feed_id = '$feed' OR $children_qpart)
1746 AND owner_uid = " . $_SESSION["uid"]);
1749 db_query($link, "UPDATE ttrss_user_entries
1750 SET unread = false,last_read = NOW()
1751 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1754 } else if ($feed < 0 && $feed > -10) { // special, like starred
1757 db_query($link, "UPDATE ttrss_user_entries
1758 SET unread = false,last_read = NOW()
1759 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1763 db_query($link, "UPDATE ttrss_user_entries
1764 SET unread = false,last_read = NOW()
1765 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1770 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1772 if (DB_TYPE
== "pgsql") {
1773 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1775 $match_part .= " AND date_entered > DATE_SUB(NOW(),
1776 INTERVAL $intl HOUR) ";
1779 db_query($link, "UPDATE ttrss_user_entries
1780 SET unread = false,last_read = NOW()
1781 WHERE $match_part AND owner_uid = ".$_SESSION["uid"]);
1784 } else if ($feed < -10) { // label
1786 // TODO make this more efficient
1788 $label_id = -$feed - 11;
1790 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1791 WHERE id = '$label_id'");
1794 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1796 db_query($link, "BEGIN");
1798 $tmp2_result = db_query($link,
1802 ttrss_user_entries,ttrss_entries,ttrss_feeds
1804 ref_id = ttrss_entries.id AND
1805 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1807 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1809 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1810 db_query($link, "UPDATE
1813 unread = false, last_read = NOW()
1815 int_id = " . $tmp_line["int_id"]);
1818 db_query($link, "COMMIT");
1820 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1821 SET unread = false,last_read = NOW()
1824 AND owner_uid = ".$_SESSION["uid"]); */
1828 db_query($link, "BEGIN");
1830 $tag_name = db_escape_string($feed);
1832 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1833 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1835 while ($line = db_fetch_assoc($result)) {
1836 db_query($link, "UPDATE ttrss_user_entries SET
1837 unread = false, last_read = NOW()
1838 WHERE int_id = " . $line["post_int_id"]);
1840 db_query($link, "COMMIT");
1844 function update_generic_feed($link, $feed, $cat_view) {
1848 $cat_qpart = "cat_id = '$feed'";
1850 $cat_qpart = "cat_id IS NULL";
1853 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1854 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1856 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1857 $feed_url = $tmp_line["feed_url"];
1858 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1862 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1863 WHERE id = '$feed'");
1864 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1865 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1869 function getAllCounters($link, $omode = "flc") {
1870 /* getLabelCounters($link);
1871 getFeedCounters($link);
1872 getTagCounters($link);
1873 getGlobalCounters($link);
1874 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1875 getCategoryCounters($link);
1878 if (!$omode) $omode = "flc";
1880 getGlobalCounters($link);
1882 if (strchr($omode, "l")) getLabelCounters($link);
1883 if (strchr($omode, "f")) getFeedCounters($link);
1884 if (strchr($omode, "t")) getTagCounters($link);
1885 if (strchr($omode, "c")) {
1886 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1887 getCategoryCounters($link);
1892 function getCategoryCounters($link) {
1893 # two special categories are -1 and -2 (all virtuals; all labels)
1895 $ctr = getCategoryUnread($link, -1);
1897 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
1899 $ctr = getCategoryUnread($link, -2);
1901 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
1903 $age_qpart = getMaxAgeSubquery();
1905 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1906 FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id
1907 AND id = ref_id AND $age_qpart
1908 AND unread = true)) AS unread FROM ttrss_feeds
1910 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1912 while ($line = db_fetch_assoc($result)) {
1913 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1914 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1915 $line["unread"]."\"/>";
1919 function getCategoryUnread($link, $cat) {
1924 $cat_query = "cat_id = '$cat'";
1926 $cat_query = "cat_id IS NULL";
1929 $age_qpart = getMaxAgeSubquery();
1931 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1933 AND owner_uid = " . $_SESSION["uid"]);
1935 $cat_feeds = array();
1936 while ($line = db_fetch_assoc($result)) {
1937 array_push($cat_feeds, "feed_id = " . $line["id"]);
1940 if (count($cat_feeds) == 0) return 0;
1942 $match_part = implode(" OR ", $cat_feeds);
1944 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1945 FROM ttrss_user_entries,ttrss_entries
1946 WHERE unread = true AND ($match_part) AND id = ref_id
1947 AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
1951 # this needs to be rewritten
1952 while ($line = db_fetch_assoc($result)) {
1953 $unread +
= $line["unread"];
1957 } else if ($cat == -1) {
1958 return getFeedUnread($link, -1) +
getFeedUnread($link, -2) +
getFeedUnread($link, -3);
1959 } else if ($cat == -2) {
1961 $rv = getLabelCounters($link, false, true);
1964 foreach (array_keys($rv) as $k) {
1966 $ctr +
= $rv[$k]["counter"];
1974 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE
) {
1975 if (DB_TYPE
== "pgsql") {
1976 return "ttrss_entries.date_entered >
1977 NOW() - INTERVAL '$days days'";
1979 return "ttrss_entries.date_entered >
1980 DATE_SUB(NOW(), INTERVAL $days DAY)";
1984 function getFeedUnread($link, $feed, $is_cat = false) {
1985 $n_feed = sprintf("%d", $feed);
1987 $age_qpart = getMaxAgeSubquery();
1990 return getCategoryUnread($link, $n_feed);
1991 } else if ($n_feed == -1) {
1992 $match_part = "marked = true";
1993 } else if ($n_feed == -2) {
1994 $match_part = "published = true";
1995 } else if ($n_feed == -3) {
1996 $match_part = "unread = true";
1998 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2000 if (DB_TYPE
== "pgsql") {
2001 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2003 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2006 } else if ($n_feed > 0) {
2008 $result = db_query($link, "SELECT id FROM ttrss_feeds
2009 WHERE parent_feed = '$n_feed'
2011 AND owner_uid = " . $_SESSION["uid"]);
2013 if (db_num_rows($result) > 0) {
2015 $linked_feeds = array();
2016 while ($line = db_fetch_assoc($result)) {
2017 array_push($linked_feeds, "feed_id = " . $line["id"]);
2020 array_push($linked_feeds, "feed_id = $n_feed");
2022 $match_part = implode(" OR ", $linked_feeds);
2024 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2025 FROM ttrss_user_entries,ttrss_entries
2026 WHERE unread = true AND
2027 ttrss_user_entries.ref_id = ttrss_entries.id AND
2030 owner_uid = " . $_SESSION["uid"]);
2034 # this needs to be rewritten
2035 while ($line = db_fetch_assoc($result)) {
2036 $unread +
= $line["unread"];
2042 $match_part = "feed_id = '$n_feed'";
2044 } else if ($feed < -10) {
2046 $label_id = -$feed - 11;
2048 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2049 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2051 $match_part = db_fetch_result($result, 0, "sql_exp");
2056 $result = db_query($link, "SELECT count(int_id) AS unread
2057 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2058 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2059 ttrss_user_entries.ref_id = ttrss_entries.id AND
2060 ttrss_feeds.hidden = false AND
2062 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2066 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2067 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
2068 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = id
2069 AND unread = true AND $age_qpart AND
2070 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2073 $unread = db_fetch_result($result, 0, "unread");
2078 /* FIXME this needs reworking */
2080 function getGlobalUnread($link, $user_id = false) {
2083 $user_id = $_SESSION["uid"];
2086 $age_qpart = getMaxAgeSubquery();
2088 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2089 WHERE unread = true AND
2090 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2091 ttrss_user_entries.ref_id = ttrss_entries.id AND
2094 ttrss_user_entries.owner_uid = '$user_id'");
2095 $c_id = db_fetch_result($result, 0, "c_id");
2099 function getGlobalCounters($link, $global_unread = -1) {
2100 if ($global_unread == -1) {
2101 $global_unread = getGlobalUnread($link);
2103 print "<counter type=\"global\" id='global-unread'
2104 counter='$global_unread'/>";
2106 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2107 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2109 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2111 print "<counter type=\"global\" id='subscribed-feeds'
2112 counter='$subscribed_feeds'/>";
2116 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2119 if (!$_SESSION["tctr_last_value"]) {
2120 $_SESSION["tctr_last_value"] = array();
2124 $old_counters = $_SESSION["tctr_last_value"];
2126 $tctrs_modified = false;
2128 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2129 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2130 ttrss_user_entries.ref_id = ttrss_entries.id AND
2131 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2132 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2134 select tag_name,0 as count FROM ttrss_tags
2135 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2137 $age_qpart = getMaxAgeSubquery();
2139 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2140 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2141 AND ref_id = id AND $age_qpart
2142 AND unread = true)) AS count FROM ttrss_tags
2143 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2144 ORDER BY count DESC LIMIT 55");
2148 while ($line = db_fetch_assoc($result)) {
2149 $tags[$line["tag_name"]] +
= $line["count"];
2152 foreach (array_keys($tags) as $tag) {
2153 $unread = $tags[$tag];
2155 $tag = htmlspecialchars($tag);
2157 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
2158 $old_counters[$tag] = $unread;
2159 $tctrs_modified = true;
2160 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2165 if ($smart_mode && $tctrs_modified) {
2166 $_SESSION["tctr_last_value"] = $old_counters;
2171 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
2173 $age_qpart = getMaxAgeSubquery();
2176 if (!$_SESSION["lctr_last_value"]) {
2177 $_SESSION["lctr_last_value"] = array();
2183 $old_counters = $_SESSION["lctr_last_value"];
2184 $lctrs_modified = false;
2186 $count = getFeedUnread($link, -1);
2189 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2191 $ret_arr["-1"]["counter"] = $count;
2192 $ret_arr["-1"]["description"] = __("Starred articles");
2195 $count = getFeedUnread($link, -2);
2198 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2200 $ret_arr["-2"]["counter"] = $count;
2201 $ret_arr["-2"]["description"] = __("Published articles");
2204 $count = getFeedUnread($link, -3);
2207 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2209 $ret_arr["-3"]["counter"] = $count;
2210 $ret_arr["-3"]["description"] = __("Fresh articles");
2214 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2215 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2217 while ($line = db_fetch_assoc($result)) {
2219 $id = -$line["id"] - 11;
2221 $label_name = $line["description"];
2223 error_reporting (0);
2225 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
2226 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
2227 ttrss_feeds.hidden = false AND
2229 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2230 ttrss_user_entries.ref_id = ttrss_entries.id AND
2231 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
2233 $count = db_fetch_result($tmp_result, 0, "count");
2235 if (!$smart_mode ||
$old_counters[$id] != $count) {
2236 $old_counters[$id] = $count;
2237 $lctrs_modified = true;
2239 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2241 $ret_arr[$id]["counter"] = $count;
2242 $ret_arr[$id]["description"] = $label_name;
2246 error_reporting (DEFAULT_ERROR_LEVEL
);
2249 if ($smart_mode && $lctrs_modified) {
2250 $_SESSION["lctr_last_value"] = $old_counters;
2256 /* function getFeedCounter($link, $id) {
2258 $result = db_query($link, "SELECT
2259 count(id) as count,last_error
2260 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2261 WHERE feed_id = '$id' AND unread = true
2262 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2263 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2265 $count = db_fetch_result($result, 0, "count");
2266 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2268 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2271 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2273 $age_qpart = getMaxAgeSubquery();
2276 if (!$_SESSION["fctr_last_value"]) {
2277 $_SESSION["fctr_last_value"] = array();
2281 $old_counters = $_SESSION["fctr_last_value"];
2283 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2284 SUBSTRING(last_updated,1,19) AS last_updated,
2286 FROM ttrss_entries,ttrss_user_entries
2287 WHERE feed_id = ttrss_feeds.id AND
2288 ttrss_user_entries.ref_id = ttrss_entries.id
2289 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2290 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2291 AND parent_feed IS NULL"); */
2293 $query = "SELECT ttrss_feeds.id,
2295 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2297 COUNT(ttrss_entries.id) AS count
2299 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2300 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2301 AND ttrss_user_entries.unread = true)
2302 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
2304 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2305 AND parent_feed IS NULL
2306 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
2308 $result = db_query($link, $query);
2309 $fctrs_modified = false;
2311 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2313 while ($line = db_fetch_assoc($result)) {
2316 $count = $line["count"];
2317 $last_error = htmlspecialchars($line["last_error"]);
2319 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2320 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2322 $last_updated = date($short_date, strtotime($line["last_updated"]));
2325 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2327 $tmp_result = db_query($link,
2328 "SELECT ttrss_feeds.id,COUNT(unread) AS unread
2329 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2330 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2331 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2332 WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
2334 if (db_num_rows($tmp_result) > 0) {
2335 while ($l = db_fetch_assoc($tmp_result)) {
2336 $count +
= $l["unread"];
2340 if (!$smart_mode ||
$old_counters[$id] != $count) {
2341 $old_counters[$id] = $count;
2342 $fctrs_modified = true;
2345 $error_part = "error=\"$last_error\"";
2351 $has_img_part = "hi=\"$has_img\"";
2356 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
2360 if ($smart_mode && $fctrs_modified) {
2361 $_SESSION["fctr_last_value"] = $old_counters;
2365 function get_script_dt_add() {
2366 if (strpos(VERSION
, ".99") === false) {
2373 function get_pgsql_version($link) {
2374 $result = db_query($link, "SELECT version() AS version");
2375 $version = split(" ", db_fetch_result($result, 0, "version"));
2379 function print_error_xml($code, $add_msg = "") {
2382 $error_msg = $ERRORS[$code];
2385 $error_msg = "$error_msg; $add_msg";
2388 print "<rpc-reply>";
2389 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2390 print "</rpc-reply>";
2393 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2394 $auth_login = '', $auth_pass = '') {
2396 # check for feed:http://url
2397 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2399 # check for feed://URL
2400 if (strpos($feed_link, "//") === 0) {
2401 $feed_link = "http:$feed_link";
2404 if ($feed_link == "") return;
2406 if ($cat_id == "0" ||
!$cat_id) {
2407 $cat_qpart = "NULL";
2409 $cat_qpart = "'$cat_id'";
2412 $result = db_query($link,
2413 "SELECT id FROM ttrss_feeds
2414 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2416 if (db_num_rows($result) == 0) {
2418 $result = db_query($link,
2419 "INSERT INTO ttrss_feeds
2420 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2421 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2422 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2424 $result = db_query($link,
2425 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2426 AND owner_uid = " . $_SESSION["uid"]);
2428 $feed_id = db_fetch_result($result, 0, "id");
2431 update_rss_feed($link, $feed_link, $feed_id, true);
2440 function print_feed_select($link, $id, $default_id = "",
2441 $attributes = "", $include_all_feeds = true) {
2443 print "<select id=\"$id\" name=\"$id\" $attributes>";
2444 if ($include_all_feeds) {
2445 print "<option value=\"0\">".__('All feeds')."</option>";
2448 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2449 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2451 if (db_num_rows($result) > 0 && $include_all_feeds) {
2452 print "<option disabled>--------</option>";
2455 while ($line = db_fetch_assoc($result)) {
2456 if ($line["id"] == $default_id) {
2457 $is_selected = "selected";
2461 printf("<option $is_selected value='%d'>%s</option>",
2462 $line["id"], htmlspecialchars($line["title"]));
2468 function print_feed_cat_select($link, $id, $default_id = "",
2469 $attributes = "", $include_all_cats = true) {
2471 print "<select id=\"$id\" name=\"$id\" $attributes>";
2473 if ($include_all_cats) {
2474 print "<option value=\"0\">".__('Uncategorized')."</option>";
2477 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2478 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2480 if (db_num_rows($result) > 0 && $include_all_cats) {
2481 print "<option disabled>--------</option>";
2484 while ($line = db_fetch_assoc($result)) {
2485 if ($line["id"] == $default_id) {
2486 $is_selected = "selected";
2490 printf("<option $is_selected value='%d'>%s</option>",
2491 $line["id"], htmlspecialchars($line["title"]));
2497 function checkbox_to_sql_bool($val) {
2498 return ($val == "on") ?
"true" : "false";
2501 function getFeedCatTitle($link, $id) {
2503 return __("Special");
2504 } else if ($id < -10) {
2505 return __("Labels");
2506 } else if ($id > 0) {
2507 $result = db_query($link, "SELECT ttrss_feed_categories.title
2508 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2509 cat_id = ttrss_feed_categories.id");
2510 if (db_num_rows($result) == 1) {
2511 return db_fetch_result($result, 0, "title");
2513 return __("Uncategorized");
2516 return "getFeedCatTitle($id) failed";
2521 function getFeedTitle($link, $id) {
2523 return __("Starred articles");
2524 } else if ($id == -2) {
2525 return __("Published articles");
2526 } else if ($id == -3) {
2527 return __("Fresh articles");
2528 } else if ($id < -10) {
2529 $label_id = -10 - $id;
2530 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2531 if (db_num_rows($result) == 1) {
2532 return db_fetch_result($result, 0, "description");
2534 return "Unknown label ($label_id)";
2537 } else if ($id > 0) {
2538 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2539 if (db_num_rows($result) == 1) {
2540 return db_fetch_result($result, 0, "title");
2542 return "Unknown feed ($id)";
2545 return "getFeedTitle($id) failed";
2550 function get_session_cookie_name() {
2551 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2554 function print_init_params($link) {
2555 print "<init-params>";
2556 if ($_SESSION["stored-params"]) {
2557 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2559 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2560 print "<param key=\"$key\" value=\"$value\"/>";
2565 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2566 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2567 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2569 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2570 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2572 print "<param key=\"hide_read_feeds\" value=\"" .
2573 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2575 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2576 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2578 print "<param key=\"confirm_feed_catchup\" value=\"" .
2579 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2581 print "<param key=\"cdm_auto_catchup\" value=\"" .
2582 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2584 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2586 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2588 print "<param key=\"default_view_mode\" value=\"" .
2589 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2591 print "<param key=\"default_view_limit\" value=\"" .
2592 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2594 print "<param key=\"prefs_active_tab\" value=\"" .
2595 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2597 print "<param key=\"infobox_disable_overlay\" value=\"" .
2598 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2600 print "<param key=\"icons_location\" value=\"" .
2603 print "</init-params>";
2606 function print_runtime_info($link) {
2607 print "<runtime-info>";
2608 if (ENABLE_UPDATE_DAEMON
) {
2609 print "<param key=\"daemon_is_running\" value=\"".
2610 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2612 if ($_SESSION["daemon_stamp_check"] +
600 < time()) {
2614 $stamp = (int)read_stampfile("update_daemon.stamp");
2617 if ($stamp +
86400*3 < time()) {
2618 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2620 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2623 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2625 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2628 $_SESSION["daemon_stamp_check"] = time();
2632 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2634 if ($_SESSION["last_version_check"] +
7200 < time()) {
2635 $new_version_details = check_for_update($link);
2637 print "<param key=\"new_version_available\" value=\"".
2638 sprintf("%d", $new_version_details != ""). "\"/>";
2640 $_SESSION["last_version_check"] = time();
2644 // print "<param key=\"new_version_available\" value=\"1\"/>";
2646 print "</runtime-info>";
2649 function getSearchSql($search, $match_on) {
2651 $search_query_part = "";
2653 $keywords = split(" ", $search);
2654 $query_keywords = array();
2656 if ($match_on == "both") {
2658 foreach ($keywords as $k) {
2659 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2660 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2663 $search_query_part = implode("AND", $query_keywords) . " AND ";
2665 } else if ($match_on == "title") {
2667 foreach ($keywords as $k) {
2668 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2671 $search_query_part = implode("AND", $query_keywords) . " AND ";
2673 } else if ($match_on == "content") {
2675 foreach ($keywords as $k) {
2676 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2680 $search_query_part = implode("AND", $query_keywords);
2682 return $search_query_part;
2685 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2687 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2691 $search_query_part = getSearchSql($search, $match_on);
2692 $search_query_part .= " AND ";
2695 $search_query_part = "";
2698 $view_query_part = "";
2700 if ($view_mode == "adaptive") {
2702 $view_query_part = " ";
2703 } else if ($feed != -1) {
2704 $unread = getFeedUnread($link, $feed, $cat_view);
2706 $view_query_part = " unread = true AND ";
2711 if ($view_mode == "marked") {
2712 $view_query_part = " marked = true AND ";
2715 if ($view_mode == "unread") {
2716 $view_query_part = " unread = true AND ";
2720 $limit_query_part = "LIMIT " . $limit;
2723 $vfeed_query_part = "";
2725 // override query strategy and enable feed display when searching globally
2726 if ($search && $search_mode == "all_feeds") {
2727 $query_strategy_part = "ttrss_entries.id > 0";
2728 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2729 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2730 $query_strategy_part = "ttrss_entries.id > 0";
2731 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2732 id = feed_id) as feed_title,";
2733 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2735 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2737 $tmp_result = false;
2740 $tmp_result = db_query($link, "SELECT id
2741 FROM ttrss_feeds WHERE cat_id = '$feed'");
2743 $tmp_result = db_query($link, "SELECT id
2744 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2745 WHERE id = '$feed') AND id != '$feed'");
2748 $cat_siblings = array();
2750 if (db_num_rows($tmp_result) > 0) {
2751 while ($p = db_fetch_assoc($tmp_result)) {
2752 array_push($cat_siblings, "feed_id = " . $p["id"]);
2755 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2756 $feed, implode(" OR ", $cat_siblings));
2759 $query_strategy_part = "ttrss_entries.id > 0";
2762 } else if ($feed >= 0) {
2767 $query_strategy_part = "cat_id = '$feed'";
2769 $query_strategy_part = "cat_id IS NULL";
2772 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2775 $tmp_result = db_query($link, "SELECT id
2776 FROM ttrss_feeds WHERE parent_feed = '$feed'
2777 ORDER BY cat_id,title");
2779 $parent_ids = array();
2781 if (db_num_rows($tmp_result) > 0) {
2782 while ($p = db_fetch_assoc($tmp_result)) {
2783 array_push($parent_ids, "feed_id = " . $p["id"]);
2786 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2787 $feed, implode(" OR ", $parent_ids));
2789 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2791 $query_strategy_part = "feed_id = '$feed'";
2794 } else if ($feed == -1) { // starred virtual feed
2795 $query_strategy_part = "marked = true";
2796 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2797 } else if ($feed == -2) { // published virtual feed
2798 $query_strategy_part = "published = true";
2799 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2800 } else if ($feed == -3) { // fresh virtual feed
2801 $query_strategy_part = "unread = true";
2803 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2805 if (DB_TYPE
== "pgsql") {
2806 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2808 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2811 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2812 } else if ($feed <= -10) { // labels
2813 $label_id = -$feed - 11;
2815 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2816 WHERE id = '$label_id'");
2818 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2820 if (!$query_strategy_part) {
2824 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2826 $query_strategy_part = "id > 0"; // dumb
2829 if (get_pref($link, 'REVERSE_HEADLINES')) {
2830 $order_by = "updated";
2832 $order_by = "updated DESC";
2835 if ($override_order) {
2836 $order_by = $override_order;
2841 if ($search && $search_mode == "all_feeds") {
2842 $feed_title = __("Search results")." ($search)";
2843 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2844 $feed_title = __("Search results")." ($search, $feed)";
2845 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2846 $feed_title = $feed;
2847 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2852 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2853 WHERE id = '$feed' AND owner_uid = $owner_uid");
2854 $feed_title = db_fetch_result($result, 0, "title");
2856 $feed_title = __("Uncategorized");
2860 $feed_title = __("Searched for")." $search ($feed_title)";
2865 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2866 WHERE id = '$feed' AND owner_uid = $owner_uid");
2868 $feed_title = db_fetch_result($result, 0, "title");
2869 $feed_site_url = db_fetch_result($result, 0, "site_url");
2870 $last_error = db_fetch_result($result, 0, "last_error");
2873 $feed_title = __("Searched for") . " $search ($feed_title)";
2877 } else if ($feed == -1) {
2878 $feed_title = __("Starred articles");
2879 } else if ($feed == -2) {
2880 $feed_title = __("Published articles");
2881 } else if ($feed == -3) {
2882 $feed_title = __("Fresh articles");
2883 } else if ($feed < -10) {
2884 $label_id = -$feed - 11;
2885 $result = db_query($link, "SELECT description FROM ttrss_labels
2886 WHERE id = '$label_id'");
2887 $feed_title = db_fetch_result($result, 0, "description");
2890 $feed_title = __("Searched for") . " $search ($feed_title)";
2896 if ($feed < -10) error_reporting (0);
2898 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2901 $feed_kind = "Feeds";
2903 $feed_kind = "Labels";
2906 $content_query_part = "content as content_preview,";
2908 if ($limit_query_part) {
2909 $offset_query_part = "OFFSET $offset";
2914 ttrss_entries.id,ttrss_entries.title,
2916 unread,feed_id,marked,published,link,last_read,
2917 SUBSTRING(last_read,1,19) as last_read_noms,
2920 SUBSTRING(updated,1,19) as updated_noms,
2923 ttrss_entries,ttrss_user_entries,ttrss_feeds
2925 ttrss_feeds.hidden = false AND
2926 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2927 ttrss_user_entries.ref_id = ttrss_entries.id AND
2928 ttrss_user_entries.owner_uid = '$owner_uid' AND
2931 $query_strategy_part ORDER BY $order_by
2932 $limit_query_part $offset_query_part";
2934 $result = db_query($link, $query);
2936 if ($_GET["debug"]) print $query;
2941 $feed_kind = "Tags";
2943 $result = db_query($link, "SELECT
2945 ttrss_entries.id as id,title,
2948 marked,link,last_read,
2949 SUBSTRING(last_read,1,19) as last_read_noms,
2952 SUBSTRING(updated,1,19) as updated_noms
2954 ttrss_entries,ttrss_user_entries,ttrss_tags
2956 ref_id = ttrss_entries.id AND
2957 ttrss_user_entries.owner_uid = '$owner_uid' AND
2958 post_int_id = int_id AND tag_name = '$feed' AND
2961 $query_strategy_part ORDER BY $order_by
2962 $limit_query_part");
2965 return array($result, $feed_title, $feed_site_url, $last_error);
2969 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
2970 $search, $search_mode, $match_on) {
2972 $qfh_ret = queryFeedHeadlines($link, $feed,
2973 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
2976 $result = $qfh_ret[0];
2977 $feed_title = htmlspecialchars($qfh_ret[1]);
2978 $feed_site_url = $qfh_ret[2];
2979 $last_error = $qfh_ret[3];
2981 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
2983 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
2984 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
2985 <rss version=\"2.0\">
2987 <title>$feed_title</title>
2988 <link>$feed_site_url</link>
2989 <description>Feed generated by Tiny Tiny RSS</description>";
2991 while ($line = db_fetch_assoc($result)) {
2993 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
2994 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2996 $tags = get_article_tags($link, $line["id"], $owner_uid);
2998 foreach ($tags as $tag) {
2999 print "<category>" . htmlspecialchars($tag) . "</category>";
3002 $rfc822_date = date('r', strtotime($line["updated"]));
3004 print "<pubDate>$rfc822_date</pubDate>";
3007 htmlspecialchars($line["title"]) . "</title>";
3009 print "<description><![CDATA[" .
3010 $line["content_preview"] . "]]></description>";
3015 print "</channel></rss>";
3019 function getCategoryTitle($link, $cat_id) {
3021 if ($cat_id == -1) {
3022 return __("Special");
3023 } else if ($cat_id == -2) {
3024 return __("Labels");
3027 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3030 if (db_num_rows($result) == 1) {
3031 return db_fetch_result($result, 0, "title");
3033 return "Uncategorized";
3038 // http://ru2.php.net/strip-tags
3040 function strip_tags_long($textstring, $allowed){
3041 while($textstring != strip_tags($textstring, $allowed))
3043 while (strlen($textstring) != 0)
3045 if (strlen($textstring) > 1024) {
3048 $otherlen = strlen($textstring);
3050 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3051 $safetext .= $temptext;
3052 $textstring = substr_replace($textstring,'',0,$otherlen);
3054 $textstring = $safetext;
3060 function sanitize_rss($link, $str, $force_strip_tags = false) {
3063 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
3065 global $tw_paranoya_setup;
3067 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
3069 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
3070 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3076 function send_headlines_digests($link, $limit = 100) {
3078 if (!DIGEST_ENABLE
) return false;
3080 $user_limit = DIGEST_EMAIL_LIMIT
;
3083 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3085 if (DB_TYPE
== "pgsql") {
3086 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3087 } else if (DB_TYPE
== "mysql") {
3088 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3091 $result = db_query($link, "SELECT id,email FROM ttrss_users
3092 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3094 while ($line = db_fetch_assoc($result)) {
3095 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3096 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3098 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3099 $digest = $tuple[0];
3100 $headlines_count = $tuple[1];
3102 if ($headlines_count > 0) {
3103 $rc = mail($line["login"] . " <" . $line["email"] . ">",
3104 "[tt-rss] New headlines for last 24 hours", $digest,
3105 "From: " . MAIL_FROM
. "\n".
3106 "Content-Type: text/plain; charset=\"utf-8\"\n".
3107 "Content-Transfer-Encoding: 8bit\n");
3109 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3110 WHERE id = " . $line["id"]);
3112 print "No headlines\n";
3117 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
3121 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3122 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
3123 $tmp .= "=======================================================\n\n";
3125 if (DB_TYPE
== "pgsql") {
3126 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3127 } else if (DB_TYPE
== "mysql") {
3128 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3131 $result = db_query($link, "SELECT ttrss_entries.title,
3132 ttrss_feeds.title AS feed_title,
3135 SUBSTRING(last_updated,1,19) AS last_updated
3137 ttrss_user_entries,ttrss_entries,ttrss_feeds
3139 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3140 AND include_in_digest = true
3142 AND ttrss_user_entries.owner_uid = $user_id
3143 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
3146 $cur_feed_title = "";
3148 $headlines_count = db_num_rows($result);
3150 while ($line = db_fetch_assoc($result)) {
3151 $updated = smart_date_time(strtotime($line["last_updated"]));
3152 $feed_title = $line["feed_title"];
3154 if ($cur_feed_title != $feed_title) {
3155 $cur_feed_title = $feed_title;
3157 $tmp .= "$feed_title\n\n";
3160 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
3161 $tmp .= " " . trim($line["link"]) . "\n";
3166 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
3167 DIGEST_HOSTNAME
. "\n".
3168 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
3171 return array($tmp, $headlines_count);
3174 function check_for_update($link, $brief_fmt = true) {
3175 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3177 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
3182 if (ENABLE_SIMPLEPIE
) {
3183 $rss = new SimplePie();
3184 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
3185 $rss->set_timeout(MAGPIE_FETCH_TIME_OUT
);
3186 $rss->set_feed_url($fetch_url);
3187 $rss->set_output_encoding('UTF-8');
3190 $rss = fetch_rss($releases_feed);
3192 error_reporting (DEFAULT_ERROR_LEVEL
);
3196 if (ENABLE_SIMPLEPIE
) {
3197 $items = $rss->get_items();
3199 $items = $rss->items
;
3201 if (!$items ||
!is_array($items)) $items = $rss->entries
;
3202 if (!$items ||
!is_array($items)) $items = $rss;
3205 if (!is_array($items) ||
count($items) == 0) {
3209 $latest_item = $items[0];
3211 if (ENABLE_SIMPLEPIE
) {
3212 $last_title = $latest_item->get_title();
3214 $last_title = $latest_item["title"];
3217 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3219 if (ENABLE_SIMPLEPIE
) {
3220 $release_url = sanitize_rss($link, $latest_item->get_link());
3221 $content = sanitize_rss($link, $latest_item->get_description());
3223 $release_url = sanitize_rss($link, $latest_item["link"]);
3224 $content = sanitize_rss($link, $latest_item["description"]);
3227 if (version_compare(VERSION
, $latest_version) == -1) {
3229 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3230 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3231 <div id=\"milestoneDetails\">$content</div>");
3233 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3234 <div class='milestoneDetails'>$content</div>
3235 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3236 download and update information.";
3243 function markArticlesById($link, $ids, $cmode) {
3247 foreach ($ids as $id) {
3248 array_push($tmp_ids, "ref_id = '$id'");
3251 $ids_qpart = join(" OR ", $tmp_ids);
3254 db_query($link, "UPDATE ttrss_user_entries SET
3255 marked = false,last_read = NOW()
3256 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3257 } else if ($cmode == 1) {
3258 db_query($link, "UPDATE ttrss_user_entries SET
3260 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3262 db_query($link, "UPDATE ttrss_user_entries SET
3263 marked = NOT marked,last_read = NOW()
3264 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3268 function publishArticlesById($link, $ids, $cmode) {
3272 foreach ($ids as $id) {
3273 array_push($tmp_ids, "ref_id = '$id'");
3276 $ids_qpart = join(" OR ", $tmp_ids);
3279 db_query($link, "UPDATE ttrss_user_entries SET
3280 published = false,last_read = NOW()
3281 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3282 } else if ($cmode == 1) {
3283 db_query($link, "UPDATE ttrss_user_entries SET
3285 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3287 db_query($link, "UPDATE ttrss_user_entries SET
3288 published = NOT published,last_read = NOW()
3289 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3293 function catchupArticlesById($link, $ids, $cmode) {
3297 foreach ($ids as $id) {
3298 array_push($tmp_ids, "ref_id = '$id'");
3301 $ids_qpart = join(" OR ", $tmp_ids);
3304 db_query($link, "UPDATE ttrss_user_entries SET
3305 unread = false,last_read = NOW()
3306 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3307 } else if ($cmode == 1) {
3308 db_query($link, "UPDATE ttrss_user_entries SET
3310 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3312 db_query($link, "UPDATE ttrss_user_entries SET
3313 unread = NOT unread,last_read = NOW()
3314 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3318 function catchupArticleById($link, $id, $cmode) {
3321 db_query($link, "UPDATE ttrss_user_entries SET
3322 unread = false,last_read = NOW()
3323 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3324 } else if ($cmode == 1) {
3325 db_query($link, "UPDATE ttrss_user_entries SET
3327 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3329 db_query($link, "UPDATE ttrss_user_entries SET
3330 unread = NOT unread,last_read = NOW()
3331 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3335 function make_guid_from_title($title) {
3336 return preg_replace("/[ \"\',.:;]/", "-",
3337 mb_strtolower(strip_tags($title), 'utf-8'));
3340 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3341 $bottom = false, $rtl_content = false, $feed_id = 0,
3342 $is_cat = false, $search = false, $match_on = false,
3343 $search_mode = false, $offset = 0, $limit = 0) {
3345 $user_page_offset = $offset +
1;
3348 $class = "headlinesSubToolbar";
3349 $tid = "headlineActionsTop";
3351 $class = "headlinesSubToolbar";
3352 $tid = "headlineActionsBottom";
3355 print "<table class=\"$class\" id=\"$tid\"
3356 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3364 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3365 $page_next_link = "javascript:viewFeedGoPage(1)";
3366 $page_first_link = "javascript:viewFeedGoPage(0)";
3368 $catchup_page_link = "javascript:catchupPage()";
3369 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3370 $catchup_sel_link = "javascript:catchupSelection()";
3372 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3374 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3375 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3376 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3378 $tog_unread_link = "javascript:selectionToggleUnread()";
3379 $tog_marked_link = "javascript:selectionToggleMarked()";
3380 $tog_published_link = "javascript:selectionTogglePublished()";
3384 $sel_all_link = "javascript:cdmSelectArticles('all')";
3385 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3386 $sel_none_link = "javascript:cdmSelectArticles('none')";
3388 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3389 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3390 $tog_published_link = "javascript:selectionTogglePublished(true)";
3394 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3396 print "<td class=\"headlineActions$rtl_cpart\">
3397 <ul class=\"headlineDropdownMenu\">
3400 <a href=\"$sel_all_link\">".__('All')."</a>,
3401 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3402 <a href=\"$sel_none_link\">".__('None')."</a></li>
3403 <li class=\"vsep\"> </li>
3404 <li class=\"top\">".__('Toggle')."<ul>
3405 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3406 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3407 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3409 <li class=\"vsep\"> </li>
3410 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
3411 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
3412 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3413 <li><span class=\"insensitive\">--------</span></li>
3414 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3415 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3416 <li><span class=\"insensitive\">--------</span></li>
3417 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3420 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3422 if ($limit != 0 && !$search && $enable_pagination) {
3424 <li class=\"vsep\"> </li>
3425 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3426 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3427 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3431 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3433 <li class=\"vsep\"> </li>
3435 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3436 '$match_on', '$feed_id', '$is_cat');\">
3437 ".__('Convert to label')."</a></td>";
3443 // old style subtoolbar:
3445 print "<td class=\"headlineActions$rtl_cpart\">".
3447 <a href=\"$sel_all_link\">".__('All')."</a>,
3448 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3449 <a href=\"$sel_none_link\">".__('None')."</a>
3451 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3452 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3454 __('Mark as read:')."
3455 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3456 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3458 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3461 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3462 '$match_on', '$feed_id', '$is_cat');\">
3463 ".__('Convert to label')."</a>";
3470 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3471 print "<td class=\"headlineActions$rtl_cpart\">
3472 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3473 '$match_on', '$feed_id', '$is_cat');\">
3474 ".__('Convert to Label')."</a></td>";
3477 print "<td class=\"headlineTitle$rtl_cpart\">";
3479 if ($feed_site_url) {
3481 $target = "target=\"_new\"";
3483 print "<a $target href=\"$feed_site_url\">".
3484 truncate_string($feed_title,30)."</a>";
3490 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3493 if ($user_page_offset > 1) {
3494 print " [$user_page_offset] ";
3500 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3501 <img class=\"noborder\"
3502 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3507 print "</tr></table>";
3511 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3513 $tmp_category = getCategoryTitle($link, $cat_id);
3514 $cat_unread = getCategoryUnread($link, $cat_id);
3517 $holder_style = "display:none;";
3524 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3526 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3527 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3530 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3532 print "<span id=\"FCAP-$cat_id\">";
3535 print " <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3536 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3546 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3549 function outputFeedList($link, $tags = false) {
3551 print "<ul class=\"feedList\" id=\"feedList\">";
3553 $owner_uid = $_SESSION["uid"];
3557 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3559 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
3562 $cat_hidden = false;
3565 # print "<li class=\"feedCat\">".__('Special')."</li>";
3566 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3567 # print "<li class=\"feedCat\">".
3568 # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3569 # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3571 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3572 # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3574 # $cat_unread = getCategoryUnread($link, -1);
3575 # $tmp_category = __("Special");
3576 # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3578 printCategoryHeader($link, -1, $cat_hidden, false);
3581 $num_starred = getFeedUnread($link, -1);
3582 $num_published = getFeedUnread($link, -2);
3583 $num_fresh = getFeedUnread($link, -3);
3587 if ($num_fresh > 0) $class .= "Unread";
3589 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3590 "images/fresh.png", $link);
3594 if ($num_starred > 0) $class .= "Unread";
3596 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
3599 $mark_img_ext = "gif";
3601 $mark_img_ext = "png";
3604 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3605 "images/mark_set.$mark_img_ext", $link);
3609 if ($num_published > 0) $class .= "Unread";
3611 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3612 "images/pub_set.gif", $link);
3614 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3620 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3622 $result = db_query($link, "SELECT id,sql_exp,description FROM
3623 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3625 if (db_num_rows($result) > 0) {
3626 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3628 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
3631 $cat_hidden = false;
3634 printCategoryHeader($link, -2, $cat_hidden, false);
3636 # print "<li class=\"feedCat\">".
3637 # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3638 # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3640 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
3642 print "<li><hr></li>";
3646 while ($line = db_fetch_assoc($result)) {
3648 error_reporting (0);
3650 $label_id = -$line['id'] - 11;
3651 $count = getFeedUnread($link, $label_id);
3659 error_reporting (DEFAULT_ERROR_LEVEL
);
3661 printFeedEntry($label_id,
3662 $class, $line["description"],
3663 $count, "images/label.png", $link);
3667 if (db_num_rows($result) > 0) {
3668 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3675 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3676 print "<li><hr></li>";
3679 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3680 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3681 $order_by_qpart = "category,unread DESC,title";
3683 $order_by_qpart = "category,title";
3686 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3687 $order_by_qpart = "unread DESC,title";
3689 $order_by_qpart = "title";
3693 $age_qpart = getMaxAgeSubquery();
3695 $result = db_query($link, "SELECT ttrss_feeds.*,
3696 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3697 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3698 WHERE feed_id = ttrss_feeds.id AND unread = true
3700 AND ttrss_user_entries.ref_id = ttrss_entries.id
3701 AND owner_uid = '$owner_uid') as unread,
3703 ttrss_feed_categories.title AS category,
3704 ttrss_feed_categories.collapsed
3705 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3706 ON (ttrss_feed_categories.id = cat_id)
3708 ttrss_feeds.hidden = false AND
3709 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3710 ORDER BY $order_by_qpart");
3712 $actid = $_GET["actid"];
3722 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3724 while ($line = db_fetch_assoc($result)) {
3726 $feed = trim($line["title"]);
3728 if (!$feed) $feed = "[Untitled]";
3730 $feed_id = $line["id"];
3732 $subop = $_GET["subop"];
3734 $unread = $line["unread"];
3736 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3737 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3739 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3742 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3745 $rtl_tag = "dir=\"RTL\"";
3750 $tmp_result = db_query($link,
3751 "SELECT id,COUNT(unread) AS unread
3752 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3753 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3754 WHERE parent_feed = '$feed_id' AND unread = true
3755 GROUP BY ttrss_feeds.id");
3757 if (db_num_rows($tmp_result) > 0) {
3758 while ($l = db_fetch_assoc($tmp_result)) {
3759 $unread +
= $l["unread"];
3763 $cat_id = $line["cat_id"];
3765 $tmp_category = $line["category"];
3767 if (!$tmp_category) {
3768 $tmp_category = __("Uncategorized");
3771 // $class = ($lnum % 2) ? "even" : "odd";
3773 if ($line["last_error"]) {
3779 if ($unread > 0) $class .= "Unread";
3781 if ($actid == $feed_id) {
3782 $class .= "Selected";
3785 $total_unread +
= $unread;
3787 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3793 $category = $tmp_category;
3795 $collapsed = $line["collapsed"];
3797 // workaround for NULL category
3798 if ($category == __("Uncategorized")) {
3799 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3804 if ($collapsed == "t" ||
$collapsed == "1") {
3805 $holder_class = "feedCatHolder";
3806 $holder_style = "display:none;";
3809 $holder_class = "feedCatHolder";
3814 $cat_id = sprintf("%d", $cat_id);
3816 $cat_unread = getCategoryUnread($link, $cat_id);
3818 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3820 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3821 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3822 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3823 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3824 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3827 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3830 printFeedEntry($feed_id, $class, $feed, $unread,
3831 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
3832 $last_updated, $line["last_error"]);
3837 if (db_num_rows($result) == 0) {
3838 print "<li>".__('No feeds to display.')."</li>";
3845 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3846 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3847 post_int_id = ttrss_user_entries.int_id AND
3848 unread = true AND ref_id = ttrss_entries.id
3849 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3851 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3852 ORDER BY tag_name"); */
3854 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3855 print "<li class=\"feedCat\">".__('Tags')."</li>";
3856 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3859 $age_qpart = getMaxAgeSubquery();
3861 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3862 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
3863 AND ref_id = id AND $age_qpart
3864 AND unread = true)) AS count FROM ttrss_tags
3865 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
3866 ORDER BY count DESC LIMIT 50");
3870 while ($line = db_fetch_assoc($result)) {
3871 $tags[$line["tag_name"]] +
= $line["count"];
3874 foreach (array_keys($tags) as $tag) {
3876 $unread = $tags[$tag];
3884 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3888 if (db_num_rows($result) == 0) {
3889 print "<li>No tags to display.</li>";
3892 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3902 function get_article_tags($link, $id, $owner_uid = 0) {
3904 $a_id = db_escape_string($id);
3906 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3908 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3909 owner_uid as owner FROM
3910 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3911 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
3915 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3916 array_push($tags, $tmp_line["tag_name"]);
3922 function trim_value(&$value) {
3923 $value = trim($value);
3926 function trim_array($array) {
3928 array_walk($tmp, 'trim_value');
3932 function tag_is_valid($tag) {
3933 if ($tag == '') return false;
3934 if (preg_match("/^[0-9]*$/", $tag)) return false;
3936 $tag = iconv("utf-8", "utf-8", $tag);
3937 if (!$tag) return false;
3942 function render_login_form($link, $mobile = false) {
3944 require_once "login_form.php";
3946 require_once "mobile/login_form.php";
3950 // from http://developer.apple.com/internet/safari/faq.html
3951 function no_cache_incantation() {
3952 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3953 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3954 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3955 header("Cache-Control: post-check=0, pre-check=0", false);
3956 header("Pragma: no-cache"); // HTTP/1.0
3959 function format_warning($msg, $id = "") {
3960 return "<div class=\"warning\" id=\"$id\">
3961 <img src=\"images/sign_excl.gif\">$msg</div>";
3964 function format_notice($msg) {
3965 return "<div class=\"notice\">
3966 <img src=\"images/sign_info.gif\">$msg</div>";
3969 function format_error($msg) {
3970 return "<div class=\"error\">
3971 <img src=\"images/sign_excl.gif\">$msg</div>";
3974 function print_notice($msg) {
3975 return print format_notice($msg);
3978 function print_warning($msg) {
3979 return print format_warning($msg);
3982 function print_error($msg) {
3983 return print format_error($msg);
3987 function T_sprintf() {
3988 $args = func_get_args();
3989 return vsprintf(__(array_shift($args)), $args);
3992 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3994 /* we can figure out feed_id from article id anyway, why do we
3995 * pass feed_id here? */
3997 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3998 WHERE ref_id = '$id'");
4000 $feed_id = db_fetch_result($result, 0, "feed_id");
4002 print "<article id='$id'><![CDATA[";
4004 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4005 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4007 if (db_num_rows($result) == 1) {
4008 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4010 $rtl_content = false;
4014 $rtl_tag = "dir=\"RTL\"";
4021 if ($mark_as_read) {
4022 $result = db_query($link, "UPDATE ttrss_user_entries
4023 SET unread = false,last_read = NOW()
4024 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4027 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
4028 SUBSTRING(updated,1,16) as updated,
4029 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4032 FROM ttrss_entries,ttrss_user_entries
4033 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4039 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4040 $link_target = "target=\"_new\"";
4043 $line = db_fetch_assoc($result);
4045 if ($line["icon_url"]) {
4046 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4048 $feed_icon = " ";
4051 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
4052 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4054 $entry_comments = "";
4057 $num_comments = $line["num_comments"];
4058 $entry_comments = "";
4060 if ($num_comments > 0) {
4061 if ($line["comments"]) {
4062 $comments_url = $line["comments"];
4064 $comments_url = $line["link"];
4066 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4068 if ($line["comments"] && $line["link"] != $line["comments"]) {
4069 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4073 print "<div class=\"postReply\">";
4075 print "<div class=\"postHeader\">";
4077 $entry_author = $line["author"];
4079 if ($entry_author) {
4080 $entry_author = __(" - by ") . $entry_author;
4083 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4084 strtotime($line["updated"]));
4086 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4088 if ($line["link"]) {
4089 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
4090 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
4092 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4095 /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
4096 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
4097 ORDER BY tag_name"); */
4099 $tags = get_article_tags($link, $id);
4106 foreach ($tags as $tag) {
4108 $tag_escaped = str_replace("'", "\\'", $tag);
4110 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4112 if ($num_tags == 6) {
4115 } else if ($num_tags < 6) {
4116 $tags_str .= $tag_str;
4118 $f_tags_str .= $tag_str;
4121 $tags_str = preg_replace("/, $/", "", $tags_str);
4122 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4124 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4125 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4127 if (!$entry_comments) $entry_comments = " "; # placeholder
4129 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4131 print "<div style='float : right'>
4132 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4134 <a title=\"Edit tags for this article\"
4135 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4136 <div clear='both'>$entry_comments</div>";
4140 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4141 print "<div class=\"postContent\">";
4143 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4145 $line["content"] = sanitize_rss($link, $line["content"]);
4147 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4148 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4151 print $line["content"] . "</div>";
4157 print "]]></article>";
4161 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4162 $next_unread_feed, $offset) {
4164 $timing_info = getmicrotime();
4166 $topmost_article_ids = array();
4172 if ($subop == "undefined") $subop = "";
4174 if ($subop == "CatchupSelected") {
4175 $ids = split(",", db_escape_string($_GET["ids"]));
4176 $cmode = sprintf("%d", $_GET["cmode"]);
4178 catchupArticlesById($link, $ids, $cmode);
4181 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4182 update_generic_feed($link, $feed, $cat_view);
4185 if ($subop == "MarkAllRead") {
4186 catchup_feed($link, $feed, $cat_view);
4188 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4189 if ($next_unread_feed) {
4190 $feed = $next_unread_feed;
4196 $result = db_query($link,
4197 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4199 if (db_num_rows($result) == 0) {
4200 print "<div align='center'>".__('Feed not found.')."</div>";
4205 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4207 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4208 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4210 if (db_num_rows($result) == 1) {
4211 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4213 $rtl_content = false;
4217 $rtl_tag = "dir=\"RTL\"";
4223 $rtl_content = false;
4226 $script_dt_add = get_script_dt_add();
4228 /// START /////////////////////////////////////////////////////////////////////////////////
4230 $search = db_escape_string($_GET["query"]);
4231 $search_mode = db_escape_string($_GET["search_mode"]);
4232 $match_on = db_escape_string($_GET["match_on"]);
4238 $real_offset = $offset * $limit;
4240 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4242 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4243 $search, $search_mode, $match_on, false, $real_offset);
4245 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4247 $result = $qfh_ret[0];
4248 $feed_title = $qfh_ret[1];
4249 $feed_site_url = $qfh_ret[2];
4250 $last_error = $qfh_ret[3];
4253 $feed_site_url = article_publish_url($link);
4256 /// STOP //////////////////////////////////////////////////////////////////////////////////
4259 print "<div id=\"headlinesContainer\" $rtl_tag>";
4262 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4266 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4267 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4270 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4273 $headlines_count = db_num_rows($result);
4275 if (db_num_rows($result) > 0) {
4277 # print "\{$offset}";
4279 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4280 print "<table class=\"headlinesList\" id=\"headlinesList\"
4281 cellspacing=\"0\">";
4284 $lnum = $limit*$offset;
4286 error_reporting (DEFAULT_ERROR_LEVEL
);
4290 while ($line = db_fetch_assoc($result)) {
4292 $class = ($lnum %
2) ?
"even" : "odd";
4295 $feed_id = $line["feed_id"];
4297 if (count($topmost_article_ids) < 5) {
4298 array_push($topmost_article_ids, $id);
4301 if ($line["last_read"] == "" &&
4302 ($line["unread"] != "t" && $line["unread"] != "1")) {
4304 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4307 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4311 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
4319 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4322 $mark_img_ext = "gif";
4324 $mark_img_ext = "png";
4327 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
4328 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
4330 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
4332 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
4334 alt=\"Star article\" onclick='javascript:tMark($id)'>";
4337 if ($line["published"] == "t" ||
$line["published"] == "1") {
4338 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
4340 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
4342 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
4344 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
4347 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4348 # $line["title"] . "</a>";
4350 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4351 $line["title"] . "</a>";
4353 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4354 # $line["title"] . "</a>";
4356 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4357 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
4359 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4360 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
4363 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4364 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4368 $entry_author = $line["author"];
4370 if ($entry_author) {
4371 $entry_author = " - by $entry_author";
4374 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4376 print "<tr class='$class' id='RROW-$id'>";
4378 print "<td class='hlUpdPic'>$update_pic</td>";
4380 print "<td class='hlSelectRow'>
4381 <input type=\"checkbox\" onclick=\"tSR(this)\"
4385 print "<td class='hlMarkedPic'>$marked_pic</td>";
4386 print "<td class='hlMarkedPic'>$published_pic</td>";
4388 # if ($line["feed_title"]) {
4389 # print "<td class='hlContent'>$content_link</td>";
4390 # print "<td class='hlFeed'>
4391 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4392 # truncate_string($line["feed_title"],30)."</a> </td>";
4395 print "<td class='hlContent' valign='middle'>";
4397 print "<a href=\"javascript:view($id,$feed_id);\">" .
4400 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4401 if ($content_preview) {
4402 print "<span class=\"contentPreview\"> - $content_preview</span>";
4408 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4409 # $line["feed_title"]."</a>
4411 if ($line["feed_title"]) {
4412 print "<span class=\"hlFeed\">
4413 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
4414 $line["feed_title"]."</a>)
4423 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
4430 $add_class = "Unread";
4435 print "<div class=\"cdmArticle$add_class\"
4436 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4437 onmouseout='cdmMouseOut(this)'>";
4439 print "<div class=\"cdmHeader\">";
4441 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4443 print "<a class=\"title\"
4444 onclick=\"javascript:toggleUnread($id, 0)\"
4445 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4447 print $entry_author;
4449 if ($line["feed_title"]) {
4450 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4455 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4456 $line["content_preview"] = preg_replace("/href=/i",
4457 "target=\"_new\" href=", $line["content_preview"]);
4460 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4462 print "<div class=\"cdmFooter\"><span class='s0'>";
4464 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4466 print __("Select:").
4467 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4468 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4470 print "</span><span class='s1'>$marked_pic</span> ";
4471 print "<span class='s1'>$published_pic</span> ";
4473 $tags = get_article_tags($link, $id);
4476 $full_tags_str = "";
4479 foreach ($tags as $tag) {
4481 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4482 if ($num_tags < 5) {
4483 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4484 } else if ($num_tags == 5) {
4489 $tags_str = preg_replace("/, $/", "", $tags_str);
4490 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4492 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4494 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4497 if ($tags_str == "") $tags_str = "no tags";
4499 // print "<img src='images/tag.png' class='markedPic'>";
4501 print "<span class='s1'>
4502 <img class='tagsPic' src='images/tag.png' alt='Tags'
4503 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
4504 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4508 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4509 href=\"javascript:toggleUnread($id)\">
4520 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4524 // print_headline_subtoolbar($link,
4525 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4529 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
4537 return array($topmost_article_ids, $headlines_count);
4540 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
4542 function printTagCloud($link) {
4544 /* get first ref_id to count from */
4550 if (DB_TYPE == "pgsql") {
4551 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4552 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4553 AND date_entered > NOW() - INTERVAL '30 days'";
4555 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4556 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4557 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4560 $result = db_query($link, $query);
4561 $first_id = db_fetch_result($result, 0, "id"); */
4563 //AND post_int_id >= '$first_id'
4564 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4565 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
4566 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
4568 $result = db_query($link, $query);
4572 while ($line = db_fetch_assoc($result)) {
4573 $tags[$line["tag_name"]] = $line["count"];
4578 $max_size = 32; // max font size in pixels
4579 $min_size = 11; // min font size in pixels
4581 // largest and smallest array values
4582 $max_qty = max(array_values($tags));
4583 $min_qty = min(array_values($tags));
4585 // find the range of values
4586 $spread = $max_qty - $min_qty;
4587 if ($spread == 0) { // we don't want to divide by zero
4591 // set the font-size increment
4592 $step = ($max_size - $min_size) / ($spread);
4594 // loop through the tag array
4595 foreach ($tags as $key => $value) {
4596 // calculate font-size
4597 // find the $value in excess of $min_qty
4598 // multiply by the font-size increment ($size)
4599 // and add the $min_size set above
4600 $size = round($min_size +
(($value - $min_qty) * $step));
4602 $key_escaped = str_replace("'", "\\'", $key);
4604 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
4605 $size . "px\" title=\"$value articles tagged with " .
4606 $key . '">' . $key . '</a> ';
4610 function print_checkpoint($n, $s) {
4611 $ts = getmicrotime();
4612 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4616 function sanitize_tag($tag) {
4619 $tag = mb_strtolower($tag, 'utf-8');
4621 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4623 // $tag = str_replace('"', "", $tag);
4624 // $tag = str_replace("+", " ", $tag);
4625 $tag = str_replace("technorati tag: ", "", $tag);
4630 function generate_publish_key() {
4631 return sha1(uniqid(rand(), true));
4634 function article_publish_url($link) {
4636 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
4638 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");