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",
16 "nb_NO" => "Norsk Bokmål",
18 "pt_BR" => "Portuguese/Brazil",
19 "zh_CN" => "Simplified Chinese");
24 if (ENABLE_TRANSLATIONS
== true) {
25 require_once "accept-to-gettext.php";
26 require_once "gettext/gettext.inc";
28 function startup_gettext() {
30 # Get locale from Accept-Language header
31 $lang = al2gt(array_keys(get_translations()), "text/html");
33 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
34 $lang = _TRANSLATION_OVERRIDE_DEFAULT
;
37 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
38 $lang = $_COOKIE["ttrss_lang"];
42 _setlocale(LC_MESSAGES
, $lang);
43 _bindtextdomain("messages", "locale");
44 _textdomain("messages");
45 _bind_textdomain_codeset("messages", "UTF-8");
55 function startup_gettext() {
61 require_once 'db-prefs.php';
62 require_once 'compat.php';
63 require_once 'errors.php';
64 require_once 'version.php';
66 require_once 'phpmailer/class.phpmailer.php';
68 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
69 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
71 require_once "simplepie/simplepie.inc";
72 require_once "magpierss/rss_fetch.inc";
73 require_once 'magpierss/rss_utils.inc';
75 function _debug($msg) {
76 $ts = strftime("%H:%M:%S", time());
77 $ts = "$ts/" . posix_getpid();
81 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
83 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
87 $result = db_query($link,
88 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
92 if (db_num_rows($result) == 1) {
93 $owner_uid = db_fetch_result($result, 0, "owner_uid");
96 if (!$owner_uid) return;
98 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
101 if (!$purge_unread) $query_limit = " unread = false AND ";
103 if (DB_TYPE
== "pgsql") {
104 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
105 marked = false AND feed_id = '$feed_id' AND
106 (SELECT date_entered FROM ttrss_entries WHERE
107 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
109 $pg_version = get_pgsql_version($link);
111 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
113 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
114 ttrss_entries.id = ref_id AND
116 feed_id = '$feed_id' AND
118 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
122 $result = db_query($link, "DELETE FROM ttrss_user_entries
124 WHERE ttrss_entries.id = ref_id AND
126 feed_id = '$feed_id' AND
128 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
131 $rows = pg_affected_rows($result);
135 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
136 marked = false AND feed_id = '$feed_id' AND
137 (SELECT date_entered FROM ttrss_entries WHERE
138 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
140 $result = db_query($link, "DELETE FROM ttrss_user_entries
141 USING ttrss_user_entries, ttrss_entries
142 WHERE ttrss_entries.id = ref_id AND
144 feed_id = '$feed_id' AND
146 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
148 $rows = mysql_affected_rows($link);
153 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
157 function global_purge_old_posts($link, $do_output = false, $limit = false) {
159 $random_qpart = sql_random_function();
162 $limit_qpart = "LIMIT $limit";
167 $result = db_query($link,
168 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
169 ORDER BY $random_qpart $limit_qpart");
171 while ($line = db_fetch_assoc($result)) {
173 $feed_id = $line["id"];
174 $purge_interval = $line["purge_interval"];
175 $owner_uid = $line["owner_uid"];
177 if ($purge_interval == 0) {
179 $tmp_result = db_query($link,
180 "SELECT value FROM ttrss_user_prefs WHERE
181 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
183 if (db_num_rows($tmp_result) != 0) {
184 $purge_interval = db_fetch_result($tmp_result, 0, "value");
189 // print "Feed $feed_id: purge interval = $purge_interval\n";
192 if ($purge_interval > 0) {
193 purge_feed($link, $feed_id, $purge_interval, $do_output);
197 // purge orphaned posts in main content table
198 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
199 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
202 $rows = db_affected_rows($link, $result);
203 _debug("Purged $rows orphaned posts.");
208 function feed_purge_interval($link, $feed_id) {
210 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
211 WHERE id = '$feed_id'");
213 if (db_num_rows($result) == 1) {
214 $purge_interval = db_fetch_result($result, 0, "purge_interval");
215 $owner_uid = db_fetch_result($result, 0, "owner_uid");
217 if ($purge_interval == 0) $purge_interval = get_pref($link,
218 'PURGE_OLD_DAYS', $user_id);
220 return $purge_interval;
227 function purge_old_posts($link) {
229 $user_id = $_SESSION["uid"];
231 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
232 WHERE owner_uid = '$user_id'");
234 while ($line = db_fetch_assoc($result)) {
236 $feed_id = $line["id"];
237 $purge_interval = $line["purge_interval"];
239 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
241 if ($purge_interval > 0) {
242 purge_feed($link, $feed_id, $purge_interval);
246 // purge orphaned posts in main content table
247 db_query($link, "DELETE FROM ttrss_entries WHERE
248 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
251 function get_feed_update_interval($link, $feed_id) {
252 $result = db_query($link, "SELECT owner_uid, update_interval FROM
253 ttrss_feeds WHERE id = '$feed_id'");
255 if (db_num_rows($result) == 1) {
256 $update_interval = db_fetch_result($result, 0, "update_interval");
257 $owner_uid = db_fetch_result($result, 0, "owner_uid");
259 if ($update_interval != 0) {
260 return $update_interval;
262 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
270 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
272 if (WEB_DEMO_MODE
) return;
275 $user_id = $_SESSION["uid"];
276 purge_old_posts($link);
279 // db_query($link, "BEGIN");
281 if (MAX_UPDATE_TIME
> 0) {
282 if (DB_TYPE
== "mysql") {
285 $q_order = "RANDOM()";
288 $q_order = "last_updated DESC";
291 $result = db_query($link, "SELECT feed_url,id,
292 SUBSTRING(last_updated,1,19) AS last_updated,
293 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
298 while ($line = db_fetch_assoc($result)) {
299 $upd_intl = $line["update_interval"];
301 if (!$upd_intl ||
$upd_intl == 0) {
302 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
306 // Updates for this feed are disabled
310 if ($fetch ||
(!$line["last_updated"] ||
311 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
313 // print "<!-- feed: ".$line["feed_url"]." -->";
315 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
317 $upd_elapsed = time() - $upd_start;
319 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
325 // db_query($link, "COMMIT");
329 function fetch_file_contents($url) {
330 if (USE_CURL_FOR_ICONS
) {
331 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
333 $ch = curl_init($url);
334 $fp = fopen($tmpfile, "w");
337 curl_setopt($ch, CURLOPT_FILE
, $fp);
338 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
339 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
345 $contents = file_get_contents($tmpfile);
351 return file_get_contents($url);
356 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
357 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
359 function get_favicon_url($url) {
361 if ($html = @fetch_file_contents
($url)) {
363 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
364 // Attempt to grab a favicon link from their webpage url
365 $linkUrl = html_entity_decode($matches[1]);
367 if (substr($linkUrl, 0, 1) == '/') {
368 $urlParts = parse_url($url);
369 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
370 } else if (substr($linkUrl, 0, 7) == 'http://') {
371 $faviconURL = $linkUrl;
372 } else if (substr($url, -1, 1) == '/') {
373 $faviconURL = $url.$linkUrl;
375 $faviconURL = $url.'/'.$linkUrl;
379 // If unsuccessful, attempt to "guess" the favicon location
380 $urlParts = parse_url($url);
381 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
385 // Run a test to see if what we have attempted to get actually exists.
386 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
393 function url_validate($link) {
395 $url_parts = @parse_url
($link);
397 if ( empty( $url_parts["host"] ) )
400 if ( !empty( $url_parts["path"] ) ) {
401 $documentpath = $url_parts["path"];
406 if ( !empty( $url_parts["query"] ) )
407 $documentpath .= "?" . $url_parts["query"];
409 $host = $url_parts["host"];
410 $port = $url_parts["port"];
415 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
420 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
422 $http_response = fgets( $socket, 22 );
424 $responses = "/(200 OK)|(30[0-9] Moved)/";
425 if ( preg_match($responses, $http_response) ) {
434 function check_feed_favicon($site_url, $feed, $link) {
435 $favicon_url = get_favicon_url($site_url);
437 # print "FAVICON [$site_url]: $favicon_url\n";
441 $icon_file = ICONS_DIR
. "/$feed.ico";
443 if ($favicon_url && !file_exists($icon_file)) {
444 $contents = fetch_file_contents($favicon_url);
446 $fp = fopen($icon_file, "w");
449 fwrite($fp, $contents);
451 chmod($icon_file, 0644);
455 error_reporting(DEFAULT_ERROR_LEVEL
);
459 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
461 if (!$_GET["daemon"] && !$ignore_daemon) {
465 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
466 _debug("update_rss_feed: start");
469 if (!$ignore_daemon) {
471 if (DB_TYPE
== "pgsql") {
472 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
474 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
477 $result = db_query($link, "SELECT id,update_interval,auth_login,
478 auth_pass,cache_images,update_method
479 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
483 $result = db_query($link, "SELECT id,update_interval,auth_login,
484 auth_pass,cache_images,update_method
485 FROM ttrss_feeds WHERE id = '$feed'");
489 if (db_num_rows($result) == 0) {
490 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
491 _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
496 $update_method = db_fetch_result($result, 0, "update_method");
498 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
499 WHERE id = '$feed'");
501 $auth_login = db_fetch_result($result, 0, "auth_login");
502 $auth_pass = db_fetch_result($result, 0, "auth_pass");
504 if (ALLOW_SELECT_UPDATE_METHOD
) {
505 if (ENABLE_SIMPLEPIE
) {
506 $use_simplepie = $update_method != 1;
508 $use_simplepie = $update_method == 2;
511 $use_simplepie = ENABLE_SIMPLEPIE
;
514 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
515 _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n");
518 if (!$use_simplepie) {
519 $auth_login = urlencode($auth_login);
520 $auth_pass = urlencode($auth_pass);
523 $update_interval = db_fetch_result($result, 0, "update_interval");
524 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
526 if ($update_interval < 0) { return; }
528 $feed = db_escape_string($feed);
530 $fetch_url = $feed_url;
532 if ($auth_login && $auth_pass) {
533 $url_parts = array();
534 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
536 if ($url_parts[1] && $url_parts[2]) {
537 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
542 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
543 _debug("update_rss_feed: fetching [$fetch_url]...");
546 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
550 if (!$use_simplepie) {
551 $rss = fetch_rss($fetch_url);
553 if (!is_dir(SIMPLEPIE_CACHE_DIR
)) {
554 mkdir(SIMPLEPIE_CACHE_DIR
);
557 $rss = new SimplePie();
558 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
559 # $rss->set_timeout(10);
560 $rss->set_feed_url($fetch_url);
561 $rss->set_output_encoding('UTF-8');
563 if (SIMPLEPIE_CACHE_IMAGES
&& $cache_images) {
564 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
565 _debug("enabling image cache");
568 $rss->set_image_handler('./image.php', 'i');
571 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
572 _debug("feed update interval (sec): " .
573 get_feed_update_interval($link, $feed)*60);
576 if (is_dir(SIMPLEPIE_CACHE_DIR
)) {
577 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR
);
578 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
586 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
587 _debug("update_rss_feed: fetch done, parsing...");
589 error_reporting (DEFAULT_ERROR_LEVEL
);
592 $feed = db_escape_string($feed);
594 if ($use_simplepie) {
595 $fetch_ok = !$rss->error();
602 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
603 _debug("update_rss_feed: processing feed data...");
606 // db_query($link, "BEGIN");
608 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
609 FROM ttrss_feeds WHERE id = '$feed'");
611 $registered_title = db_fetch_result($result, 0, "title");
612 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
613 $orig_site_url = db_fetch_result($result, 0, "site_url");
615 $owner_uid = db_fetch_result($result, 0, "owner_uid");
617 if ($use_simplepie) {
618 $site_url = $rss->get_link();
620 $site_url = $rss->channel
["link"];
623 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
624 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
625 _debug("update_rss_feed: checking favicon...");
628 check_feed_favicon($site_url, $feed, $link);
631 if (!$registered_title ||
$registered_title == "[Unknown]") {
633 if ($use_simplepie) {
634 $feed_title = db_escape_string($rss->get_title());
636 $feed_title = db_escape_string($rss->channel
["title"]);
639 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
640 _debug("update_rss_feed: registering title: $feed_title");
643 db_query($link, "UPDATE ttrss_feeds SET
644 title = '$feed_title' WHERE id = '$feed'");
647 // weird, weird Magpie
648 if (!$use_simplepie) {
649 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
652 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
653 db_query($link, "UPDATE ttrss_feeds SET
654 site_url = '$site_url' WHERE id = '$feed'");
657 // print "I: " . $rss->channel["image"]["url"];
659 if (!$use_simplepie) {
660 $icon_url = $rss->image
["url"];
662 $icon_url = $rss->get_image_url();
665 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
666 $icon_url = db_escape_string($icon_url);
667 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
670 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
671 _debug("update_rss_feed: loading filters...");
676 $result = db_query($link, "SELECT reg_exp,
677 ttrss_filter_types.name AS name,
678 ttrss_filter_actions.name AS action,
681 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
683 owner_uid = $owner_uid AND
684 ttrss_filter_types.id = filter_type AND
685 ttrss_filter_actions.id = action_id AND
686 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
688 while ($line = db_fetch_assoc($result)) {
689 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
691 $filter["reg_exp"] = $line["reg_exp"];
692 $filter["action"] = $line["action"];
693 $filter["action_param"] = $line["action_param"];
694 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
696 array_push($filters[$line["name"]], $filter);
699 if ($use_simplepie) {
700 $iterator = $rss->get_items();
702 $iterator = $rss->items
;
703 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
704 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
707 if (!is_array($iterator)) {
708 /* db_query($link, "UPDATE ttrss_feeds
709 SET last_error = 'Parse error: can\'t find any articles.'
710 WHERE id = '$feed'"); */
712 // clear any errors and mark feed as updated if fetched okay
713 // even if it's blank
715 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
716 _debug("update_rss_feed: entry iterator is not an array, no articles?");
719 db_query($link, "UPDATE ttrss_feeds
720 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
722 return; // no articles
725 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
726 _debug("update_rss_feed: processing articles...");
729 foreach ($iterator as $item) {
731 if ($_GET['xdebug']) {
736 if ($use_simplepie) {
737 $entry_guid = $item->get_id();
738 if (!$entry_guid) $entry_guid = $item->get_link();
739 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
743 $entry_guid = $item["id"];
745 if (!$entry_guid) $entry_guid = $item["guid"];
746 if (!$entry_guid) $entry_guid = $item["link"];
747 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
750 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
751 _debug("update_rss_feed: guid $entry_guid");
754 if (!$entry_guid) continue;
756 $entry_timestamp = "";
758 if ($use_simplepie) {
759 $entry_timestamp = strtotime($item->get_date());
761 $rss_2_date = $item['pubdate'];
762 $rss_1_date = $item['dc']['date'];
763 $atom_date = $item['issued'];
764 if (!$atom_date) $atom_date = $item['updated'];
766 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
767 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
768 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
771 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
772 _debug("update_rss_feed: date $entry_timestamp");
775 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
776 $entry_timestamp = time();
777 $no_orig_date = 'true';
779 $no_orig_date = 'false';
782 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
784 if ($use_simplepie) {
785 $entry_title = $item->get_title();
787 $entry_title = trim(strip_tags($item["title"]));
790 if ($use_simplepie) {
791 $entry_link = $item->get_link();
793 // strange Magpie workaround
794 $entry_link = $item["link_"];
795 if (!$entry_link) $entry_link = $item["link"];
798 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
799 _debug("update_rss_feed: title $entry_title");
802 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
804 $entry_link = strip_tags($entry_link);
806 if ($use_simplepie) {
807 $entry_content = $item->get_description();
809 $entry_content = $item["content:escaped"];
811 if (!$entry_content) $entry_content = $item["content:encoded"];
812 if (!$entry_content) $entry_content = $item["content"];
814 // Magpie bugs are getting ridiculous
815 if (trim($entry_content) == "Array") $entry_content = false;
817 if (!$entry_content) $entry_content = $item["atom_content"];
818 if (!$entry_content) $entry_content = $item["summary"];
819 if (!$entry_content) $entry_content = $item["description"];
822 if (is_array($entry_content)) {
823 $entry_content = $entry_content["encoded"];
824 if (!$entry_content) $entry_content = $entry_content["escaped"];
828 if ($_GET["xdebug"]) {
829 print "update_rss_feed: content: ";
830 print_r(htmlspecialchars($entry_content));
833 $entry_content_unescaped = $entry_content;
835 if ($use_simplepie) {
836 $entry_comments = strip_tags($item->data
["comments"]);
837 if ($item->get_author()) {
838 $entry_author_item = $item->get_author();
839 $entry_author = $entry_author_item->get_name();
842 $entry_comments = strip_tags($item["comments"]);
844 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
846 if ($item['author']) {
848 if (is_array($item['author'])) {
850 if (!$entry_author) {
851 $entry_author = db_escape_string(strip_tags($item['author']['name']));
854 if (!$entry_author) {
855 $entry_author = db_escape_string(strip_tags($item['author']['email']));
859 if (!$entry_author) {
860 $entry_author = db_escape_string(strip_tags($item['author']));
865 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
867 $entry_guid = db_escape_string(strip_tags($entry_guid));
868 $entry_guid = mb_substr($entry_guid, 0, 250);
870 $result = db_query($link, "SELECT id FROM ttrss_entries
871 WHERE guid = '$entry_guid'");
873 $entry_content = db_escape_string($entry_content);
875 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
877 $entry_title = db_escape_string($entry_title);
878 $entry_link = db_escape_string($entry_link);
879 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
880 $entry_author = mb_substr($entry_author, 0, 250);
882 if ($use_simplepie) {
883 $num_comments = 0; #FIXME#
885 $num_comments = db_escape_string($item["slash"]["comments"]);
888 if (!$num_comments) $num_comments = 0;
890 // parse <category> entries into tags
892 if ($use_simplepie) {
894 $additional_tags = array();
895 $additional_tags_src = $item->get_categories();
897 if (is_array($additional_tags_src)) {
898 foreach ($additional_tags_src as $tobj) {
899 array_push($additional_tags, $tobj->get_term());
903 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
904 _debug("update_rss_feed: category tags:");
905 print_r($additional_tags);
910 $t_ctr = $item['category#'];
912 $additional_tags = false;
915 $additional_tags = false;
916 } else if ($t_ctr > 0) {
917 $additional_tags = array($item['category']);
919 if ($item['category@term']) {
920 array_push($additional_tags, $item['category@term']);
923 for ($i = 0; $i <= $t_ctr; $i++
) {
924 if ($item["category#$i"]) {
925 array_push($additional_tags, $item["category#$i"]);
928 if ($item["category#$i@term"]) {
929 array_push($additional_tags, $item["category#$i@term"]);
934 // parse <dc:subject> elements
936 $t_ctr = $item['dc']['subject#'];
939 $additional_tags = array($item['dc']['subject']);
941 for ($i = 0; $i <= $t_ctr; $i++
) {
942 if ($item['dc']["subject#$i"]) {
943 array_push($additional_tags, $item['dc']["subject#$i"]);
951 $enclosures = array();
953 if ($use_simplepie) {
954 $encs = $item->get_enclosures();
956 if (is_array($encs)) {
957 foreach ($encs as $e) {
959 $e->link
, $e->type
, $e->length
);
961 array_push($enclosures, $e_item);
966 $e_ctr = $item['enclosure#'];
969 $e_item = array($item['enclosure@url'],
970 $item['enclosure@type'],
971 $item['enclosure@length']);
973 array_push($enclosures, $e_item);
975 for ($i = 0; $i <= $e_ctr; $i++
) {
977 if ($item["enclosure#$i@url"]) {
978 $e_item = array($item["enclosure#$i@url"],
979 $item["enclosure#$i@type"],
980 $item["enclosure#$i@length"]);
981 array_push($enclosures, $e_item);
990 // $entry_content = sanitize_rss($entry_content);
992 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
993 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
996 db_query($link, "BEGIN");
998 if (db_num_rows($result) == 0) {
1000 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1001 _debug("update_rss_feed: base guid not found");
1004 // base post entry does not exist, create it
1006 $result = db_query($link,
1007 "INSERT INTO ttrss_entries
1023 '$entry_timestamp_fmt',
1032 // we keep encountering the entry in feeds, so we need to
1033 // update date_entered column so that we don't get horrible
1034 // dupes when the entry gets purged and reinserted again e.g.
1035 // in the case of SLOW SLOW OMG SLOW updating feeds
1037 $base_entry_id = db_fetch_result($result, 0, "id");
1039 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
1040 WHERE id = '$base_entry_id'");
1043 // now it should exist, if not - bad luck then
1045 $result = db_query($link, "SELECT
1046 id,content_hash,no_orig_date,title,
1047 substring(date_entered,1,19) as date_entered,
1048 substring(updated,1,19) as updated,
1052 WHERE guid = '$entry_guid'");
1057 if (db_num_rows($result) == 1) {
1059 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1060 _debug("update_rss_feed: base guid found, checking for user record");
1063 // this will be used below in update handler
1064 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1065 $orig_title = db_fetch_result($result, 0, "title");
1066 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
1067 $orig_date_entered = strtotime(db_fetch_result($result,
1068 0, "date_entered"));
1070 $ref_id = db_fetch_result($result, 0, "id");
1071 $entry_ref_id = $ref_id;
1073 // check for user post link to main table
1075 // do we allow duplicate posts with same GUID in different feeds?
1076 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
1077 $dupcheck_qpart = "AND feed_id = '$feed'";
1079 $dupcheck_qpart = "";
1082 // error_reporting(0);
1084 $article_filters = get_article_filters($filters, $entry_title,
1085 $entry_content, $entry_link);
1087 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1088 _debug("update_rss_feed: article filters: ");
1089 if (count($article_filters) != 0) {
1090 print_r($article_filters);
1094 if (find_article_filter($article_filters, "filter")) {
1098 // error_reporting (DEFAULT_ERROR_LEVEL);
1100 $result = db_query($link,
1101 "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
1102 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
1105 // okay it doesn't exist - create user entry
1106 if (db_num_rows($result) == 0) {
1108 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1109 _debug("update_rss_feed: user record not found, creating...");
1112 if (!find_article_filter($article_filters, 'catchup')) {
1114 $last_read_qpart = 'NULL';
1117 $last_read_qpart = 'NOW()';
1120 if (find_article_filter($article_filters, 'mark')) {
1126 if (find_article_filter($article_filters, 'publish')) {
1127 $published = 'true';
1129 $published = 'false';
1132 $result = db_query($link,
1133 "INSERT INTO ttrss_user_entries
1134 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
1135 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
1136 $last_read_qpart, $marked, $published)");
1138 $result = db_query($link,
1139 "SELECT int_id FROM ttrss_user_entries WHERE
1140 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1141 feed_id = '$feed' LIMIT 1");
1143 if (db_num_rows($result) == 1) {
1144 $entry_int_id = db_fetch_result($result, 0, "int_id");
1147 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1148 $entry_int_id = db_fetch_result($result, 0, "int_id");
1151 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1152 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1155 $post_needs_update = false;
1157 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
1158 ($content_hash != $orig_content_hash)) {
1159 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
1160 $post_needs_update = true;
1163 if (db_escape_string($orig_title) != $entry_title) {
1164 $post_needs_update = true;
1167 if ($orig_num_comments != $num_comments) {
1168 $post_needs_update = true;
1171 // this doesn't seem to be very reliable
1173 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1174 // $post_needs_update = true;
1177 // if post needs update, update it and mark all user entries
1178 // linking to this post as updated
1179 if ($post_needs_update) {
1181 if (defined('DAEMON_EXTENDED_DEBUG')) {
1182 _debug("update_rss_feed: post $entry_guid needs update...");
1185 // print "<!-- post $orig_title needs update : $post_needs_update -->";
1187 db_query($link, "UPDATE ttrss_entries
1188 SET title = '$entry_title', content = '$entry_content',
1189 content_hash = '$content_hash',
1190 num_comments = '$num_comments'
1191 WHERE id = '$ref_id'");
1193 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
1194 db_query($link, "UPDATE ttrss_user_entries
1195 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1197 db_query($link, "UPDATE ttrss_user_entries
1198 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1204 db_query($link, "COMMIT");
1206 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1207 _debug("update_rss_feed: looking for enclosures...");
1210 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1211 print_r($enclosures);
1214 db_query($link, "BEGIN");
1216 foreach ($enclosures as $enc) {
1217 $enc_url = db_escape_string($enc[0]);
1218 $enc_type = db_escape_string($enc[1]);
1219 $enc_dur = db_escape_string($enc[2]);
1221 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1222 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1224 if (db_num_rows($result) == 0) {
1225 db_query($link, "INSERT INTO ttrss_enclosures
1226 (content_url, content_type, title, duration, post_id) VALUES
1227 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1231 db_query($link, "COMMIT");
1233 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1234 _debug("update_rss_feed: looking for tags...");
1238 // <a href="..." rel="tag">Xorg</a>, //
1242 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
1243 $entry_content_unescaped, $entry_tags);
1245 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1246 print_r($entry_tags);
1247 print "<br/></p>"; */
1249 $entry_tags = $entry_tags[1];
1251 # check for manual tags
1253 $tag_filter = find_article_filter($article_filters, "tag");
1257 $manual_tags = trim_array(split(",", $tag_filter[1]));
1259 foreach ($manual_tags as $tag) {
1260 if (tag_is_valid($tag)) {
1261 array_push($entry_tags, $tag);
1266 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1267 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1269 if ($additional_tags && is_array($additional_tags)) {
1270 foreach ($additional_tags as $tag) {
1271 if (tag_is_valid($tag) &&
1272 array_search($tag, $boring_tags) === FALSE) {
1273 array_push($entry_tags, $tag);
1278 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1280 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1281 print_r($entry_tags);
1284 if (count($entry_tags) > 0) {
1286 db_query($link, "BEGIN");
1288 foreach ($entry_tags as $tag) {
1290 $tag = sanitize_tag($tag);
1291 $tag = db_escape_string($tag);
1293 if (!tag_is_valid($tag)) continue;
1295 $result = db_query($link, "SELECT id FROM ttrss_tags
1296 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1297 owner_uid = '$owner_uid' LIMIT 1");
1299 // print db_fetch_result($result, 0, "id");
1301 if ($result && db_num_rows($result) == 0) {
1303 db_query($link, "INSERT INTO ttrss_tags
1304 (owner_uid,tag_name,post_int_id)
1305 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1309 db_query($link, "COMMIT");
1312 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1313 _debug("update_rss_feed: article processed");
1317 db_query($link, "UPDATE ttrss_feeds
1318 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1320 // db_query($link, "COMMIT");
1324 if ($use_simplepie) {
1325 $error_msg = mb_substr($rss->error(), 0, 250);
1327 $error_msg = mb_substr(magpie_error(), 0, 250);
1330 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1331 _debug("update_rss_feed: error fetching feed: $error_msg");
1334 $error_msg = db_escape_string($error_msg);
1337 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1338 last_updated = NOW() WHERE id = '$feed'");
1341 if ($use_simplepie) {
1345 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1346 _debug("update_rss_feed: done");
1351 function print_select($id, $default, $values, $attributes = "") {
1352 print "<select name=\"$id\" id=\"$id\" $attributes>";
1353 foreach ($values as $v) {
1359 print "<option$sel>$v</option>";
1364 function print_select_hash($id, $default, $values, $attributes = "") {
1365 print "<select name=\"$id\" id='$id' $attributes>";
1366 foreach (array_keys($values) as $v) {
1372 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1378 function get_article_filters($filters, $title, $content, $link) {
1381 if ($filters["title"]) {
1382 foreach ($filters["title"] as $filter) {
1383 $reg_exp = $filter["reg_exp"];
1384 $inverse = $filter["inverse"];
1385 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1386 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1388 array_push($matches, array($filter["action"], $filter["action_param"]));
1393 if ($filters["content"]) {
1394 foreach ($filters["content"] as $filter) {
1395 $reg_exp = $filter["reg_exp"];
1396 $inverse = $filter["inverse"];
1398 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1399 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1401 array_push($matches, array($filter["action"], $filter["action_param"]));
1406 if ($filters["both"]) {
1407 foreach ($filters["both"] as $filter) {
1408 $reg_exp = $filter["reg_exp"];
1409 $inverse = $filter["inverse"];
1412 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1413 array_push($matches, array($filter["action"], $filter["action_param"]));
1416 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1417 array_push($matches, array($filter["action"], $filter["action_param"]));
1423 if ($filters["link"]) {
1424 $reg_exp = $filter["reg_exp"];
1425 foreach ($filters["link"] as $filter) {
1426 $reg_exp = $filter["reg_exp"];
1427 $inverse = $filter["inverse"];
1429 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1430 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1432 array_push($matches, array($filter["action"], $filter["action_param"]));
1440 function find_article_filter($filters, $filter_name) {
1441 foreach ($filters as $f) {
1442 if ($f[0] == $filter_name) {
1449 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1450 $rtl_content = false, $last_updated = false, $last_error = false) {
1452 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1453 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1455 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1459 $rtl_tag = "dir=\"rtl\"";
1461 $rtl_tag = "dir=\"ltr\"";
1464 $error_notify_msg = "";
1467 $link_title = "Error: $last_error ($last_updated)";
1468 $error_notify_msg = "(Error)";
1469 } else if ($last_updated) {
1470 $link_title = "Updated: $last_updated";
1473 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1474 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1476 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1477 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1481 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1486 $fctr_class = "class=\"invisible\"";
1489 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1490 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1492 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1493 print "<div class=\"feedExtInfo\">
1494 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1501 function getmicrotime() {
1502 list($usec, $sec) = explode(" ",microtime());
1503 return ((float)$usec +
(float)$sec);
1506 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1507 foreach ($values as $v) {
1514 if ($v == $true_is) {
1515 $sel .= " value=\"1\"";
1517 $sel .= " value=\"0\"";
1520 print "<input class=\"noborder\"
1521 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1526 function initialize_user_prefs($link, $uid) {
1528 $uid = db_escape_string($uid);
1530 db_query($link, "BEGIN");
1532 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1534 $u_result = db_query($link, "SELECT pref_name
1535 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1537 $active_prefs = array();
1539 while ($line = db_fetch_assoc($u_result)) {
1540 array_push($active_prefs, $line["pref_name"]);
1543 while ($line = db_fetch_assoc($result)) {
1544 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1545 // print "adding " . $line["pref_name"] . "<br>";
1547 db_query($link, "INSERT INTO ttrss_user_prefs
1548 (owner_uid,pref_name,value) VALUES
1549 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1554 db_query($link, "COMMIT");
1558 function lookup_user_id($link, $user) {
1560 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1563 if (db_num_rows($result) == 1) {
1564 return db_fetch_result($result, 0, "id");
1570 function http_authenticate_user($link) {
1572 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1574 if (!$_SERVER["PHP_AUTH_USER"]) {
1576 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1577 header('HTTP/1.0 401 Unauthorized');
1581 $auth_result = authenticate_user($link,
1582 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1584 if (!$auth_result) {
1585 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1586 header('HTTP/1.0 401 Unauthorized');
1594 function authenticate_user($link, $login, $password, $force_auth = false) {
1596 if (!SINGLE_USER_MODE
) {
1598 $pwd_hash1 = encrypt_password($password);
1599 $pwd_hash2 = encrypt_password($password, $login);
1601 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
1602 && $_SERVER["REMOTE_USER"]) {
1604 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1606 $query = "SELECT id,login,access_level
1607 FROM ttrss_users WHERE
1611 $query = "SELECT id,login,access_level,pwd_hash
1612 FROM ttrss_users WHERE
1613 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1614 pwd_hash = '$pwd_hash2')";
1617 $result = db_query($link, $query);
1619 if (db_num_rows($result) == 1) {
1620 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1621 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1622 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1624 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1627 $user_theme = get_user_theme_path($link);
1629 $_SESSION["theme"] = $user_theme;
1630 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1631 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
1633 initialize_user_prefs($link, $_SESSION["uid"]);
1642 $_SESSION["uid"] = 1;
1643 $_SESSION["name"] = "admin";
1645 $user_theme = get_user_theme_path($link);
1647 $_SESSION["theme"] = $user_theme;
1648 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1650 initialize_user_prefs($link, $_SESSION["uid"]);
1656 function make_password($length = 8) {
1659 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1663 while ($i < $length) {
1664 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1666 if (!strstr($password, $char)) {
1674 // this is called after user is created to initialize default feeds, labels
1677 // user preferences are checked on every login, not here
1679 function initialize_user($link, $uid) {
1681 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1682 values ('$uid','unread = true', 'Unread articles')");
1684 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1685 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1687 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1688 values ('$uid', 'Tiny Tiny RSS: New Releases',
1689 'http://tt-rss.spb.ru/releases.rss')");
1691 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1692 values ('$uid', 'Tiny Tiny RSS: Forum',
1693 'http://tt-rss.spb.ru/forum/rss.php')");
1696 function logout_user() {
1698 if (isset($_COOKIE[session_name()])) {
1699 setcookie(session_name(), '', time()-42000, '/');
1703 function get_script_urlpath() {
1704 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1707 function validate_session($link) {
1708 if (SINGLE_USER_MODE
) {
1712 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1713 if ($_SESSION["ip_address"]) {
1714 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1715 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1721 if ($_SESSION["uid"]) {
1723 $result = db_query($link,
1724 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1726 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1728 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1733 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1735 //print_r($_SESSION);
1737 if (time() > $_SESSION["cookie_lifetime"]) {
1745 function login_sequence($link, $mobile = false) {
1746 if (!SINGLE_USER_MODE
) {
1748 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1749 $swu = db_escape_string($_REQUEST["swu"]);
1751 $_SESSION["prefs_cache"] = false;
1752 return authenticate_user($link, $swu, null, true);
1756 $login_action = $_POST["login_action"];
1758 # try to authenticate user if called from login form
1759 if ($login_action == "do_login") {
1760 $login = $_POST["login"];
1761 $password = $_POST["password"];
1762 $remember_me = $_POST["remember_me"];
1764 if (authenticate_user($link, $login, $password)) {
1765 $_POST["password"] = "";
1767 $_SESSION["language"] = $_POST["language"];
1769 header("Location: " . $_SERVER["REQUEST_URI"]);
1774 $_SESSION["login_error_msg"] = "Incorrect username or password";
1778 // print session_id();
1779 // print_r($_SESSION);
1781 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1782 render_login_form($link, $mobile);
1785 /* bump login timestamp */
1786 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1789 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME
> 0) {
1790 setcookie("ttrss_lang", $_SESSION["language"],
1791 time() + SESSION_COOKIE_LIFETIME
);
1796 return authenticate_user($link, "admin", null);
1800 function truncate_string($str, $max_len) {
1801 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1802 return mb_substr($str, 0, $max_len, "utf-8") . "…";
1808 function get_user_theme_path($link) {
1809 $result = db_query($link, "SELECT theme_path
1811 ttrss_themes,ttrss_users
1812 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1813 if (db_num_rows($result) != 0) {
1814 return db_fetch_result($result, 0, "theme_path");
1820 function smart_date_time($timestamp) {
1821 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1822 return date("G:i", $timestamp);
1823 } else if (date("Y", $timestamp) == date("Y")) {
1824 return date("M d, G:i", $timestamp);
1826 return date("Y/m/d, G:i", $timestamp);
1830 function smart_date($timestamp) {
1831 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1833 } else if (date("Y", $timestamp) == date("Y")) {
1834 return date("D m", $timestamp);
1836 return date("Y/m/d", $timestamp);
1840 function sql_bool_to_string($s) {
1841 if ($s == "t" ||
$s == "1") {
1848 function sql_bool_to_bool($s) {
1849 if ($s == "t" ||
$s == "1") {
1857 function toggleEvenOdd($a) {
1864 function sanity_check($link) {
1869 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1870 $schema_version = db_fetch_result($result, 0, "schema_version");
1872 if ($schema_version != SCHEMA_VERSION
) {
1876 if (DB_TYPE
== "mysql") {
1877 $result = db_query($link, "SELECT true", false);
1878 if (db_num_rows($result) != 1) {
1883 error_reporting (DEFAULT_ERROR_LEVEL
);
1885 if ($error_code != 0) {
1886 print_error_xml($error_code);
1893 function file_is_locked($filename) {
1894 if (function_exists('flock')) {
1896 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "r");
1897 error_reporting(DEFAULT_ERROR_LEVEL
);
1899 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1900 flock($fp, LOCK_UN
);
1911 function make_lockfile($filename) {
1912 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "w");
1914 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1921 function make_stampfile($filename) {
1922 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "w");
1924 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1925 fwrite($fp, time() . "\n");
1926 flock($fp, LOCK_UN
);
1934 function read_stampfile($filename) {
1937 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "r");
1938 error_reporting (DEFAULT_ERROR_LEVEL
);
1940 if (flock($fp, LOCK_EX
)) {
1941 $stamp = fgets($fp);
1942 flock($fp, LOCK_UN
);
1950 function sql_random_function() {
1951 if (DB_TYPE
== "mysql") {
1958 function catchup_feed($link, $feed, $cat_view) {
1960 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1965 $cat_qpart = "cat_id = '$feed'";
1967 $cat_qpart = "cat_id IS NULL";
1970 $tmp_result = db_query($link, "SELECT id
1971 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1974 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1976 $tmp_feed = $tmp_line["id"];
1978 db_query($link, "UPDATE ttrss_user_entries
1979 SET unread = false,last_read = NOW()
1980 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1983 } else if ($feed > 0) {
1985 $tmp_result = db_query($link, "SELECT id
1986 FROM ttrss_feeds WHERE parent_feed = '$feed'
1987 ORDER BY cat_id,title");
1989 $parent_ids = array();
1991 if (db_num_rows($tmp_result) > 0) {
1992 while ($p = db_fetch_assoc($tmp_result)) {
1993 array_push($parent_ids, "feed_id = " . $p["id"]);
1996 $children_qpart = implode(" OR ", $parent_ids);
1998 db_query($link, "UPDATE ttrss_user_entries
1999 SET unread = false,last_read = NOW()
2000 WHERE (feed_id = '$feed' OR $children_qpart)
2001 AND owner_uid = " . $_SESSION["uid"]);
2004 db_query($link, "UPDATE ttrss_user_entries
2005 SET unread = false,last_read = NOW()
2006 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2009 } else if ($feed < 0 && $feed > -10) { // special, like starred
2012 db_query($link, "UPDATE ttrss_user_entries
2013 SET unread = false,last_read = NOW()
2014 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
2018 db_query($link, "UPDATE ttrss_user_entries
2019 SET unread = false,last_read = NOW()
2020 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
2025 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2027 if (DB_TYPE
== "pgsql") {
2028 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
2030 $match_part = "date_entered > DATE_SUB(NOW(),
2031 INTERVAL $intl HOUR) ";
2034 $result = db_query($link, "SELECT id FROM ttrss_entries,
2035 ttrss_user_entries WHERE $match_part AND
2037 ttrss_user_entries.ref_id = ttrss_entries.id AND
2038 owner_uid = ".$_SESSION["uid"]);
2040 $affected_ids = array();
2042 while ($line = db_fetch_assoc($result)) {
2043 array_push($affected_ids, $line["id"]);
2046 catchupArticlesById($link, $affected_ids, 0);
2049 } else if ($feed < -10) { // label
2051 // TODO make this more efficient
2053 $label_id = -$feed - 11;
2055 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2056 WHERE id = '$label_id'");
2059 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
2061 db_query($link, "BEGIN");
2063 $tmp2_result = db_query($link,
2067 ttrss_user_entries,ttrss_entries,ttrss_feeds
2069 ref_id = ttrss_entries.id AND
2070 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2072 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2074 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
2075 db_query($link, "UPDATE
2078 unread = false, last_read = NOW()
2080 int_id = " . $tmp_line["int_id"]);
2083 db_query($link, "COMMIT");
2085 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
2086 SET unread = false,last_read = NOW()
2089 AND owner_uid = ".$_SESSION["uid"]); */
2093 db_query($link, "BEGIN");
2095 $tag_name = db_escape_string($feed);
2097 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
2098 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
2100 while ($line = db_fetch_assoc($result)) {
2101 db_query($link, "UPDATE ttrss_user_entries SET
2102 unread = false, last_read = NOW()
2103 WHERE int_id = " . $line["post_int_id"]);
2105 db_query($link, "COMMIT");
2109 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
2113 $cat_qpart = "cat_id = '$feed'";
2115 $cat_qpart = "cat_id IS NULL";
2118 $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds
2119 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2121 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2122 $feed_url = $tmp_line["feed_url"];
2123 $feed_id = $tmp_line["id"];
2124 update_rss_feed($link, $feed_url, $feed_id, $force_update);
2128 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
2129 WHERE id = '$feed'");
2130 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
2131 update_rss_feed($link, $feed_url, $feed, $force_update);
2135 function getAllCounters($link, $omode = "flc", $active_feed = false) {
2136 /* getLabelCounters($link);
2137 getFeedCounters($link);
2138 getTagCounters($link);
2139 getGlobalCounters($link);
2140 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2141 getCategoryCounters($link);
2144 if (!$omode) $omode = "flc";
2146 getGlobalCounters($link);
2148 if (strchr($omode, "l")) getLabelCounters($link);
2149 if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS
, $active_feed);
2150 if (strchr($omode, "t")) getTagCounters($link);
2151 if (strchr($omode, "c")) {
2152 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2153 getCategoryCounters($link);
2158 function getCategoryCounters($link) {
2159 # two special categories are -1 and -2 (all virtuals; all labels)
2161 $ctr = getCategoryUnread($link, -1);
2163 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
2165 $ctr = getCategoryUnread($link, -2);
2167 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2169 $age_qpart = getMaxAgeSubquery();
2171 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
2172 FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id
2173 AND id = ref_id AND $age_qpart
2174 AND unread = true)) AS unread FROM ttrss_feeds
2176 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
2178 while ($line = db_fetch_assoc($result)) {
2179 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
2180 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2181 $line["unread"]."\"/>";
2185 function getCategoryUnread($link, $cat) {
2190 $cat_query = "cat_id = '$cat'";
2192 $cat_query = "cat_id IS NULL";
2195 $age_qpart = getMaxAgeSubquery();
2197 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
2199 AND owner_uid = " . $_SESSION["uid"]);
2201 $cat_feeds = array();
2202 while ($line = db_fetch_assoc($result)) {
2203 array_push($cat_feeds, "feed_id = " . $line["id"]);
2206 if (count($cat_feeds) == 0) return 0;
2208 $match_part = implode(" OR ", $cat_feeds);
2210 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2211 FROM ttrss_user_entries,ttrss_entries
2212 WHERE unread = true AND ($match_part) AND id = ref_id
2213 AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
2217 # this needs to be rewritten
2218 while ($line = db_fetch_assoc($result)) {
2219 $unread +
= $line["unread"];
2223 } else if ($cat == -1) {
2224 return getFeedUnread($link, -1) +
getFeedUnread($link, -2) +
getFeedUnread($link, -3);
2225 } else if ($cat == -2) {
2227 $rv = getLabelCounters($link, false, true);
2230 foreach (array_keys($rv) as $k) {
2232 $ctr +
= $rv[$k]["counter"];
2240 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE
) {
2241 if (DB_TYPE
== "pgsql") {
2242 return "ttrss_entries.date_entered >
2243 NOW() - INTERVAL '$days days'";
2245 return "ttrss_entries.date_entered >
2246 DATE_SUB(NOW(), INTERVAL $days DAY)";
2250 function getFeedUnread($link, $feed, $is_cat = false) {
2251 $n_feed = sprintf("%d", $feed);
2253 $age_qpart = getMaxAgeSubquery();
2256 return getCategoryUnread($link, $n_feed);
2257 } else if ($n_feed == -1) {
2258 $match_part = "marked = true";
2259 } else if ($n_feed == -2) {
2260 $match_part = "published = true";
2261 } else if ($n_feed == -3) {
2262 $match_part = "unread = true";
2264 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2266 if (DB_TYPE
== "pgsql") {
2267 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2269 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2272 } else if ($n_feed > 0) {
2274 $result = db_query($link, "SELECT id FROM ttrss_feeds
2275 WHERE parent_feed = '$n_feed'
2277 AND owner_uid = " . $_SESSION["uid"]);
2279 if (db_num_rows($result) > 0) {
2281 $linked_feeds = array();
2282 while ($line = db_fetch_assoc($result)) {
2283 array_push($linked_feeds, "feed_id = " . $line["id"]);
2286 array_push($linked_feeds, "feed_id = $n_feed");
2288 $match_part = implode(" OR ", $linked_feeds);
2290 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2291 FROM ttrss_user_entries,ttrss_entries
2292 WHERE unread = true AND
2293 ttrss_user_entries.ref_id = ttrss_entries.id AND
2296 owner_uid = " . $_SESSION["uid"]);
2300 # this needs to be rewritten
2301 while ($line = db_fetch_assoc($result)) {
2302 $unread +
= $line["unread"];
2308 $match_part = "feed_id = '$n_feed'";
2310 } else if ($feed < -10) {
2312 $label_id = -$feed - 11;
2314 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2315 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2317 $match_part = db_fetch_result($result, 0, "sql_exp");
2322 $result = db_query($link, "SELECT count(int_id) AS unread
2323 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2324 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2325 ttrss_user_entries.ref_id = ttrss_entries.id AND
2326 ttrss_feeds.hidden = false AND
2328 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2332 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2333 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
2334 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
2335 AND unread = true AND $age_qpart AND
2336 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2339 $unread = db_fetch_result($result, 0, "unread");
2344 /* FIXME this needs reworking */
2346 function getGlobalUnread($link, $user_id = false) {
2349 $user_id = $_SESSION["uid"];
2352 $age_qpart = getMaxAgeSubquery();
2354 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2355 WHERE unread = true AND
2356 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2357 ttrss_user_entries.ref_id = ttrss_entries.id AND
2360 ttrss_user_entries.owner_uid = '$user_id'");
2361 $c_id = db_fetch_result($result, 0, "c_id");
2365 function getGlobalCounters($link, $global_unread = -1) {
2366 if ($global_unread == -1) {
2367 $global_unread = getGlobalUnread($link);
2369 print "<counter type=\"global\" id='global-unread'
2370 counter='$global_unread'/>";
2372 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2373 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2375 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2377 print "<counter type=\"global\" id='subscribed-feeds'
2378 counter='$subscribed_feeds'/>";
2382 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2385 if (!$_SESSION["tctr_last_value"]) {
2386 $_SESSION["tctr_last_value"] = array();
2390 $old_counters = $_SESSION["tctr_last_value"];
2392 $tctrs_modified = false;
2394 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2395 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2396 ttrss_user_entries.ref_id = ttrss_entries.id AND
2397 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2398 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2400 select tag_name,0 as count FROM ttrss_tags
2401 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2403 $age_qpart = getMaxAgeSubquery();
2405 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2406 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2407 AND ref_id = id AND $age_qpart
2408 AND unread = true)) AS count FROM ttrss_tags
2409 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2410 ORDER BY count DESC LIMIT 55");
2414 while ($line = db_fetch_assoc($result)) {
2415 $tags[$line["tag_name"]] +
= $line["count"];
2418 foreach (array_keys($tags) as $tag) {
2419 $unread = $tags[$tag];
2421 $tag = htmlspecialchars($tag);
2423 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
2424 $old_counters[$tag] = $unread;
2425 $tctrs_modified = true;
2426 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2431 if ($smart_mode && $tctrs_modified) {
2432 $_SESSION["tctr_last_value"] = $old_counters;
2437 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
2439 $age_qpart = getMaxAgeSubquery();
2442 if (!$_SESSION["lctr_last_value"]) {
2443 $_SESSION["lctr_last_value"] = array();
2449 $old_counters = $_SESSION["lctr_last_value"];
2450 $lctrs_modified = false;
2452 $count = getFeedUnread($link, -1);
2455 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2457 $ret_arr["-1"]["counter"] = $count;
2458 $ret_arr["-1"]["description"] = __("Starred articles");
2461 $count = getFeedUnread($link, -2);
2464 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2466 $ret_arr["-2"]["counter"] = $count;
2467 $ret_arr["-2"]["description"] = __("Published articles");
2470 $count = getFeedUnread($link, -3);
2473 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2475 $ret_arr["-3"]["counter"] = $count;
2476 $ret_arr["-3"]["description"] = __("Fresh articles");
2480 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2481 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2483 while ($line = db_fetch_assoc($result)) {
2485 $id = -$line["id"] - 11;
2487 $label_name = $line["description"];
2489 error_reporting (0);
2491 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
2492 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
2493 ttrss_feeds.hidden = false AND
2495 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2496 ttrss_user_entries.ref_id = ttrss_entries.id AND
2497 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
2499 $count = db_fetch_result($tmp_result, 0, "count");
2501 if (!$smart_mode ||
$old_counters[$id] != $count) {
2502 $old_counters[$id] = $count;
2503 $lctrs_modified = true;
2505 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2507 $ret_arr[$id]["counter"] = $count;
2508 $ret_arr[$id]["description"] = $label_name;
2512 error_reporting (DEFAULT_ERROR_LEVEL
);
2515 if ($smart_mode && $lctrs_modified) {
2516 $_SESSION["lctr_last_value"] = $old_counters;
2522 /* function getFeedCounter($link, $id) {
2524 $result = db_query($link, "SELECT
2525 count(id) as count,last_error
2526 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2527 WHERE feed_id = '$id' AND unread = true
2528 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2529 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2531 $count = db_fetch_result($result, 0, "count");
2532 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2534 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2537 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $active_feed = false) {
2539 $age_qpart = getMaxAgeSubquery();
2542 if (!$_SESSION["fctr_last_value"]) {
2543 $_SESSION["fctr_last_value"] = array();
2547 $old_counters = $_SESSION["fctr_last_value"];
2549 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2550 SUBSTRING(last_updated,1,19) AS last_updated,
2552 FROM ttrss_entries,ttrss_user_entries
2553 WHERE feed_id = ttrss_feeds.id AND
2554 ttrss_user_entries.ref_id = ttrss_entries.id
2555 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2556 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2557 AND parent_feed IS NULL"); */
2559 $query = "SELECT ttrss_feeds.id,
2561 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2563 COUNT(ttrss_entries.id) AS count
2565 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2566 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2567 AND ttrss_user_entries.unread = true)
2568 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
2570 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2571 AND parent_feed IS NULL
2572 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
2574 $result = db_query($link, $query);
2575 $fctrs_modified = false;
2577 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2579 while ($line = db_fetch_assoc($result)) {
2582 $count = $line["count"];
2583 $last_error = htmlspecialchars($line["last_error"]);
2585 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2586 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2588 $last_updated = date($short_date, strtotime($line["last_updated"]));
2591 $last_updated = htmlspecialchars($last_updated);
2593 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2595 $tmp_result = db_query($link,
2596 "SELECT ttrss_feeds.id,COUNT(unread) AS unread
2597 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2598 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2599 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2600 WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
2602 if (db_num_rows($tmp_result) > 0) {
2603 while ($l = db_fetch_assoc($tmp_result)) {
2604 $count +
= $l["unread"];
2608 if (!$smart_mode ||
$old_counters[$id] != $count) {
2609 $old_counters[$id] = $count;
2610 $fctrs_modified = true;
2613 $error_part = "error=\"$last_error\"";
2619 $has_img_part = "hi=\"$has_img\"";
2624 if ($active_feed && $id == $active_feed) {
2625 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2627 $has_title_part = "";
2630 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $has_title_part/>";
2634 if ($smart_mode && $fctrs_modified) {
2635 $_SESSION["fctr_last_value"] = $old_counters;
2639 function get_script_dt_add() {
2640 if (strpos(VERSION
, ".99") === false) {
2647 function get_pgsql_version($link) {
2648 $result = db_query($link, "SELECT version() AS version");
2649 $version = split(" ", db_fetch_result($result, 0, "version"));
2653 function print_error_xml($code, $add_msg = "") {
2656 $error_msg = $ERRORS[$code];
2659 $error_msg = "$error_msg; $add_msg";
2662 print "<rpc-reply>";
2663 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2664 print "</rpc-reply>";
2667 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2668 $auth_login = '', $auth_pass = '') {
2670 # check for feed:http://url
2671 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2673 # check for feed://URL
2674 if (strpos($feed_link, "//") === 0) {
2675 $feed_link = "http:$feed_link";
2678 if ($feed_link == "") return;
2680 if ($cat_id == "0" ||
!$cat_id) {
2681 $cat_qpart = "NULL";
2683 $cat_qpart = "'$cat_id'";
2686 $result = db_query($link,
2687 "SELECT id FROM ttrss_feeds
2688 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2690 if (db_num_rows($result) == 0) {
2692 $result = db_query($link,
2693 "INSERT INTO ttrss_feeds
2694 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2695 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2696 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2698 $result = db_query($link,
2699 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2700 AND owner_uid = " . $_SESSION["uid"]);
2702 $feed_id = db_fetch_result($result, 0, "id");
2705 update_rss_feed($link, $feed_link, $feed_id, true);
2714 function print_feed_select($link, $id, $default_id = "",
2715 $attributes = "", $include_all_feeds = true) {
2717 print "<select id=\"$id\" name=\"$id\" $attributes>";
2718 if ($include_all_feeds) {
2719 print "<option value=\"0\">".__('All feeds')."</option>";
2722 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2723 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2725 if (db_num_rows($result) > 0 && $include_all_feeds) {
2726 print "<option disabled>--------</option>";
2729 while ($line = db_fetch_assoc($result)) {
2730 if ($line["id"] == $default_id) {
2731 $is_selected = "selected";
2735 printf("<option $is_selected value='%d'>%s</option>",
2736 $line["id"], htmlspecialchars($line["title"]));
2742 function print_feed_cat_select($link, $id, $default_id = "",
2743 $attributes = "", $include_all_cats = true) {
2745 print "<select id=\"$id\" name=\"$id\" $attributes>";
2747 if ($include_all_cats) {
2748 print "<option value=\"0\">".__('Uncategorized')."</option>";
2751 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2752 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2754 if (db_num_rows($result) > 0 && $include_all_cats) {
2755 print "<option disabled>--------</option>";
2758 while ($line = db_fetch_assoc($result)) {
2759 if ($line["id"] == $default_id) {
2760 $is_selected = "selected";
2764 printf("<option $is_selected value='%d'>%s</option>",
2765 $line["id"], htmlspecialchars($line["title"]));
2771 function checkbox_to_sql_bool($val) {
2772 return ($val == "on") ?
"true" : "false";
2775 function getFeedCatTitle($link, $id) {
2777 return __("Special");
2778 } else if ($id < -10) {
2779 return __("Labels");
2780 } else if ($id > 0) {
2781 $result = db_query($link, "SELECT ttrss_feed_categories.title
2782 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2783 cat_id = ttrss_feed_categories.id");
2784 if (db_num_rows($result) == 1) {
2785 return db_fetch_result($result, 0, "title");
2787 return __("Uncategorized");
2790 return "getFeedCatTitle($id) failed";
2795 function getFeedTitle($link, $id) {
2797 return __("Starred articles");
2798 } else if ($id == -2) {
2799 return __("Published articles");
2800 } else if ($id == -3) {
2801 return __("Fresh articles");
2802 } else if ($id < -10) {
2803 $label_id = -10 - $id;
2804 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2805 if (db_num_rows($result) == 1) {
2806 return db_fetch_result($result, 0, "description");
2808 return "Unknown label ($label_id)";
2811 } else if ($id > 0) {
2812 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2813 if (db_num_rows($result) == 1) {
2814 return db_fetch_result($result, 0, "title");
2816 return "Unknown feed ($id)";
2819 return "getFeedTitle($id) failed";
2824 function get_session_cookie_name() {
2825 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2828 function print_init_params($link) {
2829 print "<init-params>";
2830 if ($_SESSION["stored-params"]) {
2831 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2833 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2834 print "<param key=\"$key\" value=\"$value\"/>";
2839 print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
2840 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2841 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2842 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
2844 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2845 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2847 print "<param key=\"hide_read_feeds\" value=\"" .
2848 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2850 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2851 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2853 print "<param key=\"confirm_feed_catchup\" value=\"" .
2854 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2856 print "<param key=\"cdm_auto_catchup\" value=\"" .
2857 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2859 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2861 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2863 print "<param key=\"default_view_mode\" value=\"" .
2864 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2866 print "<param key=\"default_view_limit\" value=\"" .
2867 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2869 print "<param key=\"prefs_active_tab\" value=\"" .
2870 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2872 print "<param key=\"infobox_disable_overlay\" value=\"" .
2873 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2875 print "<param key=\"icons_location\" value=\"" .
2878 print "<param key=\"hide_read_shows_special\" value=\"" .
2879 (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
2881 print "</init-params>";
2884 function print_runtime_info($link) {
2885 print "<runtime-info>";
2887 if (ENABLE_UPDATE_DAEMON
) {
2888 print "<param key=\"daemon_is_running\" value=\"".
2889 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2891 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
2893 $stamp = (int)read_stampfile("update_daemon.stamp");
2895 // print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
2898 $stamp_delta = time() - $stamp;
2900 if ($stamp_delta > 1800) {
2904 $_SESSION["daemon_stamp_check"] = time();
2907 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
2909 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2911 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2916 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2918 if ($_SESSION["last_version_check"] +
7200 < time()) {
2919 $new_version_details = check_for_update($link);
2921 print "<param key=\"new_version_available\" value=\"".
2922 sprintf("%d", $new_version_details != ""). "\"/>";
2924 $_SESSION["last_version_check"] = time();
2928 // print "<param key=\"new_version_available\" value=\"1\"/>";
2930 print "</runtime-info>";
2933 function getSearchSql($search, $match_on) {
2935 $search_query_part = "";
2937 $keywords = split(" ", $search);
2938 $query_keywords = array();
2940 if ($match_on == "both") {
2942 foreach ($keywords as $k) {
2943 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2944 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2947 $search_query_part = implode("AND", $query_keywords) . " AND ";
2949 } else if ($match_on == "title") {
2951 foreach ($keywords as $k) {
2952 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2955 $search_query_part = implode("AND", $query_keywords) . " AND ";
2957 } else if ($match_on == "content") {
2959 foreach ($keywords as $k) {
2960 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2964 $search_query_part = implode("AND", $query_keywords);
2966 return $search_query_part;
2969 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2971 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2975 $search_query_part = getSearchSql($search, $match_on);
2976 $search_query_part .= " AND ";
2979 $search_query_part = "";
2982 $view_query_part = "";
2984 if ($view_mode == "adaptive") {
2986 $view_query_part = " ";
2987 } else if ($feed != -1) {
2988 $unread = getFeedUnread($link, $feed, $cat_view);
2990 $view_query_part = " unread = true AND ";
2995 if ($view_mode == "marked") {
2996 $view_query_part = " marked = true AND ";
2999 if ($view_mode == "unread") {
3000 $view_query_part = " unread = true AND ";
3004 $limit_query_part = "LIMIT " . $limit;
3007 $vfeed_query_part = "";
3009 // override query strategy and enable feed display when searching globally
3010 if ($search && $search_mode == "all_feeds") {
3011 $query_strategy_part = "ttrss_entries.id > 0";
3012 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3013 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
3014 $query_strategy_part = "ttrss_entries.id > 0";
3015 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
3016 id = feed_id) as feed_title,";
3017 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
3019 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3021 $tmp_result = false;
3024 $tmp_result = db_query($link, "SELECT id
3025 FROM ttrss_feeds WHERE cat_id = '$feed'");
3027 $tmp_result = db_query($link, "SELECT id
3028 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
3029 WHERE id = '$feed') AND id != '$feed'");
3032 $cat_siblings = array();
3034 if (db_num_rows($tmp_result) > 0) {
3035 while ($p = db_fetch_assoc($tmp_result)) {
3036 array_push($cat_siblings, "feed_id = " . $p["id"]);
3039 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3040 $feed, implode(" OR ", $cat_siblings));
3043 $query_strategy_part = "ttrss_entries.id > 0";
3046 } else if ($feed >= 0) {
3051 $query_strategy_part = "cat_id = '$feed'";
3053 $query_strategy_part = "cat_id IS NULL";
3056 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3059 $tmp_result = db_query($link, "SELECT id
3060 FROM ttrss_feeds WHERE parent_feed = '$feed'
3061 ORDER BY cat_id,title");
3063 $parent_ids = array();
3065 if (db_num_rows($tmp_result) > 0) {
3066 while ($p = db_fetch_assoc($tmp_result)) {
3067 array_push($parent_ids, "feed_id = " . $p["id"]);
3070 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3071 $feed, implode(" OR ", $parent_ids));
3073 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3075 $query_strategy_part = "feed_id = '$feed'";
3078 } else if ($feed == -1) { // starred virtual feed
3079 $query_strategy_part = "marked = true";
3080 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3081 } else if ($feed == -2) { // published virtual feed
3082 $query_strategy_part = "published = true";
3083 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3084 } else if ($feed == -3) { // fresh virtual feed
3085 $query_strategy_part = "unread = true";
3087 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
3089 if (DB_TYPE
== "pgsql") {
3090 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
3092 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
3095 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3096 } else if ($feed <= -10) { // labels
3097 $label_id = -$feed - 11;
3099 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
3100 WHERE id = '$label_id'");
3102 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
3104 if (!$query_strategy_part) {
3108 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3110 $query_strategy_part = "id > 0"; // dumb
3113 if (get_pref($link, 'REVERSE_HEADLINES')) {
3114 $order_by = "updated";
3116 $order_by = "updated DESC";
3119 if ($override_order) {
3120 $order_by = $override_order;
3125 if ($search && $search_mode == "all_feeds") {
3126 $feed_title = __("Search results")." ($search)";
3127 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3128 $feed_title = __("Search results")." ($search, $feed)";
3129 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3130 $feed_title = $feed;
3131 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
3136 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
3137 WHERE id = '$feed' AND owner_uid = $owner_uid");
3138 $feed_title = db_fetch_result($result, 0, "title");
3140 $feed_title = __("Uncategorized");
3144 $feed_title = __("Searched for")." $search ($feed_title)";
3149 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
3150 WHERE id = '$feed' AND owner_uid = $owner_uid");
3152 $feed_title = db_fetch_result($result, 0, "title");
3153 $feed_site_url = db_fetch_result($result, 0, "site_url");
3154 $last_error = db_fetch_result($result, 0, "last_error");
3157 $feed_title = __("Searched for") . " $search ($feed_title)";
3161 } else if ($feed == -1) {
3162 $feed_title = __("Starred articles");
3163 } else if ($feed == -2) {
3164 $feed_title = __("Published articles");
3165 } else if ($feed == -3) {
3166 $feed_title = __("Fresh articles");
3167 } else if ($feed < -10) {
3168 $label_id = -$feed - 11;
3169 $result = db_query($link, "SELECT description FROM ttrss_labels
3170 WHERE id = '$label_id'");
3171 $feed_title = db_fetch_result($result, 0, "description");
3174 $feed_title = __("Searched for") . " $search ($feed_title)";
3180 if ($feed < -10) error_reporting (0);
3182 $content_query_part = "content as content_preview,";
3184 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3187 $feed_kind = "Feeds";
3189 $feed_kind = "Labels";
3192 if ($limit_query_part) {
3193 $offset_query_part = "OFFSET $offset";
3198 ttrss_entries.id,ttrss_entries.title,
3200 unread,feed_id,marked,published,link,last_read,
3201 SUBSTRING(last_read,1,19) as last_read_noms,
3204 SUBSTRING(updated,1,19) as updated_noms,
3207 ttrss_entries,ttrss_user_entries,ttrss_feeds
3209 ttrss_feeds.hidden = false AND
3210 ttrss_user_entries.feed_id = ttrss_feeds.id AND
3211 ttrss_user_entries.ref_id = ttrss_entries.id AND
3212 ttrss_user_entries.owner_uid = '$owner_uid' AND
3215 $query_strategy_part ORDER BY $order_by
3216 $limit_query_part $offset_query_part";
3218 $result = db_query($link, $query);
3220 if ($_GET["debug"]) print $query;
3225 $feed_kind = "Tags";
3227 $result = db_query($link, "SELECT
3229 ttrss_entries.id as id,title,
3232 marked,link,last_read,
3233 SUBSTRING(last_read,1,19) as last_read_noms,
3236 SUBSTRING(updated,1,19) as updated_noms
3238 ttrss_entries,ttrss_user_entries,ttrss_tags
3240 ref_id = ttrss_entries.id AND
3241 ttrss_user_entries.owner_uid = '$owner_uid' AND
3242 post_int_id = int_id AND tag_name = '$feed' AND
3245 $query_strategy_part ORDER BY $order_by
3246 $limit_query_part");
3249 return array($result, $feed_title, $feed_site_url, $last_error);
3253 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3254 $search, $search_mode, $match_on) {
3256 $qfh_ret = queryFeedHeadlines($link, $feed,
3257 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
3260 $result = $qfh_ret[0];
3261 $feed_title = htmlspecialchars($qfh_ret[1]);
3262 $feed_site_url = $qfh_ret[2];
3263 $last_error = $qfh_ret[3];
3265 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
3267 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3268 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3269 <rss version=\"2.0\">
3271 <title>$feed_title</title>
3272 <link>$feed_site_url</link>
3273 <description>Feed generated by Tiny Tiny RSS</description>";
3275 while ($line = db_fetch_assoc($result)) {
3277 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3278 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
3280 $tags = get_article_tags($link, $line["id"], $owner_uid);
3282 foreach ($tags as $tag) {
3283 print "<category>" . htmlspecialchars($tag) . "</category>";
3286 $rfc822_date = date('r', strtotime($line["updated"]));
3288 print "<pubDate>$rfc822_date</pubDate>";
3291 htmlspecialchars($line["title"]) . "</title>";
3293 print "<description><![CDATA[" .
3294 $line["content_preview"] . "]]></description>";
3299 print "</channel></rss>";
3303 function getCategoryTitle($link, $cat_id) {
3305 if ($cat_id == -1) {
3306 return __("Special");
3307 } else if ($cat_id == -2) {
3308 return __("Labels");
3311 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3314 if (db_num_rows($result) == 1) {
3315 return db_fetch_result($result, 0, "title");
3317 return "Uncategorized";
3322 // http://ru2.php.net/strip-tags
3324 function strip_tags_long($textstring, $allowed){
3325 while($textstring != strip_tags($textstring, $allowed))
3327 while (strlen($textstring) != 0)
3329 if (strlen($textstring) > 1024) {
3332 $otherlen = strlen($textstring);
3334 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3335 $safetext .= $temptext;
3336 $textstring = substr_replace($textstring,'',0,$otherlen);
3338 $textstring = $safetext;
3344 function sanitize_rss($link, $str, $force_strip_tags = false) {
3347 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
3349 $res = strip_tags_long($res,
3350 "<p><a><i><em><b><strong><blockquote><br><img><div><span><ul><ol><li>");
3352 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
3353 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3359 function send_headlines_digests($link, $limit = 100) {
3361 if (!DIGEST_ENABLE
) return false;
3363 $user_limit = DIGEST_EMAIL_LIMIT
;
3366 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3368 if (DB_TYPE
== "pgsql") {
3369 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3370 } else if (DB_TYPE
== "mysql") {
3371 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3374 $result = db_query($link, "SELECT id,email FROM ttrss_users
3375 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3377 while ($line = db_fetch_assoc($result)) {
3379 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3380 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3382 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3384 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3385 $digest = $tuple[0];
3386 $headlines_count = $tuple[1];
3387 $affected_ids = $tuple[2];
3388 $digest_text = $tuple[3];
3390 if ($headlines_count > 0) {
3392 $mail = new PHPMailer();
3394 $mail->PluginDir
= "phpmailer/";
3395 $mail->SetLanguage("en", "phpmailer/language/");
3397 $mail->CharSet
= "UTF-8";
3399 $mail->From
= DIGEST_FROM_ADDRESS
;
3400 $mail->FromName
= DIGEST_FROM_NAME
;
3401 $mail->AddAddress($line["email"], $line["login"]);
3403 if (DIGEST_SMTP_HOST
) {
3404 $mail->Host
= DIGEST_SMTP_HOST
;
3405 $mail->Mailer
= "smtp";
3406 $mail->Username
= DIGEST_SMTP_LOGIN
;
3407 $mail->Password
= DIGEST_SMTP_PASSWORD
;
3410 $mail->IsHTML(true);
3411 $mail->Subject
= DIGEST_SUBJECT
;
3412 $mail->Body
= $digest;
3413 $mail->AltBody
= $digest_text;
3415 $rc = $mail->Send();
3417 if (!$rc) print "ERROR: " . $mail->ErrorInfo
;
3421 if ($rc && $do_catchup) {
3422 print "Marking affected articles as read...\n";
3423 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
3426 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3427 WHERE id = " . $line["id"]);
3429 print "No headlines\n";
3434 print "All done.\n";
3438 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3440 require_once "MiniTemplator.class.php";
3442 $tpl = new MiniTemplator
;
3443 $tpl_t = new MiniTemplator
;
3445 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3446 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3448 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3449 $tpl->setVariable('CUR_TIME', date('G:i'));
3451 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3452 $tpl_t->setVariable('CUR_TIME', date('G:i'));
3454 $affected_ids = array();
3456 if (DB_TYPE
== "pgsql") {
3457 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3458 } else if (DB_TYPE
== "mysql") {
3459 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3462 $result = db_query($link, "SELECT ttrss_entries.title,
3463 ttrss_feeds.title AS feed_title,
3465 ttrss_user_entries.ref_id,
3467 SUBSTRING(content, 1, 120) AS excerpt,
3468 SUBSTRING(last_updated,1,19) AS last_updated
3470 ttrss_user_entries,ttrss_entries,ttrss_feeds
3472 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3473 AND include_in_digest = true
3476 AND ttrss_user_entries.owner_uid = $user_id
3478 ORDER BY ttrss_feeds.title, date_entered DESC
3481 $cur_feed_title = "";
3483 $headlines_count = db_num_rows($result);
3485 $headlines = array();
3487 while ($line = db_fetch_assoc($result)) {
3488 array_push($headlines, $line);
3491 for ($i = 0; $i < sizeof($headlines); $i++
) {
3493 $line = $headlines[$i];
3495 array_push($affected_ids, $line["ref_id"]);
3497 $updated = smart_date_time(strtotime($line["last_updated"]));
3499 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3500 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3501 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3502 $tpl->setVariable('ARTICLE_UPDATED', $updated);
3503 $tpl->setVariable('ARTICLE_EXCERPT',
3504 truncate_string(strip_tags($line["excerpt"]), 100));
3506 $tpl->addBlock('article');
3508 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3509 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3510 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3511 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3512 // $tpl_t->setVariable('ARTICLE_EXCERPT',
3513 // truncate_string(strip_tags($line["excerpt"]), 100));
3515 $tpl_t->addBlock('article');
3517 if ($headlines[$i]['feed_title'] != $headlines[$i+
1]['feed_title']) {
3518 $tpl->addBlock('feed');
3519 $tpl_t->addBlock('feed');
3524 $tpl->addBlock('digest');
3525 $tpl->generateOutputToString($tmp);
3527 $tpl_t->addBlock('digest');
3528 $tpl_t->generateOutputToString($tmp_t);
3530 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
3533 function check_for_update($link, $brief_fmt = true) {
3534 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3536 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
3541 if (ENABLE_SIMPLEPIE
) {
3542 $rss = new SimplePie();
3543 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
3544 // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
3545 $rss->set_feed_url($fetch_url);
3546 $rss->set_output_encoding('UTF-8');
3549 $rss = fetch_rss($releases_feed);
3551 error_reporting (DEFAULT_ERROR_LEVEL
);
3555 if (ENABLE_SIMPLEPIE
) {
3556 $items = $rss->get_items();
3558 $items = $rss->items
;
3560 if (!$items ||
!is_array($items)) $items = $rss->entries
;
3561 if (!$items ||
!is_array($items)) $items = $rss;
3564 if (!is_array($items) ||
count($items) == 0) {
3568 $latest_item = $items[0];
3570 if (ENABLE_SIMPLEPIE
) {
3571 $last_title = $latest_item->get_title();
3573 $last_title = $latest_item["title"];
3576 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3578 if (ENABLE_SIMPLEPIE
) {
3579 $release_url = sanitize_rss($link, $latest_item->get_link());
3580 $content = sanitize_rss($link, $latest_item->get_description());
3582 $release_url = sanitize_rss($link, $latest_item["link"]);
3583 $content = sanitize_rss($link, $latest_item["description"]);
3586 if (version_compare(VERSION
, $latest_version) == -1) {
3588 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3589 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3590 <div id=\"milestoneDetails\">$content</div>");
3592 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3593 <div class='milestoneDetails'>$content</div>
3594 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3595 download and update information.";
3602 function markArticlesById($link, $ids, $cmode) {
3606 foreach ($ids as $id) {
3607 array_push($tmp_ids, "ref_id = '$id'");
3610 $ids_qpart = join(" OR ", $tmp_ids);
3613 db_query($link, "UPDATE ttrss_user_entries SET
3614 marked = false,last_read = NOW()
3615 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3616 } else if ($cmode == 1) {
3617 db_query($link, "UPDATE ttrss_user_entries SET
3619 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3621 db_query($link, "UPDATE ttrss_user_entries SET
3622 marked = NOT marked,last_read = NOW()
3623 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3627 function publishArticlesById($link, $ids, $cmode) {
3631 foreach ($ids as $id) {
3632 array_push($tmp_ids, "ref_id = '$id'");
3635 $ids_qpart = join(" OR ", $tmp_ids);
3638 db_query($link, "UPDATE ttrss_user_entries SET
3639 published = false,last_read = NOW()
3640 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3641 } else if ($cmode == 1) {
3642 db_query($link, "UPDATE ttrss_user_entries SET
3644 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3646 db_query($link, "UPDATE ttrss_user_entries SET
3647 published = NOT published,last_read = NOW()
3648 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3652 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3654 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3658 foreach ($ids as $id) {
3659 array_push($tmp_ids, "ref_id = '$id'");
3662 $ids_qpart = join(" OR ", $tmp_ids);
3665 db_query($link, "UPDATE ttrss_user_entries SET
3666 unread = false,last_read = NOW()
3667 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3668 } else if ($cmode == 1) {
3669 db_query($link, "UPDATE ttrss_user_entries SET
3671 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3673 db_query($link, "UPDATE ttrss_user_entries SET
3674 unread = NOT unread,last_read = NOW()
3675 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3679 function catchupArticleById($link, $id, $cmode) {
3682 db_query($link, "UPDATE ttrss_user_entries SET
3683 unread = false,last_read = NOW()
3684 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3685 } else if ($cmode == 1) {
3686 db_query($link, "UPDATE ttrss_user_entries SET
3688 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3690 db_query($link, "UPDATE ttrss_user_entries SET
3691 unread = NOT unread,last_read = NOW()
3692 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3696 function make_guid_from_title($title) {
3697 return preg_replace("/[ \"\',.:;]/", "-",
3698 mb_strtolower(strip_tags($title), 'utf-8'));
3701 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3702 $bottom = false, $rtl_content = false, $feed_id = 0,
3703 $is_cat = false, $search = false, $match_on = false,
3704 $search_mode = false, $offset = 0, $limit = 0) {
3706 $user_page_offset = $offset +
1;
3709 $class = "headlinesSubToolbar";
3710 $tid = "headlineActionsTop";
3712 $class = "headlinesSubToolbar";
3713 $tid = "headlineActionsBottom";
3716 print "<table class=\"$class\" id=\"$tid\"
3717 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3725 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3726 $page_next_link = "javascript:viewFeedGoPage(1)";
3727 $page_first_link = "javascript:viewFeedGoPage(0)";
3729 $catchup_page_link = "javascript:catchupPage()";
3730 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3731 $catchup_sel_link = "javascript:catchupSelection()";
3733 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3735 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3736 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3737 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3739 $tog_unread_link = "javascript:selectionToggleUnread()";
3740 $tog_marked_link = "javascript:selectionToggleMarked()";
3741 $tog_published_link = "javascript:selectionTogglePublished()";
3745 $sel_all_link = "javascript:cdmSelectArticles('all')";
3746 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3747 $sel_none_link = "javascript:cdmSelectArticles('none')";
3749 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3750 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3751 $tog_published_link = "javascript:selectionTogglePublished(true)";
3755 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3757 print "<td class=\"headlineActions$rtl_cpart\">
3758 <ul class=\"headlineDropdownMenu\">
3761 <a href=\"$sel_all_link\">".__('All')."</a>,
3762 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3763 <a href=\"$sel_none_link\">".__('None')."</a></li>
3764 <li class=\"vsep\"> </li>
3765 <li class=\"top\">".__('Toggle')."<ul>
3766 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3767 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3768 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3770 <li class=\"vsep\"> </li>
3771 <li class=\"top\">".__('Mark as read')."<ul>
3772 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
3773 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->";
3775 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3778 <li><span class=\"insensitive\">--------</span></li>
3779 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3780 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3781 <li><span class=\"insensitive\">--------</span></li>";
3785 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3788 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3790 if ($limit != 0 && !$search && $enable_pagination) {
3792 <li class=\"vsep\"> </li>
3793 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3794 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3795 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3799 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3801 <li class=\"vsep\"> </li>
3803 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3804 '$match_on', '$feed_id', '$is_cat');\">
3805 ".__('Convert to label')."</a></td>";
3811 // old style subtoolbar:
3813 print "<td class=\"headlineActions$rtl_cpart\">".
3815 <a href=\"$sel_all_link\">".__('All')."</a>,
3816 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3817 <a href=\"$sel_none_link\">".__('None')."</a>
3819 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3820 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3822 __('Mark as read:')."
3823 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3824 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3826 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3829 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3830 '$match_on', '$feed_id', '$is_cat');\">
3831 ".__('Convert to label')."</a>";
3838 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3839 print "<td class=\"headlineActions$rtl_cpart\">
3840 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3841 '$match_on', '$feed_id', '$is_cat');\">
3842 ".__('Convert to Label')."</a></td>";
3845 print "<td class=\"headlineTitle$rtl_cpart\">";
3847 print "<span class=\"headlineInnerTitle\">";
3849 if ($feed_site_url) {
3851 $target = "target=\"_new\"";
3853 print "<a $target href=\"$feed_site_url\">".
3854 truncate_string($feed_title,30)."</a>";
3860 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3863 if ($user_page_offset > 1) {
3864 print " [$user_page_offset] ";
3872 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3873 <img class=\"noborder\"
3874 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3879 print "</tr></table>";
3883 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3885 $tmp_category = getCategoryTitle($link, $cat_id);
3886 $cat_unread = getCategoryUnread($link, $cat_id);
3889 $holder_style = "display:none;";
3896 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3898 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3899 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3902 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3904 print "<span id=\"FCAP-$cat_id\">";
3907 print " <span id=\"FCATCTR-$cat_id\"
3908 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3918 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3921 function outputFeedList($link, $tags = false) {
3923 print "<ul class=\"feedList\" id=\"feedList\">";
3925 $owner_uid = $_SESSION["uid"];
3929 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3931 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
3934 $cat_hidden = false;
3937 # print "<li class=\"feedCat\">".__('Special')."</li>";
3938 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3939 # print "<li class=\"feedCat\">".
3940 # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3941 # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3943 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3944 # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3946 # $cat_unread = getCategoryUnread($link, -1);
3947 # $tmp_category = __("Special");
3948 # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3950 printCategoryHeader($link, -1, $cat_hidden, false);
3953 $num_starred = getFeedUnread($link, -1);
3954 $num_published = getFeedUnread($link, -2);
3955 $num_fresh = getFeedUnread($link, -3);
3959 if ($num_fresh > 0) $class .= "Unread";
3961 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3962 "images/fresh.png", $link);
3966 if ($num_starred > 0) $class .= "Unread";
3968 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
3971 $mark_img_ext = "gif";
3973 $mark_img_ext = "png";
3976 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3977 "images/mark_set.$mark_img_ext", $link);
3981 if ($num_published > 0) $class .= "Unread";
3983 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3984 "images/pub_set.gif", $link);
3986 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3992 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3994 $result = db_query($link, "SELECT id,sql_exp,description FROM
3995 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3997 if (db_num_rows($result) > 0) {
3998 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4000 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
4003 $cat_hidden = false;
4006 printCategoryHeader($link, -2, $cat_hidden, false);
4008 # print "<li class=\"feedCat\">".
4009 # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
4010 # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
4012 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
4014 print "<li><hr></li>";
4018 while ($line = db_fetch_assoc($result)) {
4020 error_reporting (0);
4022 $label_id = -$line['id'] - 11;
4023 $count = getFeedUnread($link, $label_id);
4031 error_reporting (DEFAULT_ERROR_LEVEL
);
4033 printFeedEntry($label_id,
4034 $class, $line["description"],
4035 $count, "images/label.png", $link);
4039 if (db_num_rows($result) > 0) {
4040 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4047 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
4048 print "<li><hr></li>";
4051 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4052 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4053 $order_by_qpart = "category,unread DESC,title";
4055 $order_by_qpart = "category,title";
4058 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4059 $order_by_qpart = "unread DESC,title";
4061 $order_by_qpart = "title";
4065 $age_qpart = getMaxAgeSubquery();
4067 $result = db_query($link, "SELECT ttrss_feeds.*,
4068 SUBSTRING(last_updated,1,19) AS last_updated_noms,
4069 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
4070 WHERE feed_id = ttrss_feeds.id AND unread = true
4072 AND ttrss_user_entries.ref_id = ttrss_entries.id
4073 AND owner_uid = '$owner_uid') as unread,
4075 ttrss_feed_categories.title AS category,
4076 ttrss_feed_categories.collapsed
4077 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4078 ON (ttrss_feed_categories.id = cat_id)
4080 ttrss_feeds.hidden = false AND
4081 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
4082 ORDER BY $order_by_qpart");
4084 $actid = $_GET["actid"];
4094 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4096 while ($line = db_fetch_assoc($result)) {
4098 $feed = trim($line["title"]);
4100 if (!$feed) $feed = "[Untitled]";
4102 $feed_id = $line["id"];
4104 $subop = $_GET["subop"];
4106 $unread = $line["unread"];
4108 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4109 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4111 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4114 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4117 $rtl_tag = "dir=\"RTL\"";
4122 $tmp_result = db_query($link,
4123 "SELECT id,COUNT(unread) AS unread
4124 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
4125 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
4126 WHERE parent_feed = '$feed_id' AND unread = true
4127 GROUP BY ttrss_feeds.id");
4129 if (db_num_rows($tmp_result) > 0) {
4130 while ($l = db_fetch_assoc($tmp_result)) {
4131 $unread +
= $l["unread"];
4135 $cat_id = $line["cat_id"];
4137 $tmp_category = $line["category"];
4139 if (!$tmp_category) {
4140 $tmp_category = __("Uncategorized");
4143 // $class = ($lnum % 2) ? "even" : "odd";
4145 if ($line["last_error"]) {
4151 if ($unread > 0) $class .= "Unread";
4153 if ($actid == $feed_id) {
4154 $class .= "Selected";
4157 $total_unread +
= $unread;
4159 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4165 $category = $tmp_category;
4167 $collapsed = $line["collapsed"];
4169 // workaround for NULL category
4170 if ($category == __("Uncategorized")) {
4171 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
4176 if ($collapsed == "t" ||
$collapsed == "1") {
4177 $holder_class = "feedCatHolder";
4178 $holder_style = "display:none;";
4181 $holder_class = "feedCatHolder";
4186 $cat_id = sprintf("%d", $cat_id);
4188 $cat_unread = getCategoryUnread($link, $cat_id);
4190 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
4192 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
4193 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
4194 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
4195 <span id=\"FCATCTR-$cat_id\"
4196 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
4199 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
4202 printFeedEntry($feed_id, $class, $feed, $unread,
4203 ICONS_URL
."/$feed_id.ico", $link, $rtl_content,
4204 $last_updated, $line["last_error"]);
4209 if (db_num_rows($result) == 0) {
4210 print "<li>".__('No feeds to display.')."</li>";
4217 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4218 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4219 post_int_id = ttrss_user_entries.int_id AND
4220 unread = true AND ref_id = ttrss_entries.id
4221 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4223 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4224 ORDER BY tag_name"); */
4226 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4227 print "<li class=\"feedCat\">".__('Tags')."</li>";
4228 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
4231 $age_qpart = getMaxAgeSubquery();
4233 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
4234 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4235 AND ref_id = id AND $age_qpart
4236 AND unread = true)) AS count FROM ttrss_tags
4237 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4238 ORDER BY count DESC LIMIT 50");
4242 while ($line = db_fetch_assoc($result)) {
4243 $tags[$line["tag_name"]] +
= $line["count"];
4246 foreach (array_keys($tags) as $tag) {
4248 $unread = $tags[$tag];
4256 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4260 if (db_num_rows($result) == 0) {
4261 print "<li>No tags to display.</li>";
4264 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4274 function get_article_tags($link, $id, $owner_uid = 0) {
4276 $a_id = db_escape_string($id);
4278 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4280 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
4281 owner_uid as owner FROM
4282 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
4283 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
4287 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4288 array_push($tags, $tmp_line["tag_name"]);
4294 function trim_value(&$value) {
4295 $value = trim($value);
4298 function trim_array($array) {
4300 array_walk($tmp, 'trim_value');
4304 function tag_is_valid($tag) {
4305 if ($tag == '') return false;
4306 if (preg_match("/^[0-9]*$/", $tag)) return false;
4308 $tag = iconv("utf-8", "utf-8", $tag);
4309 if (!$tag) return false;
4314 function render_login_form($link, $mobile = false) {
4316 require_once "login_form.php";
4318 require_once "mobile/login_form.php";
4322 // from http://developer.apple.com/internet/safari/faq.html
4323 function no_cache_incantation() {
4324 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4325 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4326 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4327 header("Cache-Control: post-check=0, pre-check=0", false);
4328 header("Pragma: no-cache"); // HTTP/1.0
4331 function format_warning($msg, $id = "") {
4332 return "<div class=\"warning\" id=\"$id\">
4333 <img src=\"images/sign_excl.gif\">$msg</div>";
4336 function format_notice($msg) {
4337 return "<div class=\"notice\">
4338 <img src=\"images/sign_info.gif\">$msg</div>";
4341 function format_error($msg) {
4342 return "<div class=\"error\">
4343 <img src=\"images/sign_excl.gif\">$msg</div>";
4346 function print_notice($msg) {
4347 return print format_notice($msg);
4350 function print_warning($msg) {
4351 return print format_warning($msg);
4354 function print_error($msg) {
4355 return print format_error($msg);
4359 function T_sprintf() {
4360 $args = func_get_args();
4361 return vsprintf(__(array_shift($args)), $args);
4364 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
4366 /* we can figure out feed_id from article id anyway, why do we
4367 * pass feed_id here? */
4369 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4370 WHERE ref_id = '$id'");
4372 $feed_id = db_fetch_result($result, 0, "feed_id");
4374 print "<article id='$id'><![CDATA[";
4376 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4377 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4379 if (db_num_rows($result) == 1) {
4380 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4382 $rtl_content = false;
4386 $rtl_tag = "dir=\"RTL\"";
4393 if ($mark_as_read) {
4394 $result = db_query($link, "UPDATE ttrss_user_entries
4395 SET unread = false,last_read = NOW()
4396 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4399 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
4400 SUBSTRING(updated,1,16) as updated,
4401 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4404 FROM ttrss_entries,ttrss_user_entries
4405 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4411 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4412 $link_target = "target=\"_new\"";
4415 $line = db_fetch_assoc($result);
4417 if ($line["icon_url"]) {
4418 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4420 $feed_icon = " ";
4423 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
4424 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4426 $entry_comments = "";
4429 $num_comments = $line["num_comments"];
4430 $entry_comments = "";
4432 if ($num_comments > 0) {
4433 if ($line["comments"]) {
4434 $comments_url = $line["comments"];
4436 $comments_url = $line["link"];
4438 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4440 if ($line["comments"] && $line["link"] != $line["comments"]) {
4441 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4445 print "<div class=\"postReply\">";
4447 print "<div class=\"postHeader\">";
4449 $entry_author = $line["author"];
4451 if ($entry_author) {
4452 $entry_author = __(" - by ") . $entry_author;
4455 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4456 strtotime($line["updated"]));
4458 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4460 if ($line["link"]) {
4461 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
4462 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
4464 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4467 /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
4468 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
4469 ORDER BY tag_name"); */
4471 $tags = get_article_tags($link, $id);
4478 if ($_SESSION["theme"] == "3pane") {
4484 foreach ($tags as $tag) {
4486 $tag_escaped = str_replace("'", "\\'", $tag);
4488 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4490 if ($num_tags == $tag_limit) {
4491 $tags_str .= "…";
4493 } else if ($num_tags < $tag_limit) {
4494 $tags_str .= $tag_str;
4496 $f_tags_str .= $tag_str;
4499 $tags_str = preg_replace("/, $/", "", $tags_str);
4500 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4502 $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4503 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4505 if (!$entry_comments) $entry_comments = " "; # placeholder
4507 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4509 print "<div style='float : right'>
4510 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4512 <a title=\"Edit tags for this article\"
4513 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4514 <div clear='both'>$entry_comments</div>";
4518 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4519 print "<div class=\"postContent\">";
4521 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4523 $line["content"] = sanitize_rss($link, $line["content"]);
4525 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4526 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4529 print $line["content"];
4531 $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4534 if (db_num_rows($result) > 0) {
4535 print "<div class=\"postEnclosures\">";
4537 if (db_num_rows($result) == 1) {
4538 print __("Attachment:") . " ";
4540 print __("Attachments:") . " ";
4545 while ($line = db_fetch_assoc($result)) {
4547 $url = $line["content_url"];
4549 $filename = substr($url, strrpos($url, "/")+
1);
4551 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4552 $filename . " (" . $line["content_type"] . ")" . "</a>";
4554 array_push($entries, $entry);
4557 print join(", ", $entries);
4568 print "]]></article>";
4572 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4573 $next_unread_feed, $offset) {
4575 $disable_cache = false;
4577 $timing_info = getmicrotime();
4579 $topmost_article_ids = array();
4585 if ($subop == "undefined") $subop = "";
4587 if ($subop == "CatchupSelected") {
4588 $ids = split(",", db_escape_string($_GET["ids"]));
4589 $cmode = sprintf("%d", $_GET["cmode"]);
4591 catchupArticlesById($link, $ids, $cmode);
4594 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4595 update_generic_feed($link, $feed, $cat_view, true);
4598 if ($subop == "MarkAllRead") {
4599 catchup_feed($link, $feed, $cat_view);
4601 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4602 if ($next_unread_feed) {
4603 $feed = $next_unread_feed;
4609 $result = db_query($link,
4610 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4612 if (db_num_rows($result) == 0) {
4613 print "<div align='center'>".__('Feed not found.')."</div>";
4618 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4620 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4621 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4623 if (db_num_rows($result) == 1) {
4624 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4626 $rtl_content = false;
4630 $rtl_tag = "dir=\"RTL\"";
4636 $rtl_content = false;
4639 $script_dt_add = get_script_dt_add();
4641 /// START /////////////////////////////////////////////////////////////////////////////////
4643 $search = db_escape_string($_GET["query"]);
4646 $disable_cache = true;
4649 $search_mode = db_escape_string($_GET["search_mode"]);
4650 $match_on = db_escape_string($_GET["match_on"]);
4656 $real_offset = $offset * $limit;
4658 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4660 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4661 $search, $search_mode, $match_on, false, $real_offset);
4663 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4665 $result = $qfh_ret[0];
4666 $feed_title = $qfh_ret[1];
4667 $feed_site_url = $qfh_ret[2];
4668 $last_error = $qfh_ret[3];
4671 $feed_site_url = article_publish_url($link);
4674 /// STOP //////////////////////////////////////////////////////////////////////////////////
4677 print "<div id=\"headlinesContainer\" $rtl_tag>";
4680 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4684 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4685 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4688 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4691 $headlines_count = db_num_rows($result);
4693 if (db_num_rows($result) > 0) {
4695 # print "\{$offset}";
4697 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4698 print "<table class=\"headlinesList\" id=\"headlinesList\"
4699 cellspacing=\"0\">";
4702 $lnum = $limit*$offset;
4704 error_reporting (DEFAULT_ERROR_LEVEL
);
4708 while ($line = db_fetch_assoc($result)) {
4710 $class = ($lnum %
2) ?
"even" : "odd";
4713 $feed_id = $line["feed_id"];
4715 if (count($topmost_article_ids) < 5) {
4716 array_push($topmost_article_ids, $id);
4719 if ($line["last_read"] == "" &&
4720 ($line["unread"] != "t" && $line["unread"] != "1")) {
4722 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4725 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4729 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
4737 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4740 $mark_img_ext = "gif";
4742 $mark_img_ext = "png";
4745 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
4746 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
4748 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
4750 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
4752 alt=\"Star article\" onclick='javascript:tMark($id)'>";
4755 if ($line["published"] == "t" ||
$line["published"] == "1") {
4756 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
4758 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
4760 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
4762 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
4765 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4766 # $line["title"] . "</a>";
4768 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4769 $line["title"] . "</a>";
4771 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4772 # $line["title"] . "</a>";
4774 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4775 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
4777 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4778 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
4781 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4782 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4786 $entry_author = $line["author"];
4788 if ($entry_author) {
4789 $entry_author = " - by $entry_author";
4792 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4794 print "<tr class='$class' id='RROW-$id'>";
4796 print "<td class='hlUpdPic'>$update_pic</td>";
4798 print "<td class='hlSelectRow'>
4799 <input type=\"checkbox\" onclick=\"tSR(this)\"
4803 print "<td class='hlMarkedPic'>$marked_pic</td>";
4804 print "<td class='hlMarkedPic'>$published_pic</td>";
4806 # if ($line["feed_title"]) {
4807 # print "<td class='hlContent'>$content_link</td>";
4808 # print "<td class='hlFeed'>
4809 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4810 # truncate_string($line["feed_title"],30)."</a> </td>";
4813 print "<td class='hlContent' valign='middle'>";
4815 print "<a href=\"javascript:view($id,$feed_id);\">" .
4818 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4819 if ($content_preview) {
4820 print "<span class=\"contentPreview\"> - $content_preview</span>";
4826 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4827 # $line["feed_title"]."</a>
4829 if ($line["feed_title"]) {
4830 print "<span class=\"hlFeed\">
4831 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
4832 $line["feed_title"]."</a>)
4841 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
4848 $add_class = "Unread";
4853 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
4858 $cdm_cstyle = "style=\"display : none\"";
4861 print "<div class=\"cdmArticle$add_class\"
4862 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4863 onmouseout='cdmMouseOut(this)'>";
4865 print "<div class=\"cdmHeader\">";
4867 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4869 print "<a class=\"title\"
4870 onclick=\"javascript:toggleUnread($id, 0)\"
4871 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4873 print $entry_author;
4876 print " <a id=\"CICH-$id\"
4877 href=\"javascript:cdmExpandArticle($id)\">
4878 (".__('Show article').")</a>";
4882 if ($line["feed_title"]) {
4883 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4888 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4889 $line["content_preview"] = preg_replace("/href=/i",
4890 "target=\"_new\" href=", $line["content_preview"]);
4893 print "<div class=\"cdmContent\" id=\"CICD-$id\" $cdm_cstyle>";
4895 // print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
4896 print $line["content_preview"];
4898 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4901 if (db_num_rows($e_result) > 0) {
4902 print "<div class=\"cdmEnclosures\">";
4904 if (db_num_rows($e_result) == 1) {
4905 print __("Attachment:") . " ";
4907 print __("Attachments:") . " ";
4912 while ($e_line = db_fetch_assoc($e_result)) {
4914 $url = $e_line["content_url"];
4916 $filename = substr($url, strrpos($url, "/")+
1);
4918 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4919 $filename . " (" . $e_line["content_type"] . ")" . "</a>";
4921 array_push($entries, $entry);
4924 print join(", ", $entries);
4929 print "<br clear='both'>";
4932 /* if (!$expand_cdm) {
4933 print "<a id=\"CICH-$id\"
4934 href=\"javascript:cdmExpandArticle($id)\">
4940 print "<div class=\"cdmFooter\"><span class='s0'>";
4942 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4944 print __("Select:").
4945 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4946 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4948 print "</span><span class='s1'>$marked_pic</span> ";
4949 print "<span class='s1'>$published_pic</span> ";
4951 $tags = get_article_tags($link, $id);
4954 $full_tags_str = "";
4957 foreach ($tags as $tag) {
4959 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4960 if ($num_tags < 5) {
4961 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4962 } else if ($num_tags == 5) {
4963 $tags_str .= "…";
4967 $tags_str = preg_replace("/, $/", "", $tags_str);
4968 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4970 $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4972 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4975 if ($tags_str == "") $tags_str = "no tags";
4977 // print "<img src='images/tag.png' class='markedPic'>";
4979 print "<span class='s1'>
4980 <img class='tagsPic' src='images/tag.png' alt='Tags'
4981 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
4982 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4986 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4987 href=\"javascript:toggleUnread($id)\">
4998 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
5002 // print_headline_subtoolbar($link,
5003 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
5007 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
5015 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache);
5018 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
5020 function printTagCloud($link) {
5022 /* get first ref_id to count from */
5028 if (DB_TYPE == "pgsql") {
5029 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
5030 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5031 AND date_entered > NOW() - INTERVAL '30 days'";
5033 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
5034 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5035 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
5038 $result = db_query($link, $query);
5039 $first_id = db_fetch_result($result, 0, "id"); */
5041 //AND post_int_id >= '$first_id'
5042 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5043 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
5044 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
5046 $result = db_query($link, $query);
5050 while ($line = db_fetch_assoc($result)) {
5051 $tags[$line["tag_name"]] = $line["count"];
5056 $max_size = 32; // max font size in pixels
5057 $min_size = 11; // min font size in pixels
5059 // largest and smallest array values
5060 $max_qty = max(array_values($tags));
5061 $min_qty = min(array_values($tags));
5063 // find the range of values
5064 $spread = $max_qty - $min_qty;
5065 if ($spread == 0) { // we don't want to divide by zero
5069 // set the font-size increment
5070 $step = ($max_size - $min_size) / ($spread);
5072 // loop through the tag array
5073 foreach ($tags as $key => $value) {
5074 // calculate font-size
5075 // find the $value in excess of $min_qty
5076 // multiply by the font-size increment ($size)
5077 // and add the $min_size set above
5078 $size = round($min_size +
(($value - $min_qty) * $step));
5080 $key_escaped = str_replace("'", "\\'", $key);
5082 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
5083 $size . "px\" title=\"$value articles tagged with " .
5084 $key . '">' . $key . '</a> ';
5088 function print_checkpoint($n, $s) {
5089 $ts = getmicrotime();
5090 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5094 function sanitize_tag($tag) {
5097 $tag = mb_strtolower($tag, 'utf-8');
5099 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5101 // $tag = str_replace('"', "", $tag);
5102 // $tag = str_replace("+", " ", $tag);
5103 $tag = str_replace("technorati tag: ", "", $tag);
5108 function generate_publish_key() {
5109 return sha1(uniqid(rand(), true));
5112 function article_publish_url($link) {
5114 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
5116 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
5121 function clear_feed_articles($link, $id) {
5122 $result = db_query($link, "DELETE FROM ttrss_user_entries
5123 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
5125 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5126 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
5129 function add_feed_url() {
5130 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
5131 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5135 function encrypt_password($pass, $login = '') {
5137 return "SHA1X:" . sha1("$login:$pass");
5139 return "SHA1:" . sha1($pass);