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 if (ENABLE_SIMPLEPIE
) {
72 require_once "simplepie/simplepie.inc";
74 require_once "magpierss/rss_fetch.inc";
75 require_once 'magpierss/rss_utils.inc';
78 function _debug($msg) {
79 $ts = strftime("%H:%M:%S", time());
83 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
85 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
89 $result = db_query($link,
90 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
94 if (db_num_rows($result) == 1) {
95 $owner_uid = db_fetch_result($result, 0, "owner_uid");
98 if (!$owner_uid) return;
100 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
103 if (!$purge_unread) $query_limit = " unread = false AND ";
105 if (DB_TYPE
== "pgsql") {
106 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
107 marked = false AND feed_id = '$feed_id' AND
108 (SELECT date_entered FROM ttrss_entries WHERE
109 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
111 $pg_version = get_pgsql_version($link);
113 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
115 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
116 ttrss_entries.id = ref_id AND
118 feed_id = '$feed_id' AND
120 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
124 $result = db_query($link, "DELETE FROM ttrss_user_entries
126 WHERE ttrss_entries.id = ref_id AND
128 feed_id = '$feed_id' AND
130 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
133 $rows = pg_affected_rows($result);
137 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
138 marked = false AND feed_id = '$feed_id' AND
139 (SELECT date_entered FROM ttrss_entries WHERE
140 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
142 $result = db_query($link, "DELETE FROM ttrss_user_entries
143 USING ttrss_user_entries, ttrss_entries
144 WHERE ttrss_entries.id = ref_id AND
146 feed_id = '$feed_id' AND
148 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
150 $rows = mysql_affected_rows($link);
155 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
159 function global_purge_old_posts($link, $do_output = false, $limit = false) {
161 $random_qpart = sql_random_function();
164 $limit_qpart = "LIMIT $limit";
169 $result = db_query($link,
170 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
171 ORDER BY $random_qpart $limit_qpart");
173 while ($line = db_fetch_assoc($result)) {
175 $feed_id = $line["id"];
176 $purge_interval = $line["purge_interval"];
177 $owner_uid = $line["owner_uid"];
179 if ($purge_interval == 0) {
181 $tmp_result = db_query($link,
182 "SELECT value FROM ttrss_user_prefs WHERE
183 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
185 if (db_num_rows($tmp_result) != 0) {
186 $purge_interval = db_fetch_result($tmp_result, 0, "value");
191 // print "Feed $feed_id: purge interval = $purge_interval\n";
194 if ($purge_interval > 0) {
195 purge_feed($link, $feed_id, $purge_interval, $do_output);
199 // purge orphaned posts in main content table
200 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
201 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
204 $rows = db_affected_rows($link, $result);
205 _debug("Purged $rows orphaned posts.");
210 function feed_purge_interval($link, $feed_id) {
212 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
213 WHERE id = '$feed_id'");
215 if (db_num_rows($result) == 1) {
216 $purge_interval = db_fetch_result($result, 0, "purge_interval");
217 $owner_uid = db_fetch_result($result, 0, "owner_uid");
219 if ($purge_interval == 0) $purge_interval = get_pref($link,
220 'PURGE_OLD_DAYS', $user_id);
222 return $purge_interval;
229 function purge_old_posts($link) {
231 $user_id = $_SESSION["uid"];
233 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
234 WHERE owner_uid = '$user_id'");
236 while ($line = db_fetch_assoc($result)) {
238 $feed_id = $line["id"];
239 $purge_interval = $line["purge_interval"];
241 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
243 if ($purge_interval > 0) {
244 purge_feed($link, $feed_id, $purge_interval);
248 // purge orphaned posts in main content table
249 db_query($link, "DELETE FROM ttrss_entries WHERE
250 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
253 function get_feed_update_interval($link, $feed_id) {
254 $result = db_query($link, "SELECT owner_uid, update_interval FROM
255 ttrss_feeds WHERE id = '$feed_id'");
257 if (db_num_rows($result) == 1) {
258 $update_interval = db_fetch_result($result, 0, "update_interval");
259 $owner_uid = db_fetch_result($result, 0, "owner_uid");
261 if ($update_interval != 0) {
262 return $update_interval;
264 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
272 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
274 if (WEB_DEMO_MODE
) return;
277 $user_id = $_SESSION["uid"];
278 purge_old_posts($link);
281 // db_query($link, "BEGIN");
283 if (MAX_UPDATE_TIME
> 0) {
284 if (DB_TYPE
== "mysql") {
287 $q_order = "RANDOM()";
290 $q_order = "last_updated DESC";
293 $result = db_query($link, "SELECT feed_url,id,
294 SUBSTRING(last_updated,1,19) AS last_updated,
295 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
300 while ($line = db_fetch_assoc($result)) {
301 $upd_intl = $line["update_interval"];
303 if (!$upd_intl ||
$upd_intl == 0) {
304 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
308 // Updates for this feed are disabled
312 if ($fetch ||
(!$line["last_updated"] ||
313 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
315 // print "<!-- feed: ".$line["feed_url"]." -->";
317 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
319 $upd_elapsed = time() - $upd_start;
321 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
327 // db_query($link, "COMMIT");
331 function fetch_file_contents($url) {
332 if (USE_CURL_FOR_ICONS
) {
333 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
335 $ch = curl_init($url);
336 $fp = fopen($tmpfile, "w");
339 curl_setopt($ch, CURLOPT_FILE
, $fp);
340 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT
, 15);
341 curl_setopt($ch, CURLOPT_TIMEOUT
, 45);
347 $contents = file_get_contents($tmpfile);
353 return file_get_contents($url);
358 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
359 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
361 function get_favicon_url($url) {
363 if ($html = @fetch_file_contents
($url)) {
365 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
366 // Attempt to grab a favicon link from their webpage url
367 $linkUrl = html_entity_decode($matches[1]);
369 if (substr($linkUrl, 0, 1) == '/') {
370 $urlParts = parse_url($url);
371 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
372 } else if (substr($linkUrl, 0, 7) == 'http://') {
373 $faviconURL = $linkUrl;
374 } else if (substr($url, -1, 1) == '/') {
375 $faviconURL = $url.$linkUrl;
377 $faviconURL = $url.'/'.$linkUrl;
381 // If unsuccessful, attempt to "guess" the favicon location
382 $urlParts = parse_url($url);
383 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
387 // Run a test to see if what we have attempted to get actually exists.
388 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
395 function url_validate($link) {
397 $url_parts = @parse_url
($link);
399 if ( empty( $url_parts["host"] ) )
402 if ( !empty( $url_parts["path"] ) ) {
403 $documentpath = $url_parts["path"];
408 if ( !empty( $url_parts["query"] ) )
409 $documentpath .= "?" . $url_parts["query"];
411 $host = $url_parts["host"];
412 $port = $url_parts["port"];
417 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
422 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
424 $http_response = fgets( $socket, 22 );
426 $responses = "/(200 OK)|(30[0-9] Moved)/";
427 if ( preg_match($responses, $http_response) ) {
436 function check_feed_favicon($site_url, $feed, $link) {
437 $favicon_url = get_favicon_url($site_url);
439 # print "FAVICON [$site_url]: $favicon_url\n";
443 $icon_file = ICONS_DIR
. "/$feed.ico";
445 if ($favicon_url && !file_exists($icon_file)) {
446 $contents = fetch_file_contents($favicon_url);
448 $fp = fopen($icon_file, "w");
451 fwrite($fp, $contents);
453 chmod($icon_file, 0644);
457 error_reporting(DEFAULT_ERROR_LEVEL
);
461 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
463 if (!$_GET["daemon"] && !$ignore_daemon) {
467 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
468 _debug("update_rss_feed: start");
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
479 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
481 if (db_num_rows($result) == 0) {
482 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
483 _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
488 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
489 WHERE id = '$feed'");
491 $auth_login = db_fetch_result($result, 0, "auth_login");
492 $auth_pass = db_fetch_result($result, 0, "auth_pass");
494 if (!ENABLE_SIMPLEPIE
) {
495 $auth_login = urlencode($auth_login);
496 $auth_pass = urlencode($auth_pass);
499 $update_interval = db_fetch_result($result, 0, "update_interval");
500 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
502 if ($update_interval < 0) { return; }
504 $feed = db_escape_string($feed);
506 $fetch_url = $feed_url;
508 if ($auth_login && $auth_pass) {
509 $url_parts = array();
510 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
512 if ($url_parts[1] && $url_parts[2]) {
513 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
518 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
519 _debug("update_rss_feed: fetching [$fetch_url]...");
522 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
526 if (!ENABLE_SIMPLEPIE
) {
527 $rss = fetch_rss($fetch_url);
529 if (!is_dir(SIMPLEPIE_CACHE_DIR
)) {
530 mkdir(SIMPLEPIE_CACHE_DIR
);
533 $rss = new SimplePie();
534 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
535 // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
536 $rss->set_feed_url($fetch_url);
537 $rss->set_output_encoding('UTF-8');
539 if (SIMPLEPIE_CACHE_IMAGES
&& $cache_images) {
540 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
541 _debug("enabling image cache");
544 $rss->set_image_handler('./image.php', 'i');
547 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
548 _debug("feed update interval (sec): " .
549 get_feed_update_interval($link, $feed)*60);
552 if (is_dir(SIMPLEPIE_CACHE_DIR
)) {
553 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR
);
554 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
562 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
563 _debug("update_rss_feed: fetch done, parsing...");
565 error_reporting (DEFAULT_ERROR_LEVEL
);
568 $feed = db_escape_string($feed);
570 if (ENABLE_SIMPLEPIE
) {
571 $fetch_ok = !$rss->error();
578 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
579 _debug("update_rss_feed: processing feed data...");
582 // db_query($link, "BEGIN");
584 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
585 FROM ttrss_feeds WHERE id = '$feed'");
587 $registered_title = db_fetch_result($result, 0, "title");
588 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
589 $orig_site_url = db_fetch_result($result, 0, "site_url");
591 $owner_uid = db_fetch_result($result, 0, "owner_uid");
593 if (ENABLE_SIMPLEPIE
) {
594 $site_url = $rss->get_link();
596 $site_url = $rss->channel
["link"];
599 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
600 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
601 _debug("update_rss_feed: checking favicon...");
604 check_feed_favicon($site_url, $feed, $link);
607 if (!$registered_title ||
$registered_title == "[Unknown]") {
609 if (ENABLE_SIMPLEPIE
) {
610 $feed_title = db_escape_string($rss->get_title());
612 $feed_title = db_escape_string($rss->channel
["title"]);
615 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
616 _debug("update_rss_feed: registering title: $feed_title");
619 db_query($link, "UPDATE ttrss_feeds SET
620 title = '$feed_title' WHERE id = '$feed'");
623 // weird, weird Magpie
624 if (!ENABLE_SIMPLEPIE
) {
625 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
628 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
629 db_query($link, "UPDATE ttrss_feeds SET
630 site_url = '$site_url' WHERE id = '$feed'");
633 // print "I: " . $rss->channel["image"]["url"];
635 if (!ENABLE_SIMPLEPIE
) {
636 $icon_url = $rss->image
["url"];
638 $icon_url = $rss->get_image_url();
641 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
642 $icon_url = db_escape_string($icon_url);
643 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
646 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
647 _debug("update_rss_feed: loading filters...");
652 $result = db_query($link, "SELECT reg_exp,
653 ttrss_filter_types.name AS name,
654 ttrss_filter_actions.name AS action,
657 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
659 owner_uid = $owner_uid AND
660 ttrss_filter_types.id = filter_type AND
661 ttrss_filter_actions.id = action_id AND
662 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
664 while ($line = db_fetch_assoc($result)) {
665 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
667 $filter["reg_exp"] = $line["reg_exp"];
668 $filter["action"] = $line["action"];
669 $filter["action_param"] = $line["action_param"];
670 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
672 array_push($filters[$line["name"]], $filter);
675 if (ENABLE_SIMPLEPIE
) {
676 $iterator = $rss->get_items();
678 $iterator = $rss->items
;
679 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
680 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
683 if (!is_array($iterator)) {
684 /* db_query($link, "UPDATE ttrss_feeds
685 SET last_error = 'Parse error: can\'t find any articles.'
686 WHERE id = '$feed'"); */
688 // clear any errors and mark feed as updated if fetched okay
689 // even if it's blank
691 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
692 _debug("update_rss_feed: entry iterator is not an array, no articles?");
695 db_query($link, "UPDATE ttrss_feeds
696 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
698 return; // no articles
701 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
702 _debug("update_rss_feed: processing articles...");
705 foreach ($iterator as $item) {
707 if ($_GET['xdebug']) {
712 if (ENABLE_SIMPLEPIE
) {
713 $entry_guid = $item->get_id();
714 if (!$entry_guid) $entry_guid = $item->get_link();
715 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
719 $entry_guid = $item["id"];
721 if (!$entry_guid) $entry_guid = $item["guid"];
722 if (!$entry_guid) $entry_guid = $item["link"];
723 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
726 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
727 _debug("update_rss_feed: guid $entry_guid");
730 if (!$entry_guid) continue;
732 $entry_timestamp = "";
734 if (ENABLE_SIMPLEPIE
) {
735 $entry_timestamp = strtotime($item->get_date());
737 $rss_2_date = $item['pubdate'];
738 $rss_1_date = $item['dc']['date'];
739 $atom_date = $item['issued'];
740 if (!$atom_date) $atom_date = $item['updated'];
742 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
743 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
744 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
747 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
748 _debug("update_rss_feed: date $entry_timestamp");
751 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
752 $entry_timestamp = time();
753 $no_orig_date = 'true';
755 $no_orig_date = 'false';
758 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
760 if (ENABLE_SIMPLEPIE
) {
761 $entry_title = $item->get_title();
763 $entry_title = trim(strip_tags($item["title"]));
766 if (ENABLE_SIMPLEPIE
) {
767 $entry_link = $item->get_link();
769 // strange Magpie workaround
770 $entry_link = $item["link_"];
771 if (!$entry_link) $entry_link = $item["link"];
774 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
775 _debug("update_rss_feed: title $entry_title");
778 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
780 $entry_link = strip_tags($entry_link);
782 if (ENABLE_SIMPLEPIE
) {
783 $entry_content = $item->get_description();
785 $entry_content = $item["content:escaped"];
787 if (!$entry_content) $entry_content = $item["content:encoded"];
788 if (!$entry_content) $entry_content = $item["content"];
790 // Magpie bugs are getting ridiculous
791 if (trim($entry_content) == "Array") $entry_content = false;
793 if (!$entry_content) $entry_content = $item["atom_content"];
794 if (!$entry_content) $entry_content = $item["summary"];
795 if (!$entry_content) $entry_content = $item["description"];
798 if (is_array($entry_content)) {
799 $entry_content = $entry_content["encoded"];
800 if (!$entry_content) $entry_content = $entry_content["escaped"];
804 if ($_GET["xdebug"]) {
805 print "update_rss_feed: content: ";
806 print_r(htmlspecialchars($entry_content));
809 $entry_content_unescaped = $entry_content;
811 if (ENABLE_SIMPLEPIE
) {
812 $entry_comments = strip_tags($item->data
["comments"]);
813 if ($item->get_author()) {
814 $entry_author_item = $item->get_author();
815 $entry_author = $entry_author_item->get_name();
818 $entry_comments = strip_tags($item["comments"]);
820 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
822 if ($item['author']) {
824 if (is_array($item['author'])) {
826 if (!$entry_author) {
827 $entry_author = db_escape_string(strip_tags($item['author']['name']));
830 if (!$entry_author) {
831 $entry_author = db_escape_string(strip_tags($item['author']['email']));
835 if (!$entry_author) {
836 $entry_author = db_escape_string(strip_tags($item['author']));
841 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
843 $entry_guid = db_escape_string(strip_tags($entry_guid));
844 $entry_guid = mb_substr($entry_guid, 0, 250);
846 $result = db_query($link, "SELECT id FROM ttrss_entries
847 WHERE guid = '$entry_guid'");
849 $entry_content = db_escape_string($entry_content);
851 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
853 $entry_title = db_escape_string($entry_title);
854 $entry_link = db_escape_string($entry_link);
855 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
856 $entry_author = mb_substr($entry_author, 0, 250);
858 if (ENABLE_SIMPLEPIE
) {
859 $num_comments = 0; #FIXME#
861 $num_comments = db_escape_string($item["slash"]["comments"]);
864 if (!$num_comments) $num_comments = 0;
866 // parse <category> entries into tags
868 if (ENABLE_SIMPLEPIE
) {
870 $additional_tags = array();
871 $additional_tags_src = $item->get_categories();
873 if (is_array($additional_tags_src)) {
874 foreach ($additional_tags_src as $tobj) {
875 array_push($additional_tags, $tobj->get_term());
879 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
880 _debug("update_rss_feed: category tags:");
881 print_r($additional_tags);
886 $t_ctr = $item['category#'];
888 $additional_tags = false;
891 $additional_tags = false;
892 } else if ($t_ctr > 0) {
893 $additional_tags = array($item['category']);
895 if ($item['category@term']) {
896 array_push($additional_tags, $item['category@term']);
899 for ($i = 0; $i <= $t_ctr; $i++
) {
900 if ($item["category#$i"]) {
901 array_push($additional_tags, $item["category#$i"]);
904 if ($item["category#$i@term"]) {
905 array_push($additional_tags, $item["category#$i@term"]);
910 // parse <dc:subject> elements
912 $t_ctr = $item['dc']['subject#'];
915 $additional_tags = array($item['dc']['subject']);
917 for ($i = 0; $i <= $t_ctr; $i++
) {
918 if ($item['dc']["subject#$i"]) {
919 array_push($additional_tags, $item['dc']["subject#$i"]);
927 $enclosures = array();
929 if (ENABLE_SIMPLEPIE
) {
930 $encs = $item->get_enclosures();
932 if (is_array($encs)) {
933 foreach ($encs as $e) {
935 $e->link
, $e->type
, $e->length
);
937 array_push($enclosures, $e_item);
942 $e_ctr = $item['enclosure#'];
945 $e_item = array($item['enclosure@url'],
946 $item['enclosure@type'],
947 $item['enclosure@length']);
949 array_push($enclosures, $e_item);
951 for ($i = 0; $i <= $e_ctr; $i++
) {
953 if ($item["enclosure#$i@url"]) {
954 $e_item = array($item["enclosure#$i@url"],
955 $item["enclosure#$i@type"],
956 $item["enclosure#$i@length"]);
957 array_push($enclosures, $e_item);
966 // $entry_content = sanitize_rss($entry_content);
968 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
969 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
972 db_query($link, "BEGIN");
974 if (db_num_rows($result) == 0) {
976 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
977 _debug("update_rss_feed: base guid not found");
980 // base post entry does not exist, create it
982 $result = db_query($link,
983 "INSERT INTO ttrss_entries
999 '$entry_timestamp_fmt',
1008 // we keep encountering the entry in feeds, so we need to
1009 // update date_entered column so that we don't get horrible
1010 // dupes when the entry gets purged and reinserted again e.g.
1011 // in the case of SLOW SLOW OMG SLOW updating feeds
1013 $base_entry_id = db_fetch_result($result, 0, "id");
1015 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
1016 WHERE id = '$base_entry_id'");
1019 // now it should exist, if not - bad luck then
1021 $result = db_query($link, "SELECT
1022 id,content_hash,no_orig_date,title,
1023 substring(date_entered,1,19) as date_entered,
1024 substring(updated,1,19) as updated,
1028 WHERE guid = '$entry_guid'");
1033 if (db_num_rows($result) == 1) {
1035 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1036 _debug("update_rss_feed: base guid found, checking for user record");
1039 // this will be used below in update handler
1040 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1041 $orig_title = db_fetch_result($result, 0, "title");
1042 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
1043 $orig_date_entered = strtotime(db_fetch_result($result,
1044 0, "date_entered"));
1046 $ref_id = db_fetch_result($result, 0, "id");
1047 $entry_ref_id = $ref_id;
1049 // check for user post link to main table
1051 // do we allow duplicate posts with same GUID in different feeds?
1052 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
1053 $dupcheck_qpart = "AND feed_id = '$feed'";
1055 $dupcheck_qpart = "";
1058 // error_reporting(0);
1060 $article_filters = get_article_filters($filters, $entry_title,
1061 $entry_content, $entry_link);
1063 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1064 _debug("update_rss_feed: article filters: ");
1065 if (count($article_filters) != 0) {
1066 print_r($article_filters);
1070 if (find_article_filter($article_filters, "filter")) {
1074 // error_reporting (DEFAULT_ERROR_LEVEL);
1076 $result = db_query($link,
1077 "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
1078 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
1081 // okay it doesn't exist - create user entry
1082 if (db_num_rows($result) == 0) {
1084 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1085 _debug("update_rss_feed: user record not found, creating...");
1088 if (!find_article_filter($article_filters, 'catchup')) {
1090 $last_read_qpart = 'NULL';
1093 $last_read_qpart = 'NOW()';
1096 if (find_article_filter($article_filters, 'mark')) {
1102 if (find_article_filter($article_filters, 'publish')) {
1103 $published = 'true';
1105 $published = 'false';
1108 $result = db_query($link,
1109 "INSERT INTO ttrss_user_entries
1110 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
1111 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
1112 $last_read_qpart, $marked, $published)");
1114 $result = db_query($link,
1115 "SELECT int_id FROM ttrss_user_entries WHERE
1116 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1117 feed_id = '$feed' LIMIT 1");
1119 if (db_num_rows($result) == 1) {
1120 $entry_int_id = db_fetch_result($result, 0, "int_id");
1123 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1124 $entry_int_id = db_fetch_result($result, 0, "int_id");
1127 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1128 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1131 $post_needs_update = false;
1133 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
1134 ($content_hash != $orig_content_hash)) {
1135 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
1136 $post_needs_update = true;
1139 if (db_escape_string($orig_title) != $entry_title) {
1140 $post_needs_update = true;
1143 if ($orig_num_comments != $num_comments) {
1144 $post_needs_update = true;
1147 // this doesn't seem to be very reliable
1149 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1150 // $post_needs_update = true;
1153 // if post needs update, update it and mark all user entries
1154 // linking to this post as updated
1155 if ($post_needs_update) {
1157 if (defined('DAEMON_EXTENDED_DEBUG')) {
1158 _debug("update_rss_feed: post $entry_guid needs update...");
1161 // print "<!-- post $orig_title needs update : $post_needs_update -->";
1163 db_query($link, "UPDATE ttrss_entries
1164 SET title = '$entry_title', content = '$entry_content',
1165 content_hash = '$content_hash',
1166 num_comments = '$num_comments'
1167 WHERE id = '$ref_id'");
1169 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
1170 db_query($link, "UPDATE ttrss_user_entries
1171 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1173 db_query($link, "UPDATE ttrss_user_entries
1174 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1180 db_query($link, "COMMIT");
1182 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1183 _debug("update_rss_feed: looking for enclosures...");
1186 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1187 print_r($enclosures);
1190 db_query($link, "BEGIN");
1192 foreach ($enclosures as $enc) {
1193 $enc_url = db_escape_string($enc[0]);
1194 $enc_type = db_escape_string($enc[1]);
1195 $enc_dur = db_escape_string($enc[2]);
1197 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1198 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1200 if (db_num_rows($result) == 0) {
1201 db_query($link, "INSERT INTO ttrss_enclosures
1202 (content_url, content_type, title, duration, post_id) VALUES
1203 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1207 db_query($link, "COMMIT");
1209 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1210 _debug("update_rss_feed: looking for tags...");
1214 // <a href="..." rel="tag">Xorg</a>, //
1218 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
1219 $entry_content_unescaped, $entry_tags);
1221 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1222 print_r($entry_tags);
1223 print "<br/></p>"; */
1225 $entry_tags = $entry_tags[1];
1227 # check for manual tags
1229 $tag_filter = find_article_filter($article_filters, "tag");
1233 $manual_tags = trim_array(split(",", $tag_filter[1]));
1235 foreach ($manual_tags as $tag) {
1236 if (tag_is_valid($tag)) {
1237 array_push($entry_tags, $tag);
1242 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1243 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1245 if ($additional_tags && is_array($additional_tags)) {
1246 foreach ($additional_tags as $tag) {
1247 if (tag_is_valid($tag) &&
1248 array_search($tag, $boring_tags) === FALSE) {
1249 array_push($entry_tags, $tag);
1254 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1256 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1257 print_r($entry_tags);
1260 if (count($entry_tags) > 0) {
1262 db_query($link, "BEGIN");
1264 foreach ($entry_tags as $tag) {
1266 $tag = sanitize_tag($tag);
1267 $tag = db_escape_string($tag);
1269 if (!tag_is_valid($tag)) continue;
1271 $result = db_query($link, "SELECT id FROM ttrss_tags
1272 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1273 owner_uid = '$owner_uid' LIMIT 1");
1275 // print db_fetch_result($result, 0, "id");
1277 if ($result && db_num_rows($result) == 0) {
1279 db_query($link, "INSERT INTO ttrss_tags
1280 (owner_uid,tag_name,post_int_id)
1281 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1285 db_query($link, "COMMIT");
1288 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1289 _debug("update_rss_feed: article processed");
1293 db_query($link, "UPDATE ttrss_feeds
1294 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1296 // db_query($link, "COMMIT");
1300 if (ENABLE_SIMPLEPIE
) {
1301 $error_msg = mb_substr($rss->error(), 0, 250);
1303 $error_msg = mb_substr(magpie_error(), 0, 250);
1306 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1307 _debug("update_rss_feed: error fetching feed: $error_msg");
1310 $error_msg = db_escape_string($error_msg);
1313 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1314 last_updated = NOW() WHERE id = '$feed'");
1317 if (ENABLE_SIMPLEPIE
) {
1321 if (defined('DAEMON_EXTENDED_DEBUG') ||
$_GET['xdebug']) {
1322 _debug("update_rss_feed: done");
1327 function print_select($id, $default, $values, $attributes = "") {
1328 print "<select name=\"$id\" id=\"$id\" $attributes>";
1329 foreach ($values as $v) {
1335 print "<option$sel>$v</option>";
1340 function print_select_hash($id, $default, $values, $attributes = "") {
1341 print "<select name=\"$id\" id='$id' $attributes>";
1342 foreach (array_keys($values) as $v) {
1348 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1354 function get_article_filters($filters, $title, $content, $link) {
1357 if ($filters["title"]) {
1358 foreach ($filters["title"] as $filter) {
1359 $reg_exp = $filter["reg_exp"];
1360 $inverse = $filter["inverse"];
1361 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1362 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1364 array_push($matches, array($filter["action"], $filter["action_param"]));
1369 if ($filters["content"]) {
1370 foreach ($filters["content"] as $filter) {
1371 $reg_exp = $filter["reg_exp"];
1372 $inverse = $filter["inverse"];
1374 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1375 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1377 array_push($matches, array($filter["action"], $filter["action_param"]));
1382 if ($filters["both"]) {
1383 foreach ($filters["both"] as $filter) {
1384 $reg_exp = $filter["reg_exp"];
1385 $inverse = $filter["inverse"];
1388 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
1389 array_push($matches, array($filter["action"], $filter["action_param"]));
1392 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
1393 array_push($matches, array($filter["action"], $filter["action_param"]));
1399 if ($filters["link"]) {
1400 $reg_exp = $filter["reg_exp"];
1401 foreach ($filters["link"] as $filter) {
1402 $reg_exp = $filter["reg_exp"];
1403 $inverse = $filter["inverse"];
1405 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1406 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1408 array_push($matches, array($filter["action"], $filter["action_param"]));
1416 function find_article_filter($filters, $filter_name) {
1417 foreach ($filters as $f) {
1418 if ($f[0] == $filter_name) {
1425 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1426 $rtl_content = false, $last_updated = false, $last_error = false) {
1428 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1429 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1431 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1435 $rtl_tag = "dir=\"rtl\"";
1437 $rtl_tag = "dir=\"ltr\"";
1440 $error_notify_msg = "";
1443 $link_title = "Error: $last_error ($last_updated)";
1444 $error_notify_msg = "(Error)";
1445 } else if ($last_updated) {
1446 $link_title = "Updated: $last_updated";
1449 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1450 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1452 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1453 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1457 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1462 $fctr_class = "class=\"invisible\"";
1465 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1466 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1468 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1469 print "<div class=\"feedExtInfo\">
1470 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1477 function getmicrotime() {
1478 list($usec, $sec) = explode(" ",microtime());
1479 return ((float)$usec +
(float)$sec);
1482 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1483 foreach ($values as $v) {
1490 if ($v == $true_is) {
1491 $sel .= " value=\"1\"";
1493 $sel .= " value=\"0\"";
1496 print "<input class=\"noborder\"
1497 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1502 function initialize_user_prefs($link, $uid) {
1504 $uid = db_escape_string($uid);
1506 db_query($link, "BEGIN");
1508 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1510 $u_result = db_query($link, "SELECT pref_name
1511 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1513 $active_prefs = array();
1515 while ($line = db_fetch_assoc($u_result)) {
1516 array_push($active_prefs, $line["pref_name"]);
1519 while ($line = db_fetch_assoc($result)) {
1520 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1521 // print "adding " . $line["pref_name"] . "<br>";
1523 db_query($link, "INSERT INTO ttrss_user_prefs
1524 (owner_uid,pref_name,value) VALUES
1525 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1530 db_query($link, "COMMIT");
1534 function lookup_user_id($link, $user) {
1536 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1539 if (db_num_rows($result) == 1) {
1540 return db_fetch_result($result, 0, "id");
1546 function http_authenticate_user($link) {
1548 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1550 if (!$_SERVER["PHP_AUTH_USER"]) {
1552 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1553 header('HTTP/1.0 401 Unauthorized');
1557 $auth_result = authenticate_user($link,
1558 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1560 if (!$auth_result) {
1561 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1562 header('HTTP/1.0 401 Unauthorized');
1570 function authenticate_user($link, $login, $password, $force_auth = false) {
1572 if (!SINGLE_USER_MODE
) {
1574 $pwd_hash1 = encrypt_password($password);
1575 $pwd_hash2 = encrypt_password($password, $login);
1577 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
1578 && $_SERVER["REMOTE_USER"]) {
1580 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1582 $query = "SELECT id,login,access_level
1583 FROM ttrss_users WHERE
1587 $query = "SELECT id,login,access_level,pwd_hash
1588 FROM ttrss_users WHERE
1589 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1590 pwd_hash = '$pwd_hash2')";
1593 $result = db_query($link, $query);
1595 if (db_num_rows($result) == 1) {
1596 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1597 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1598 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1600 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1603 $user_theme = get_user_theme_path($link);
1605 $_SESSION["theme"] = $user_theme;
1606 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1607 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
1609 initialize_user_prefs($link, $_SESSION["uid"]);
1618 $_SESSION["uid"] = 1;
1619 $_SESSION["name"] = "admin";
1621 $user_theme = get_user_theme_path($link);
1623 $_SESSION["theme"] = $user_theme;
1624 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1626 initialize_user_prefs($link, $_SESSION["uid"]);
1632 function make_password($length = 8) {
1635 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1639 while ($i < $length) {
1640 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1642 if (!strstr($password, $char)) {
1650 // this is called after user is created to initialize default feeds, labels
1653 // user preferences are checked on every login, not here
1655 function initialize_user($link, $uid) {
1657 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1658 values ('$uid','unread = true', 'Unread articles')");
1660 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1661 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1663 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1664 values ('$uid', 'Tiny Tiny RSS: New Releases',
1665 'http://tt-rss.spb.ru/releases.rss')");
1667 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1668 values ('$uid', 'Tiny Tiny RSS: Forum',
1669 'http://tt-rss.spb.ru/forum/rss.php')");
1672 function logout_user() {
1674 if (isset($_COOKIE[session_name()])) {
1675 setcookie(session_name(), '', time()-42000, '/');
1679 function get_script_urlpath() {
1680 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1683 function validate_session($link) {
1684 if (SINGLE_USER_MODE
) {
1688 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1689 if ($_SESSION["ip_address"]) {
1690 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1691 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1697 if ($_SESSION["uid"]) {
1699 $result = db_query($link,
1700 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1702 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1704 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1709 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1711 //print_r($_SESSION);
1713 if (time() > $_SESSION["cookie_lifetime"]) {
1721 function login_sequence($link, $mobile = false) {
1722 if (!SINGLE_USER_MODE
) {
1724 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1725 $swu = db_escape_string($_REQUEST["swu"]);
1727 $_SESSION["prefs_cache"] = false;
1728 return authenticate_user($link, $swu, null, true);
1732 $login_action = $_POST["login_action"];
1734 # try to authenticate user if called from login form
1735 if ($login_action == "do_login") {
1736 $login = $_POST["login"];
1737 $password = $_POST["password"];
1738 $remember_me = $_POST["remember_me"];
1740 if (authenticate_user($link, $login, $password)) {
1741 $_POST["password"] = "";
1743 $_SESSION["language"] = $_POST["language"];
1745 header("Location: " . $_SERVER["REQUEST_URI"]);
1750 $_SESSION["login_error_msg"] = "Incorrect username or password";
1754 // print session_id();
1755 // print_r($_SESSION);
1757 if (!$_SESSION["uid"] ||
!validate_session($link)) {
1758 render_login_form($link, $mobile);
1761 /* bump login timestamp */
1762 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1765 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME
> 0) {
1766 setcookie("ttrss_lang", $_SESSION["language"],
1767 time() + SESSION_COOKIE_LIFETIME
);
1772 return authenticate_user($link, "admin", null);
1776 function truncate_string($str, $max_len) {
1777 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1778 return mb_substr($str, 0, $max_len, "utf-8") . "…";
1784 function get_user_theme_path($link) {
1785 $result = db_query($link, "SELECT theme_path
1787 ttrss_themes,ttrss_users
1788 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1789 if (db_num_rows($result) != 0) {
1790 return db_fetch_result($result, 0, "theme_path");
1796 function smart_date_time($timestamp) {
1797 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1798 return date("G:i", $timestamp);
1799 } else if (date("Y", $timestamp) == date("Y")) {
1800 return date("M d, G:i", $timestamp);
1802 return date("Y/m/d, G:i", $timestamp);
1806 function smart_date($timestamp) {
1807 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1809 } else if (date("Y", $timestamp) == date("Y")) {
1810 return date("D m", $timestamp);
1812 return date("Y/m/d", $timestamp);
1816 function sql_bool_to_string($s) {
1817 if ($s == "t" ||
$s == "1") {
1824 function sql_bool_to_bool($s) {
1825 if ($s == "t" ||
$s == "1") {
1833 function toggleEvenOdd($a) {
1840 function sanity_check($link) {
1845 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1846 $schema_version = db_fetch_result($result, 0, "schema_version");
1848 if ($schema_version != SCHEMA_VERSION
) {
1852 if (DB_TYPE
== "mysql") {
1853 $result = db_query($link, "SELECT true", false);
1854 if (db_num_rows($result) != 1) {
1859 error_reporting (DEFAULT_ERROR_LEVEL
);
1861 if ($error_code != 0) {
1862 print_error_xml($error_code);
1869 function file_is_locked($filename) {
1870 if (function_exists('flock')) {
1872 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "r");
1873 error_reporting(DEFAULT_ERROR_LEVEL
);
1875 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1876 flock($fp, LOCK_UN
);
1887 function make_lockfile($filename) {
1888 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "w");
1890 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1897 function make_stampfile($filename) {
1898 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "w");
1900 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1901 fwrite($fp, time() . "\n");
1902 flock($fp, LOCK_UN
);
1910 function read_stampfile($filename) {
1913 $fp = fopen(LOCK_DIRECTORY
. "/$filename", "r");
1914 error_reporting (DEFAULT_ERROR_LEVEL
);
1916 if (flock($fp, LOCK_EX
)) {
1917 $stamp = fgets($fp);
1918 flock($fp, LOCK_UN
);
1926 function sql_random_function() {
1927 if (DB_TYPE
== "mysql") {
1934 function catchup_feed($link, $feed, $cat_view) {
1936 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1941 $cat_qpart = "cat_id = '$feed'";
1943 $cat_qpart = "cat_id IS NULL";
1946 $tmp_result = db_query($link, "SELECT id
1947 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1950 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1952 $tmp_feed = $tmp_line["id"];
1954 db_query($link, "UPDATE ttrss_user_entries
1955 SET unread = false,last_read = NOW()
1956 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1959 } else if ($feed > 0) {
1961 $tmp_result = db_query($link, "SELECT id
1962 FROM ttrss_feeds WHERE parent_feed = '$feed'
1963 ORDER BY cat_id,title");
1965 $parent_ids = array();
1967 if (db_num_rows($tmp_result) > 0) {
1968 while ($p = db_fetch_assoc($tmp_result)) {
1969 array_push($parent_ids, "feed_id = " . $p["id"]);
1972 $children_qpart = implode(" OR ", $parent_ids);
1974 db_query($link, "UPDATE ttrss_user_entries
1975 SET unread = false,last_read = NOW()
1976 WHERE (feed_id = '$feed' OR $children_qpart)
1977 AND owner_uid = " . $_SESSION["uid"]);
1980 db_query($link, "UPDATE ttrss_user_entries
1981 SET unread = false,last_read = NOW()
1982 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1985 } else if ($feed < 0 && $feed > -10) { // special, like starred
1988 db_query($link, "UPDATE ttrss_user_entries
1989 SET unread = false,last_read = NOW()
1990 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1994 db_query($link, "UPDATE ttrss_user_entries
1995 SET unread = false,last_read = NOW()
1996 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
2001 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2003 if (DB_TYPE
== "pgsql") {
2004 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
2006 $match_part = "date_entered > DATE_SUB(NOW(),
2007 INTERVAL $intl HOUR) ";
2010 $result = db_query($link, "SELECT id FROM ttrss_entries,
2011 ttrss_user_entries WHERE $match_part AND
2013 ttrss_user_entries.ref_id = ttrss_entries.id AND
2014 owner_uid = ".$_SESSION["uid"]);
2016 $affected_ids = array();
2018 while ($line = db_fetch_assoc($result)) {
2019 array_push($affected_ids, $line["id"]);
2022 catchupArticlesById($link, $affected_ids, 0);
2025 } else if ($feed < -10) { // label
2027 // TODO make this more efficient
2029 $label_id = -$feed - 11;
2031 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2032 WHERE id = '$label_id'");
2035 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
2037 db_query($link, "BEGIN");
2039 $tmp2_result = db_query($link,
2043 ttrss_user_entries,ttrss_entries,ttrss_feeds
2045 ref_id = ttrss_entries.id AND
2046 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2048 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2050 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
2051 db_query($link, "UPDATE
2054 unread = false, last_read = NOW()
2056 int_id = " . $tmp_line["int_id"]);
2059 db_query($link, "COMMIT");
2061 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
2062 SET unread = false,last_read = NOW()
2065 AND owner_uid = ".$_SESSION["uid"]); */
2069 db_query($link, "BEGIN");
2071 $tag_name = db_escape_string($feed);
2073 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
2074 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
2076 while ($line = db_fetch_assoc($result)) {
2077 db_query($link, "UPDATE ttrss_user_entries SET
2078 unread = false, last_read = NOW()
2079 WHERE int_id = " . $line["post_int_id"]);
2081 db_query($link, "COMMIT");
2085 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
2089 $cat_qpart = "cat_id = '$feed'";
2091 $cat_qpart = "cat_id IS NULL";
2094 $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds
2095 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2097 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2098 $feed_url = $tmp_line["feed_url"];
2099 $feed_id = $tmp_line["id"];
2100 update_rss_feed($link, $feed_url, $feed_id, $force_update);
2104 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
2105 WHERE id = '$feed'");
2106 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
2107 update_rss_feed($link, $feed_url, $feed, $force_update);
2111 function getAllCounters($link, $omode = "flc", $active_feed = false) {
2112 /* getLabelCounters($link);
2113 getFeedCounters($link);
2114 getTagCounters($link);
2115 getGlobalCounters($link);
2116 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2117 getCategoryCounters($link);
2120 if (!$omode) $omode = "flc";
2122 getGlobalCounters($link);
2124 if (strchr($omode, "l")) getLabelCounters($link);
2125 if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS
, $active_feed);
2126 if (strchr($omode, "t")) getTagCounters($link);
2127 if (strchr($omode, "c")) {
2128 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2129 getCategoryCounters($link);
2134 function getCategoryCounters($link) {
2135 # two special categories are -1 and -2 (all virtuals; all labels)
2137 $ctr = getCategoryUnread($link, -1);
2139 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
2141 $ctr = getCategoryUnread($link, -2);
2143 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2145 $age_qpart = getMaxAgeSubquery();
2147 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
2148 FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id
2149 AND id = ref_id AND $age_qpart
2150 AND unread = true)) AS unread FROM ttrss_feeds
2152 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
2154 while ($line = db_fetch_assoc($result)) {
2155 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
2156 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2157 $line["unread"]."\"/>";
2161 function getCategoryUnread($link, $cat) {
2166 $cat_query = "cat_id = '$cat'";
2168 $cat_query = "cat_id IS NULL";
2171 $age_qpart = getMaxAgeSubquery();
2173 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
2175 AND owner_uid = " . $_SESSION["uid"]);
2177 $cat_feeds = array();
2178 while ($line = db_fetch_assoc($result)) {
2179 array_push($cat_feeds, "feed_id = " . $line["id"]);
2182 if (count($cat_feeds) == 0) return 0;
2184 $match_part = implode(" OR ", $cat_feeds);
2186 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2187 FROM ttrss_user_entries,ttrss_entries
2188 WHERE unread = true AND ($match_part) AND id = ref_id
2189 AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
2193 # this needs to be rewritten
2194 while ($line = db_fetch_assoc($result)) {
2195 $unread +
= $line["unread"];
2199 } else if ($cat == -1) {
2200 return getFeedUnread($link, -1) +
getFeedUnread($link, -2) +
getFeedUnread($link, -3);
2201 } else if ($cat == -2) {
2203 $rv = getLabelCounters($link, false, true);
2206 foreach (array_keys($rv) as $k) {
2208 $ctr +
= $rv[$k]["counter"];
2216 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE
) {
2217 if (DB_TYPE
== "pgsql") {
2218 return "ttrss_entries.date_entered >
2219 NOW() - INTERVAL '$days days'";
2221 return "ttrss_entries.date_entered >
2222 DATE_SUB(NOW(), INTERVAL $days DAY)";
2226 function getFeedUnread($link, $feed, $is_cat = false) {
2227 $n_feed = sprintf("%d", $feed);
2229 $age_qpart = getMaxAgeSubquery();
2232 return getCategoryUnread($link, $n_feed);
2233 } else if ($n_feed == -1) {
2234 $match_part = "marked = true";
2235 } else if ($n_feed == -2) {
2236 $match_part = "published = true";
2237 } else if ($n_feed == -3) {
2238 $match_part = "unread = true";
2240 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2242 if (DB_TYPE
== "pgsql") {
2243 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2245 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2248 } else if ($n_feed > 0) {
2250 $result = db_query($link, "SELECT id FROM ttrss_feeds
2251 WHERE parent_feed = '$n_feed'
2253 AND owner_uid = " . $_SESSION["uid"]);
2255 if (db_num_rows($result) > 0) {
2257 $linked_feeds = array();
2258 while ($line = db_fetch_assoc($result)) {
2259 array_push($linked_feeds, "feed_id = " . $line["id"]);
2262 array_push($linked_feeds, "feed_id = $n_feed");
2264 $match_part = implode(" OR ", $linked_feeds);
2266 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2267 FROM ttrss_user_entries,ttrss_entries
2268 WHERE unread = true AND
2269 ttrss_user_entries.ref_id = ttrss_entries.id AND
2272 owner_uid = " . $_SESSION["uid"]);
2276 # this needs to be rewritten
2277 while ($line = db_fetch_assoc($result)) {
2278 $unread +
= $line["unread"];
2284 $match_part = "feed_id = '$n_feed'";
2286 } else if ($feed < -10) {
2288 $label_id = -$feed - 11;
2290 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2291 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2293 $match_part = db_fetch_result($result, 0, "sql_exp");
2298 $result = db_query($link, "SELECT count(int_id) AS unread
2299 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2300 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2301 ttrss_user_entries.ref_id = ttrss_entries.id AND
2302 ttrss_feeds.hidden = false AND
2304 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
2308 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2309 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
2310 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
2311 AND unread = true AND $age_qpart AND
2312 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2315 $unread = db_fetch_result($result, 0, "unread");
2320 /* FIXME this needs reworking */
2322 function getGlobalUnread($link, $user_id = false) {
2325 $user_id = $_SESSION["uid"];
2328 $age_qpart = getMaxAgeSubquery();
2330 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2331 WHERE unread = true AND
2332 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2333 ttrss_user_entries.ref_id = ttrss_entries.id AND
2336 ttrss_user_entries.owner_uid = '$user_id'");
2337 $c_id = db_fetch_result($result, 0, "c_id");
2341 function getGlobalCounters($link, $global_unread = -1) {
2342 if ($global_unread == -1) {
2343 $global_unread = getGlobalUnread($link);
2345 print "<counter type=\"global\" id='global-unread'
2346 counter='$global_unread'/>";
2348 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2349 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2351 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2353 print "<counter type=\"global\" id='subscribed-feeds'
2354 counter='$subscribed_feeds'/>";
2358 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
2361 if (!$_SESSION["tctr_last_value"]) {
2362 $_SESSION["tctr_last_value"] = array();
2366 $old_counters = $_SESSION["tctr_last_value"];
2368 $tctrs_modified = false;
2370 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2371 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2372 ttrss_user_entries.ref_id = ttrss_entries.id AND
2373 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2374 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2376 select tag_name,0 as count FROM ttrss_tags
2377 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2379 $age_qpart = getMaxAgeSubquery();
2381 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2382 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2383 AND ref_id = id AND $age_qpart
2384 AND unread = true)) AS count FROM ttrss_tags
2385 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2386 ORDER BY count DESC LIMIT 55");
2390 while ($line = db_fetch_assoc($result)) {
2391 $tags[$line["tag_name"]] +
= $line["count"];
2394 foreach (array_keys($tags) as $tag) {
2395 $unread = $tags[$tag];
2397 $tag = htmlspecialchars($tag);
2399 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
2400 $old_counters[$tag] = $unread;
2401 $tctrs_modified = true;
2402 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2407 if ($smart_mode && $tctrs_modified) {
2408 $_SESSION["tctr_last_value"] = $old_counters;
2413 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
2415 $age_qpart = getMaxAgeSubquery();
2418 if (!$_SESSION["lctr_last_value"]) {
2419 $_SESSION["lctr_last_value"] = array();
2425 $old_counters = $_SESSION["lctr_last_value"];
2426 $lctrs_modified = false;
2428 $count = getFeedUnread($link, -1);
2431 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2433 $ret_arr["-1"]["counter"] = $count;
2434 $ret_arr["-1"]["description"] = __("Starred articles");
2437 $count = getFeedUnread($link, -2);
2440 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2442 $ret_arr["-2"]["counter"] = $count;
2443 $ret_arr["-2"]["description"] = __("Published articles");
2446 $count = getFeedUnread($link, -3);
2449 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2451 $ret_arr["-3"]["counter"] = $count;
2452 $ret_arr["-3"]["description"] = __("Fresh articles");
2456 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2457 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2459 while ($line = db_fetch_assoc($result)) {
2461 $id = -$line["id"] - 11;
2463 $label_name = $line["description"];
2465 error_reporting (0);
2467 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
2468 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
2469 ttrss_feeds.hidden = false AND
2471 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2472 ttrss_user_entries.ref_id = ttrss_entries.id AND
2473 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
2475 $count = db_fetch_result($tmp_result, 0, "count");
2477 if (!$smart_mode ||
$old_counters[$id] != $count) {
2478 $old_counters[$id] = $count;
2479 $lctrs_modified = true;
2481 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2483 $ret_arr[$id]["counter"] = $count;
2484 $ret_arr[$id]["description"] = $label_name;
2488 error_reporting (DEFAULT_ERROR_LEVEL
);
2491 if ($smart_mode && $lctrs_modified) {
2492 $_SESSION["lctr_last_value"] = $old_counters;
2498 /* function getFeedCounter($link, $id) {
2500 $result = db_query($link, "SELECT
2501 count(id) as count,last_error
2502 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2503 WHERE feed_id = '$id' AND unread = true
2504 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2505 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2507 $count = db_fetch_result($result, 0, "count");
2508 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2510 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2513 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $active_feed = false) {
2515 $age_qpart = getMaxAgeSubquery();
2518 if (!$_SESSION["fctr_last_value"]) {
2519 $_SESSION["fctr_last_value"] = array();
2523 $old_counters = $_SESSION["fctr_last_value"];
2525 /* $result = db_query($link, "SELECT id,last_error,parent_feed,
2526 SUBSTRING(last_updated,1,19) AS last_updated,
2528 FROM ttrss_entries,ttrss_user_entries
2529 WHERE feed_id = ttrss_feeds.id AND
2530 ttrss_user_entries.ref_id = ttrss_entries.id
2531 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2532 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
2533 AND parent_feed IS NULL"); */
2535 $query = "SELECT ttrss_feeds.id,
2537 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2539 COUNT(ttrss_entries.id) AS count
2541 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2542 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2543 AND ttrss_user_entries.unread = true)
2544 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
2546 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2547 AND parent_feed IS NULL
2548 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
2550 $result = db_query($link, $query);
2551 $fctrs_modified = false;
2553 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2555 while ($line = db_fetch_assoc($result)) {
2558 $count = $line["count"];
2559 $last_error = htmlspecialchars($line["last_error"]);
2561 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2562 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2564 $last_updated = date($short_date, strtotime($line["last_updated"]));
2567 $last_updated = htmlspecialchars($last_updated);
2569 $has_img = is_file(ICONS_DIR
. "/$id.ico");
2571 $tmp_result = db_query($link,
2572 "SELECT ttrss_feeds.id,COUNT(unread) AS unread
2573 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2574 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2575 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2576 WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
2578 if (db_num_rows($tmp_result) > 0) {
2579 while ($l = db_fetch_assoc($tmp_result)) {
2580 $count +
= $l["unread"];
2584 if (!$smart_mode ||
$old_counters[$id] != $count) {
2585 $old_counters[$id] = $count;
2586 $fctrs_modified = true;
2589 $error_part = "error=\"$last_error\"";
2595 $has_img_part = "hi=\"$has_img\"";
2600 if ($active_feed && $id == $active_feed) {
2601 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2603 $has_title_part = "";
2606 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $has_title_part/>";
2610 if ($smart_mode && $fctrs_modified) {
2611 $_SESSION["fctr_last_value"] = $old_counters;
2615 function get_script_dt_add() {
2616 if (strpos(VERSION
, ".99") === false) {
2623 function get_pgsql_version($link) {
2624 $result = db_query($link, "SELECT version() AS version");
2625 $version = split(" ", db_fetch_result($result, 0, "version"));
2629 function print_error_xml($code, $add_msg = "") {
2632 $error_msg = $ERRORS[$code];
2635 $error_msg = "$error_msg; $add_msg";
2638 print "<rpc-reply>";
2639 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2640 print "</rpc-reply>";
2643 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2644 $auth_login = '', $auth_pass = '') {
2646 # check for feed:http://url
2647 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2649 # check for feed://URL
2650 if (strpos($feed_link, "//") === 0) {
2651 $feed_link = "http:$feed_link";
2654 if ($feed_link == "") return;
2656 if ($cat_id == "0" ||
!$cat_id) {
2657 $cat_qpart = "NULL";
2659 $cat_qpart = "'$cat_id'";
2662 $result = db_query($link,
2663 "SELECT id FROM ttrss_feeds
2664 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2666 if (db_num_rows($result) == 0) {
2668 $result = db_query($link,
2669 "INSERT INTO ttrss_feeds
2670 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2671 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2672 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2674 $result = db_query($link,
2675 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2676 AND owner_uid = " . $_SESSION["uid"]);
2678 $feed_id = db_fetch_result($result, 0, "id");
2681 update_rss_feed($link, $feed_link, $feed_id, true);
2690 function print_feed_select($link, $id, $default_id = "",
2691 $attributes = "", $include_all_feeds = true) {
2693 print "<select id=\"$id\" name=\"$id\" $attributes>";
2694 if ($include_all_feeds) {
2695 print "<option value=\"0\">".__('All feeds')."</option>";
2698 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2699 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2701 if (db_num_rows($result) > 0 && $include_all_feeds) {
2702 print "<option disabled>--------</option>";
2705 while ($line = db_fetch_assoc($result)) {
2706 if ($line["id"] == $default_id) {
2707 $is_selected = "selected";
2711 printf("<option $is_selected value='%d'>%s</option>",
2712 $line["id"], htmlspecialchars($line["title"]));
2718 function print_feed_cat_select($link, $id, $default_id = "",
2719 $attributes = "", $include_all_cats = true) {
2721 print "<select id=\"$id\" name=\"$id\" $attributes>";
2723 if ($include_all_cats) {
2724 print "<option value=\"0\">".__('Uncategorized')."</option>";
2727 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2728 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2730 if (db_num_rows($result) > 0 && $include_all_cats) {
2731 print "<option disabled>--------</option>";
2734 while ($line = db_fetch_assoc($result)) {
2735 if ($line["id"] == $default_id) {
2736 $is_selected = "selected";
2740 printf("<option $is_selected value='%d'>%s</option>",
2741 $line["id"], htmlspecialchars($line["title"]));
2747 function checkbox_to_sql_bool($val) {
2748 return ($val == "on") ?
"true" : "false";
2751 function getFeedCatTitle($link, $id) {
2753 return __("Special");
2754 } else if ($id < -10) {
2755 return __("Labels");
2756 } else if ($id > 0) {
2757 $result = db_query($link, "SELECT ttrss_feed_categories.title
2758 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2759 cat_id = ttrss_feed_categories.id");
2760 if (db_num_rows($result) == 1) {
2761 return db_fetch_result($result, 0, "title");
2763 return __("Uncategorized");
2766 return "getFeedCatTitle($id) failed";
2771 function getFeedTitle($link, $id) {
2773 return __("Starred articles");
2774 } else if ($id == -2) {
2775 return __("Published articles");
2776 } else if ($id == -3) {
2777 return __("Fresh articles");
2778 } else if ($id < -10) {
2779 $label_id = -10 - $id;
2780 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2781 if (db_num_rows($result) == 1) {
2782 return db_fetch_result($result, 0, "description");
2784 return "Unknown label ($label_id)";
2787 } else if ($id > 0) {
2788 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2789 if (db_num_rows($result) == 1) {
2790 return db_fetch_result($result, 0, "title");
2792 return "Unknown feed ($id)";
2795 return "getFeedTitle($id) failed";
2800 function get_session_cookie_name() {
2801 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2804 function print_init_params($link) {
2805 print "<init-params>";
2806 if ($_SESSION["stored-params"]) {
2807 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2809 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2810 print "<param key=\"$key\" value=\"$value\"/>";
2815 print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
2816 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2817 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2818 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
2820 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2821 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2823 print "<param key=\"hide_read_feeds\" value=\"" .
2824 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2826 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2827 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2829 print "<param key=\"confirm_feed_catchup\" value=\"" .
2830 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2832 print "<param key=\"cdm_auto_catchup\" value=\"" .
2833 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2835 print "<param key=\"icons_url\" value=\"" . ICONS_URL
. "\"/>";
2837 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME
. "\"/>";
2839 print "<param key=\"default_view_mode\" value=\"" .
2840 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2842 print "<param key=\"default_view_limit\" value=\"" .
2843 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2845 print "<param key=\"prefs_active_tab\" value=\"" .
2846 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2848 print "<param key=\"infobox_disable_overlay\" value=\"" .
2849 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2851 print "<param key=\"icons_location\" value=\"" .
2854 print "<param key=\"hide_read_shows_special\" value=\"" .
2855 (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
2857 print "</init-params>";
2860 function print_runtime_info($link) {
2861 print "<runtime-info>";
2863 if (ENABLE_UPDATE_DAEMON
) {
2864 print "<param key=\"daemon_is_running\" value=\"".
2865 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2867 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
2869 $stamp = (int)read_stampfile("update_daemon.stamp");
2871 // print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
2874 $stamp_delta = time() - $stamp;
2876 if ($stamp_delta > 1800) {
2880 $_SESSION["daemon_stamp_check"] = time();
2883 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
2885 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2887 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2892 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2894 if ($_SESSION["last_version_check"] +
7200 < time()) {
2895 $new_version_details = check_for_update($link);
2897 print "<param key=\"new_version_available\" value=\"".
2898 sprintf("%d", $new_version_details != ""). "\"/>";
2900 $_SESSION["last_version_check"] = time();
2904 // print "<param key=\"new_version_available\" value=\"1\"/>";
2906 print "</runtime-info>";
2909 function getSearchSql($search, $match_on) {
2911 $search_query_part = "";
2913 $keywords = split(" ", $search);
2914 $query_keywords = array();
2916 if ($match_on == "both") {
2918 foreach ($keywords as $k) {
2919 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2920 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2923 $search_query_part = implode("AND", $query_keywords) . " AND ";
2925 } else if ($match_on == "title") {
2927 foreach ($keywords as $k) {
2928 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2931 $search_query_part = implode("AND", $query_keywords) . " AND ";
2933 } else if ($match_on == "content") {
2935 foreach ($keywords as $k) {
2936 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2940 $search_query_part = implode("AND", $query_keywords);
2942 return $search_query_part;
2945 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2947 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2951 $search_query_part = getSearchSql($search, $match_on);
2952 $search_query_part .= " AND ";
2955 $search_query_part = "";
2958 $view_query_part = "";
2960 if ($view_mode == "adaptive") {
2962 $view_query_part = " ";
2963 } else if ($feed != -1) {
2964 $unread = getFeedUnread($link, $feed, $cat_view);
2966 $view_query_part = " unread = true AND ";
2971 if ($view_mode == "marked") {
2972 $view_query_part = " marked = true AND ";
2975 if ($view_mode == "unread") {
2976 $view_query_part = " unread = true AND ";
2980 $limit_query_part = "LIMIT " . $limit;
2983 $vfeed_query_part = "";
2985 // override query strategy and enable feed display when searching globally
2986 if ($search && $search_mode == "all_feeds") {
2987 $query_strategy_part = "ttrss_entries.id > 0";
2988 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2989 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2990 $query_strategy_part = "ttrss_entries.id > 0";
2991 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2992 id = feed_id) as feed_title,";
2993 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2995 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2997 $tmp_result = false;
3000 $tmp_result = db_query($link, "SELECT id
3001 FROM ttrss_feeds WHERE cat_id = '$feed'");
3003 $tmp_result = db_query($link, "SELECT id
3004 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
3005 WHERE id = '$feed') AND id != '$feed'");
3008 $cat_siblings = array();
3010 if (db_num_rows($tmp_result) > 0) {
3011 while ($p = db_fetch_assoc($tmp_result)) {
3012 array_push($cat_siblings, "feed_id = " . $p["id"]);
3015 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3016 $feed, implode(" OR ", $cat_siblings));
3019 $query_strategy_part = "ttrss_entries.id > 0";
3022 } else if ($feed >= 0) {
3027 $query_strategy_part = "cat_id = '$feed'";
3029 $query_strategy_part = "cat_id IS NULL";
3032 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3035 $tmp_result = db_query($link, "SELECT id
3036 FROM ttrss_feeds WHERE parent_feed = '$feed'
3037 ORDER BY cat_id,title");
3039 $parent_ids = array();
3041 if (db_num_rows($tmp_result) > 0) {
3042 while ($p = db_fetch_assoc($tmp_result)) {
3043 array_push($parent_ids, "feed_id = " . $p["id"]);
3046 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3047 $feed, implode(" OR ", $parent_ids));
3049 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3051 $query_strategy_part = "feed_id = '$feed'";
3054 } else if ($feed == -1) { // starred virtual feed
3055 $query_strategy_part = "marked = true";
3056 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3057 } else if ($feed == -2) { // published virtual feed
3058 $query_strategy_part = "published = true";
3059 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3060 } else if ($feed == -3) { // fresh virtual feed
3061 $query_strategy_part = "unread = true";
3063 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
3065 if (DB_TYPE
== "pgsql") {
3066 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
3068 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
3071 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3072 } else if ($feed <= -10) { // labels
3073 $label_id = -$feed - 11;
3075 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
3076 WHERE id = '$label_id'");
3078 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
3080 if (!$query_strategy_part) {
3084 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3086 $query_strategy_part = "id > 0"; // dumb
3089 if (get_pref($link, 'REVERSE_HEADLINES')) {
3090 $order_by = "updated";
3092 $order_by = "updated DESC";
3095 if ($override_order) {
3096 $order_by = $override_order;
3101 if ($search && $search_mode == "all_feeds") {
3102 $feed_title = __("Search results")." ($search)";
3103 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3104 $feed_title = __("Search results")." ($search, $feed)";
3105 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3106 $feed_title = $feed;
3107 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
3112 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
3113 WHERE id = '$feed' AND owner_uid = $owner_uid");
3114 $feed_title = db_fetch_result($result, 0, "title");
3116 $feed_title = __("Uncategorized");
3120 $feed_title = __("Searched for")." $search ($feed_title)";
3125 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
3126 WHERE id = '$feed' AND owner_uid = $owner_uid");
3128 $feed_title = db_fetch_result($result, 0, "title");
3129 $feed_site_url = db_fetch_result($result, 0, "site_url");
3130 $last_error = db_fetch_result($result, 0, "last_error");
3133 $feed_title = __("Searched for") . " $search ($feed_title)";
3137 } else if ($feed == -1) {
3138 $feed_title = __("Starred articles");
3139 } else if ($feed == -2) {
3140 $feed_title = __("Published articles");
3141 } else if ($feed == -3) {
3142 $feed_title = __("Fresh articles");
3143 } else if ($feed < -10) {
3144 $label_id = -$feed - 11;
3145 $result = db_query($link, "SELECT description FROM ttrss_labels
3146 WHERE id = '$label_id'");
3147 $feed_title = db_fetch_result($result, 0, "description");
3150 $feed_title = __("Searched for") . " $search ($feed_title)";
3156 if ($feed < -10) error_reporting (0);
3158 $content_query_part = "content as content_preview,";
3160 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3163 $feed_kind = "Feeds";
3165 $feed_kind = "Labels";
3168 if ($limit_query_part) {
3169 $offset_query_part = "OFFSET $offset";
3174 ttrss_entries.id,ttrss_entries.title,
3176 unread,feed_id,marked,published,link,last_read,
3177 SUBSTRING(last_read,1,19) as last_read_noms,
3180 SUBSTRING(updated,1,19) as updated_noms,
3183 ttrss_entries,ttrss_user_entries,ttrss_feeds
3185 ttrss_feeds.hidden = false AND
3186 ttrss_user_entries.feed_id = ttrss_feeds.id AND
3187 ttrss_user_entries.ref_id = ttrss_entries.id AND
3188 ttrss_user_entries.owner_uid = '$owner_uid' AND
3191 $query_strategy_part ORDER BY $order_by
3192 $limit_query_part $offset_query_part";
3194 $result = db_query($link, $query);
3196 if ($_GET["debug"]) print $query;
3201 $feed_kind = "Tags";
3203 $result = db_query($link, "SELECT
3205 ttrss_entries.id as id,title,
3208 marked,link,last_read,
3209 SUBSTRING(last_read,1,19) as last_read_noms,
3212 SUBSTRING(updated,1,19) as updated_noms
3214 ttrss_entries,ttrss_user_entries,ttrss_tags
3216 ref_id = ttrss_entries.id AND
3217 ttrss_user_entries.owner_uid = '$owner_uid' AND
3218 post_int_id = int_id AND tag_name = '$feed' AND
3221 $query_strategy_part ORDER BY $order_by
3222 $limit_query_part");
3225 return array($result, $feed_title, $feed_site_url, $last_error);
3229 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3230 $search, $search_mode, $match_on) {
3232 $qfh_ret = queryFeedHeadlines($link, $feed,
3233 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
3236 $result = $qfh_ret[0];
3237 $feed_title = htmlspecialchars($qfh_ret[1]);
3238 $feed_site_url = $qfh_ret[2];
3239 $last_error = $qfh_ret[3];
3241 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
3243 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3244 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3245 <rss version=\"2.0\">
3247 <title>$feed_title</title>
3248 <link>$feed_site_url</link>
3249 <description>Feed generated by Tiny Tiny RSS</description>";
3251 while ($line = db_fetch_assoc($result)) {
3253 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3254 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
3256 $tags = get_article_tags($link, $line["id"], $owner_uid);
3258 foreach ($tags as $tag) {
3259 print "<category>" . htmlspecialchars($tag) . "</category>";
3262 $rfc822_date = date('r', strtotime($line["updated"]));
3264 print "<pubDate>$rfc822_date</pubDate>";
3267 htmlspecialchars($line["title"]) . "</title>";
3269 print "<description><![CDATA[" .
3270 $line["content_preview"] . "]]></description>";
3275 print "</channel></rss>";
3279 function getCategoryTitle($link, $cat_id) {
3281 if ($cat_id == -1) {
3282 return __("Special");
3283 } else if ($cat_id == -2) {
3284 return __("Labels");
3287 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3290 if (db_num_rows($result) == 1) {
3291 return db_fetch_result($result, 0, "title");
3293 return "Uncategorized";
3298 // http://ru2.php.net/strip-tags
3300 function strip_tags_long($textstring, $allowed){
3301 while($textstring != strip_tags($textstring, $allowed))
3303 while (strlen($textstring) != 0)
3305 if (strlen($textstring) > 1024) {
3308 $otherlen = strlen($textstring);
3310 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3311 $safetext .= $temptext;
3312 $textstring = substr_replace($textstring,'',0,$otherlen);
3314 $textstring = $safetext;
3320 function sanitize_rss($link, $str, $force_strip_tags = false) {
3323 if (get_pref($link, "STRIP_UNSAFE_TAGS") ||
$force_strip_tags) {
3325 $res = strip_tags_long($res,
3326 "<p><a><i><em><b><strong><blockquote><br><img><div><span><ul><ol><li>");
3328 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
3329 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3335 function send_headlines_digests($link, $limit = 100) {
3337 if (!DIGEST_ENABLE
) return false;
3339 $user_limit = DIGEST_EMAIL_LIMIT
;
3342 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3344 if (DB_TYPE
== "pgsql") {
3345 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3346 } else if (DB_TYPE
== "mysql") {
3347 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3350 $result = db_query($link, "SELECT id,email FROM ttrss_users
3351 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3353 while ($line = db_fetch_assoc($result)) {
3355 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3356 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3358 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3360 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3361 $digest = $tuple[0];
3362 $headlines_count = $tuple[1];
3363 $affected_ids = $tuple[2];
3364 $digest_text = $tuple[3];
3366 if ($headlines_count > 0) {
3368 $mail = new PHPMailer();
3370 $mail->PluginDir
= "phpmailer/";
3371 $mail->SetLanguage("en", "phpmailer/language/");
3373 $mail->CharSet
= "UTF-8";
3375 $mail->From
= DIGEST_FROM_ADDRESS
;
3376 $mail->FromName
= DIGEST_FROM_NAME
;
3377 $mail->AddAddress($line["email"], $line["login"]);
3379 if (DIGEST_SMTP_HOST
) {
3380 $mail->Host
= DIGEST_SMTP_HOST
;
3381 $mail->Mailer
= "smtp";
3382 $mail->Username
= DIGEST_SMTP_LOGIN
;
3383 $mail->Password
= DIGEST_SMTP_PASSWORD
;
3386 $mail->IsHTML(true);
3387 $mail->Subject
= DIGEST_SUBJECT
;
3388 $mail->Body
= $digest;
3389 $mail->AltBody
= $digest_text;
3391 $rc = $mail->Send();
3393 if (!$rc) print "ERROR: " . $mail->ErrorInfo
;
3397 if ($rc && $do_catchup) {
3398 print "Marking affected articles as read...\n";
3399 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
3402 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3403 WHERE id = " . $line["id"]);
3405 print "No headlines\n";
3410 print "All done.\n";
3414 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3416 require_once "MiniTemplator.class.php";
3418 $tpl = new MiniTemplator
;
3419 $tpl_t = new MiniTemplator
;
3421 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3422 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3424 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3425 $tpl->setVariable('CUR_TIME', date('G:i'));
3427 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3428 $tpl_t->setVariable('CUR_TIME', date('G:i'));
3430 $affected_ids = array();
3432 if (DB_TYPE
== "pgsql") {
3433 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3434 } else if (DB_TYPE
== "mysql") {
3435 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3438 $result = db_query($link, "SELECT ttrss_entries.title,
3439 ttrss_feeds.title AS feed_title,
3441 ttrss_user_entries.ref_id,
3443 SUBSTRING(content, 1, 120) AS excerpt,
3444 SUBSTRING(last_updated,1,19) AS last_updated
3446 ttrss_user_entries,ttrss_entries,ttrss_feeds
3448 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3449 AND include_in_digest = true
3452 AND ttrss_user_entries.owner_uid = $user_id
3454 ORDER BY ttrss_feeds.title, date_entered DESC
3457 $cur_feed_title = "";
3459 $headlines_count = db_num_rows($result);
3461 $headlines = array();
3463 while ($line = db_fetch_assoc($result)) {
3464 array_push($headlines, $line);
3467 for ($i = 0; $i < sizeof($headlines); $i++
) {
3469 $line = $headlines[$i];
3471 array_push($affected_ids, $line["ref_id"]);
3473 $updated = smart_date_time(strtotime($line["last_updated"]));
3475 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3476 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3477 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3478 $tpl->setVariable('ARTICLE_UPDATED', $updated);
3479 $tpl->setVariable('ARTICLE_EXCERPT',
3480 truncate_string(strip_tags($line["excerpt"]), 100));
3482 $tpl->addBlock('article');
3484 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3485 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3486 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3487 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3488 // $tpl_t->setVariable('ARTICLE_EXCERPT',
3489 // truncate_string(strip_tags($line["excerpt"]), 100));
3491 $tpl_t->addBlock('article');
3493 if ($headlines[$i]['feed_title'] != $headlines[$i+
1]['feed_title']) {
3494 $tpl->addBlock('feed');
3495 $tpl_t->addBlock('feed');
3500 $tpl->addBlock('digest');
3501 $tpl->generateOutputToString($tmp);
3503 $tpl_t->addBlock('digest');
3504 $tpl_t->generateOutputToString($tmp_t);
3506 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
3509 function check_for_update($link, $brief_fmt = true) {
3510 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3512 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
3517 if (ENABLE_SIMPLEPIE
) {
3518 $rss = new SimplePie();
3519 $rss->set_useragent(SIMPLEPIE_USERAGENT
. MAGPIE_USER_AGENT_EXT
);
3520 // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
3521 $rss->set_feed_url($fetch_url);
3522 $rss->set_output_encoding('UTF-8');
3525 $rss = fetch_rss($releases_feed);
3527 error_reporting (DEFAULT_ERROR_LEVEL
);
3531 if (ENABLE_SIMPLEPIE
) {
3532 $items = $rss->get_items();
3534 $items = $rss->items
;
3536 if (!$items ||
!is_array($items)) $items = $rss->entries
;
3537 if (!$items ||
!is_array($items)) $items = $rss;
3540 if (!is_array($items) ||
count($items) == 0) {
3544 $latest_item = $items[0];
3546 if (ENABLE_SIMPLEPIE
) {
3547 $last_title = $latest_item->get_title();
3549 $last_title = $latest_item["title"];
3552 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3554 if (ENABLE_SIMPLEPIE
) {
3555 $release_url = sanitize_rss($link, $latest_item->get_link());
3556 $content = sanitize_rss($link, $latest_item->get_description());
3558 $release_url = sanitize_rss($link, $latest_item["link"]);
3559 $content = sanitize_rss($link, $latest_item["description"]);
3562 if (version_compare(VERSION
, $latest_version) == -1) {
3564 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3565 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3566 <div id=\"milestoneDetails\">$content</div>");
3568 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3569 <div class='milestoneDetails'>$content</div>
3570 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3571 download and update information.";
3578 function markArticlesById($link, $ids, $cmode) {
3582 foreach ($ids as $id) {
3583 array_push($tmp_ids, "ref_id = '$id'");
3586 $ids_qpart = join(" OR ", $tmp_ids);
3589 db_query($link, "UPDATE ttrss_user_entries SET
3590 marked = false,last_read = NOW()
3591 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3592 } else if ($cmode == 1) {
3593 db_query($link, "UPDATE ttrss_user_entries SET
3595 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3597 db_query($link, "UPDATE ttrss_user_entries SET
3598 marked = NOT marked,last_read = NOW()
3599 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3603 function publishArticlesById($link, $ids, $cmode) {
3607 foreach ($ids as $id) {
3608 array_push($tmp_ids, "ref_id = '$id'");
3611 $ids_qpart = join(" OR ", $tmp_ids);
3614 db_query($link, "UPDATE ttrss_user_entries SET
3615 published = false,last_read = NOW()
3616 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3617 } else if ($cmode == 1) {
3618 db_query($link, "UPDATE ttrss_user_entries SET
3620 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3622 db_query($link, "UPDATE ttrss_user_entries SET
3623 published = NOT published,last_read = NOW()
3624 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3628 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3630 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3634 foreach ($ids as $id) {
3635 array_push($tmp_ids, "ref_id = '$id'");
3638 $ids_qpart = join(" OR ", $tmp_ids);
3641 db_query($link, "UPDATE ttrss_user_entries SET
3642 unread = false,last_read = NOW()
3643 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3644 } else if ($cmode == 1) {
3645 db_query($link, "UPDATE ttrss_user_entries SET
3647 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3649 db_query($link, "UPDATE ttrss_user_entries SET
3650 unread = NOT unread,last_read = NOW()
3651 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3655 function catchupArticleById($link, $id, $cmode) {
3658 db_query($link, "UPDATE ttrss_user_entries SET
3659 unread = false,last_read = NOW()
3660 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3661 } else if ($cmode == 1) {
3662 db_query($link, "UPDATE ttrss_user_entries SET
3664 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3666 db_query($link, "UPDATE ttrss_user_entries SET
3667 unread = NOT unread,last_read = NOW()
3668 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3672 function make_guid_from_title($title) {
3673 return preg_replace("/[ \"\',.:;]/", "-",
3674 mb_strtolower(strip_tags($title), 'utf-8'));
3677 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3678 $bottom = false, $rtl_content = false, $feed_id = 0,
3679 $is_cat = false, $search = false, $match_on = false,
3680 $search_mode = false, $offset = 0, $limit = 0) {
3682 $user_page_offset = $offset +
1;
3685 $class = "headlinesSubToolbar";
3686 $tid = "headlineActionsTop";
3688 $class = "headlinesSubToolbar";
3689 $tid = "headlineActionsBottom";
3692 print "<table class=\"$class\" id=\"$tid\"
3693 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3701 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3702 $page_next_link = "javascript:viewFeedGoPage(1)";
3703 $page_first_link = "javascript:viewFeedGoPage(0)";
3705 $catchup_page_link = "javascript:catchupPage()";
3706 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3707 $catchup_sel_link = "javascript:catchupSelection()";
3709 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3711 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3712 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3713 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3715 $tog_unread_link = "javascript:selectionToggleUnread()";
3716 $tog_marked_link = "javascript:selectionToggleMarked()";
3717 $tog_published_link = "javascript:selectionTogglePublished()";
3721 $sel_all_link = "javascript:cdmSelectArticles('all')";
3722 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3723 $sel_none_link = "javascript:cdmSelectArticles('none')";
3725 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3726 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3727 $tog_published_link = "javascript:selectionTogglePublished(true)";
3731 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
3733 print "<td class=\"headlineActions$rtl_cpart\">
3734 <ul class=\"headlineDropdownMenu\">
3737 <a href=\"$sel_all_link\">".__('All')."</a>,
3738 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3739 <a href=\"$sel_none_link\">".__('None')."</a></li>
3740 <li class=\"vsep\"> </li>
3741 <li class=\"top\">".__('Toggle')."<ul>
3742 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
3743 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3744 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3746 <li class=\"vsep\"> </li>
3747 <li class=\"top\">".__('Mark as read')."<ul>
3748 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
3749 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3750 <li><span class=\"insensitive\">--------</span></li>
3751 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3752 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3753 <li><span class=\"insensitive\">--------</span></li>
3754 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
3757 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3759 if ($limit != 0 && !$search && $enable_pagination) {
3761 <li class=\"vsep\"> </li>
3762 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3763 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3764 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
3768 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3770 <li class=\"vsep\"> </li>
3772 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3773 '$match_on', '$feed_id', '$is_cat');\">
3774 ".__('Convert to label')."</a></td>";
3780 // old style subtoolbar:
3782 print "<td class=\"headlineActions$rtl_cpart\">".
3784 <a href=\"$sel_all_link\">".__('All')."</a>,
3785 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3786 <a href=\"$sel_none_link\">".__('None')."</a>
3788 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3789 <a href=\"$tog_marked_link\">".__('Starred')."</a>
3791 __('Mark as read:')."
3792 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3793 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
3795 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
3798 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3799 '$match_on', '$feed_id', '$is_cat');\">
3800 ".__('Convert to label')."</a>";
3807 /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3808 print "<td class=\"headlineActions$rtl_cpart\">
3809 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3810 '$match_on', '$feed_id', '$is_cat');\">
3811 ".__('Convert to Label')."</a></td>";
3814 print "<td class=\"headlineTitle$rtl_cpart\">";
3816 print "<span class=\"headlineInnerTitle\">";
3818 if ($feed_site_url) {
3820 $target = "target=\"_new\"";
3822 print "<a $target href=\"$feed_site_url\">".
3823 truncate_string($feed_title,30)."</a>";
3829 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3832 if ($user_page_offset > 1) {
3833 print " [$user_page_offset] ";
3841 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3842 <img class=\"noborder\"
3843 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
3848 print "</tr></table>";
3852 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3854 $tmp_category = getCategoryTitle($link, $cat_id);
3855 $cat_unread = getCategoryUnread($link, $cat_id);
3858 $holder_style = "display:none;";
3865 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3867 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3868 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3871 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3873 print "<span id=\"FCAP-$cat_id\">";
3876 print " <span id=\"FCATCTR-$cat_id\"
3877 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3887 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3890 function outputFeedList($link, $tags = false) {
3892 print "<ul class=\"feedList\" id=\"feedList\">";
3894 $owner_uid = $_SESSION["uid"];
3898 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3900 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
3903 $cat_hidden = false;
3906 # print "<li class=\"feedCat\">".__('Special')."</li>";
3907 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3908 # print "<li class=\"feedCat\">".
3909 # "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3910 # __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3912 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3913 # <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3915 # $cat_unread = getCategoryUnread($link, -1);
3916 # $tmp_category = __("Special");
3917 # $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3919 printCategoryHeader($link, -1, $cat_hidden, false);
3922 $num_starred = getFeedUnread($link, -1);
3923 $num_published = getFeedUnread($link, -2);
3924 $num_fresh = getFeedUnread($link, -3);
3928 if ($num_fresh > 0) $class .= "Unread";
3930 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3931 "images/fresh.png", $link);
3935 if ($num_starred > 0) $class .= "Unread";
3937 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
3940 $mark_img_ext = "gif";
3942 $mark_img_ext = "png";
3945 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3946 "images/mark_set.$mark_img_ext", $link);
3950 if ($num_published > 0) $class .= "Unread";
3952 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3953 "images/pub_set.gif", $link);
3955 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3961 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
3963 $result = db_query($link, "SELECT id,sql_exp,description FROM
3964 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3966 if (db_num_rows($result) > 0) {
3967 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3969 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
3972 $cat_hidden = false;
3975 printCategoryHeader($link, -2, $cat_hidden, false);
3977 # print "<li class=\"feedCat\">".
3978 # "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3979 # __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3981 # print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
3983 print "<li><hr></li>";
3987 while ($line = db_fetch_assoc($result)) {
3989 error_reporting (0);
3991 $label_id = -$line['id'] - 11;
3992 $count = getFeedUnread($link, $label_id);
4000 error_reporting (DEFAULT_ERROR_LEVEL
);
4002 printFeedEntry($label_id,
4003 $class, $line["description"],
4004 $count, "images/label.png", $link);
4008 if (db_num_rows($result) > 0) {
4009 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4016 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
4017 print "<li><hr></li>";
4020 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4021 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4022 $order_by_qpart = "category,unread DESC,title";
4024 $order_by_qpart = "category,title";
4027 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4028 $order_by_qpart = "unread DESC,title";
4030 $order_by_qpart = "title";
4034 $age_qpart = getMaxAgeSubquery();
4036 $result = db_query($link, "SELECT ttrss_feeds.*,
4037 SUBSTRING(last_updated,1,19) AS last_updated_noms,
4038 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
4039 WHERE feed_id = ttrss_feeds.id AND unread = true
4041 AND ttrss_user_entries.ref_id = ttrss_entries.id
4042 AND owner_uid = '$owner_uid') as unread,
4044 ttrss_feed_categories.title AS category,
4045 ttrss_feed_categories.collapsed
4046 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4047 ON (ttrss_feed_categories.id = cat_id)
4049 ttrss_feeds.hidden = false AND
4050 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
4051 ORDER BY $order_by_qpart");
4053 $actid = $_GET["actid"];
4063 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4065 while ($line = db_fetch_assoc($result)) {
4067 $feed = trim($line["title"]);
4069 if (!$feed) $feed = "[Untitled]";
4071 $feed_id = $line["id"];
4073 $subop = $_GET["subop"];
4075 $unread = $line["unread"];
4077 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4078 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4080 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4083 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4086 $rtl_tag = "dir=\"RTL\"";
4091 $tmp_result = db_query($link,
4092 "SELECT id,COUNT(unread) AS unread
4093 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
4094 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
4095 WHERE parent_feed = '$feed_id' AND unread = true
4096 GROUP BY ttrss_feeds.id");
4098 if (db_num_rows($tmp_result) > 0) {
4099 while ($l = db_fetch_assoc($tmp_result)) {
4100 $unread +
= $l["unread"];
4104 $cat_id = $line["cat_id"];
4106 $tmp_category = $line["category"];
4108 if (!$tmp_category) {
4109 $tmp_category = __("Uncategorized");
4112 // $class = ($lnum % 2) ? "even" : "odd";
4114 if ($line["last_error"]) {
4120 if ($unread > 0) $class .= "Unread";
4122 if ($actid == $feed_id) {
4123 $class .= "Selected";
4126 $total_unread +
= $unread;
4128 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4134 $category = $tmp_category;
4136 $collapsed = $line["collapsed"];
4138 // workaround for NULL category
4139 if ($category == __("Uncategorized")) {
4140 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
4145 if ($collapsed == "t" ||
$collapsed == "1") {
4146 $holder_class = "feedCatHolder";
4147 $holder_style = "display:none;";
4150 $holder_class = "feedCatHolder";
4155 $cat_id = sprintf("%d", $cat_id);
4157 $cat_unread = getCategoryUnread($link, $cat_id);
4159 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
4161 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
4162 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
4163 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
4164 <span id=\"FCATCTR-$cat_id\"
4165 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
4168 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
4171 printFeedEntry($feed_id, $class, $feed, $unread,
4172 ICONS_DIR
."/$feed_id.ico", $link, $rtl_content,
4173 $last_updated, $line["last_error"]);
4178 if (db_num_rows($result) == 0) {
4179 print "<li>".__('No feeds to display.')."</li>";
4186 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4187 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4188 post_int_id = ttrss_user_entries.int_id AND
4189 unread = true AND ref_id = ttrss_entries.id
4190 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4192 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4193 ORDER BY tag_name"); */
4195 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4196 print "<li class=\"feedCat\">".__('Tags')."</li>";
4197 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
4200 $age_qpart = getMaxAgeSubquery();
4202 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
4203 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4204 AND ref_id = id AND $age_qpart
4205 AND unread = true)) AS count FROM ttrss_tags
4206 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4207 ORDER BY count DESC LIMIT 50");
4211 while ($line = db_fetch_assoc($result)) {
4212 $tags[$line["tag_name"]] +
= $line["count"];
4215 foreach (array_keys($tags) as $tag) {
4217 $unread = $tags[$tag];
4225 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4229 if (db_num_rows($result) == 0) {
4230 print "<li>No tags to display.</li>";
4233 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4243 function get_article_tags($link, $id, $owner_uid = 0) {
4245 $a_id = db_escape_string($id);
4247 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4249 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
4250 owner_uid as owner FROM
4251 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
4252 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
4256 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4257 array_push($tags, $tmp_line["tag_name"]);
4263 function trim_value(&$value) {
4264 $value = trim($value);
4267 function trim_array($array) {
4269 array_walk($tmp, 'trim_value');
4273 function tag_is_valid($tag) {
4274 if ($tag == '') return false;
4275 if (preg_match("/^[0-9]*$/", $tag)) return false;
4277 $tag = iconv("utf-8", "utf-8", $tag);
4278 if (!$tag) return false;
4283 function render_login_form($link, $mobile = false) {
4285 require_once "login_form.php";
4287 require_once "mobile/login_form.php";
4291 // from http://developer.apple.com/internet/safari/faq.html
4292 function no_cache_incantation() {
4293 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4294 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4295 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4296 header("Cache-Control: post-check=0, pre-check=0", false);
4297 header("Pragma: no-cache"); // HTTP/1.0
4300 function format_warning($msg, $id = "") {
4301 return "<div class=\"warning\" id=\"$id\">
4302 <img src=\"images/sign_excl.gif\">$msg</div>";
4305 function format_notice($msg) {
4306 return "<div class=\"notice\">
4307 <img src=\"images/sign_info.gif\">$msg</div>";
4310 function format_error($msg) {
4311 return "<div class=\"error\">
4312 <img src=\"images/sign_excl.gif\">$msg</div>";
4315 function print_notice($msg) {
4316 return print format_notice($msg);
4319 function print_warning($msg) {
4320 return print format_warning($msg);
4323 function print_error($msg) {
4324 return print format_error($msg);
4328 function T_sprintf() {
4329 $args = func_get_args();
4330 return vsprintf(__(array_shift($args)), $args);
4333 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
4335 /* we can figure out feed_id from article id anyway, why do we
4336 * pass feed_id here? */
4338 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4339 WHERE ref_id = '$id'");
4341 $feed_id = db_fetch_result($result, 0, "feed_id");
4343 print "<article id='$id'><![CDATA[";
4345 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4346 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4348 if (db_num_rows($result) == 1) {
4349 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4351 $rtl_content = false;
4355 $rtl_tag = "dir=\"RTL\"";
4362 if ($mark_as_read) {
4363 $result = db_query($link, "UPDATE ttrss_user_entries
4364 SET unread = false,last_read = NOW()
4365 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4368 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
4369 SUBSTRING(updated,1,16) as updated,
4370 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4373 FROM ttrss_entries,ttrss_user_entries
4374 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4380 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4381 $link_target = "target=\"_new\"";
4384 $line = db_fetch_assoc($result);
4386 if ($line["icon_url"]) {
4387 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4389 $feed_icon = " ";
4392 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
4393 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4395 $entry_comments = "";
4398 $num_comments = $line["num_comments"];
4399 $entry_comments = "";
4401 if ($num_comments > 0) {
4402 if ($line["comments"]) {
4403 $comments_url = $line["comments"];
4405 $comments_url = $line["link"];
4407 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4409 if ($line["comments"] && $line["link"] != $line["comments"]) {
4410 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4414 print "<div class=\"postReply\">";
4416 print "<div class=\"postHeader\">";
4418 $entry_author = $line["author"];
4420 if ($entry_author) {
4421 $entry_author = __(" - by ") . $entry_author;
4424 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4425 strtotime($line["updated"]));
4427 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4429 if ($line["link"]) {
4430 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
4431 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
4433 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4436 /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
4437 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
4438 ORDER BY tag_name"); */
4440 $tags = get_article_tags($link, $id);
4447 if ($_SESSION["theme"] == "3pane") {
4453 foreach ($tags as $tag) {
4455 $tag_escaped = str_replace("'", "\\'", $tag);
4457 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4459 if ($num_tags == $tag_limit) {
4460 $tags_str .= "…";
4462 } else if ($num_tags < $tag_limit) {
4463 $tags_str .= $tag_str;
4465 $f_tags_str .= $tag_str;
4468 $tags_str = preg_replace("/, $/", "", $tags_str);
4469 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4471 $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4472 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4474 if (!$entry_comments) $entry_comments = " "; # placeholder
4476 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4478 print "<div style='float : right'>
4479 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4481 <a title=\"Edit tags for this article\"
4482 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4483 <div clear='both'>$entry_comments</div>";
4487 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4488 print "<div class=\"postContent\">";
4490 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4492 $line["content"] = sanitize_rss($link, $line["content"]);
4494 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4495 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4498 print $line["content"];
4500 $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4503 if (db_num_rows($result) > 0) {
4504 print "<div class=\"postEnclosures\">";
4506 if (db_num_rows($result) == 1) {
4507 print __("Attachment:") . " ";
4509 print __("Attachments:") . " ";
4514 while ($line = db_fetch_assoc($result)) {
4516 $url = $line["content_url"];
4518 $filename = substr($url, strrpos($url, "/")+
1);
4520 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4521 $filename . " (" . $line["content_type"] . ")" . "</a>";
4523 array_push($entries, $entry);
4526 print join(", ", $entries);
4537 print "]]></article>";
4541 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4542 $next_unread_feed, $offset) {
4544 $disable_cache = false;
4546 $timing_info = getmicrotime();
4548 $topmost_article_ids = array();
4554 if ($subop == "undefined") $subop = "";
4556 if ($subop == "CatchupSelected") {
4557 $ids = split(",", db_escape_string($_GET["ids"]));
4558 $cmode = sprintf("%d", $_GET["cmode"]);
4560 catchupArticlesById($link, $ids, $cmode);
4563 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4564 update_generic_feed($link, $feed, $cat_view, true);
4567 if ($subop == "MarkAllRead") {
4568 catchup_feed($link, $feed, $cat_view);
4570 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4571 if ($next_unread_feed) {
4572 $feed = $next_unread_feed;
4578 $result = db_query($link,
4579 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4581 if (db_num_rows($result) == 0) {
4582 print "<div align='center'>".__('Feed not found.')."</div>";
4587 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4589 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4590 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4592 if (db_num_rows($result) == 1) {
4593 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4595 $rtl_content = false;
4599 $rtl_tag = "dir=\"RTL\"";
4605 $rtl_content = false;
4608 $script_dt_add = get_script_dt_add();
4610 /// START /////////////////////////////////////////////////////////////////////////////////
4612 $search = db_escape_string($_GET["query"]);
4615 $disable_cache = true;
4618 $search_mode = db_escape_string($_GET["search_mode"]);
4619 $match_on = db_escape_string($_GET["match_on"]);
4625 $real_offset = $offset * $limit;
4627 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4629 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4630 $search, $search_mode, $match_on, false, $real_offset);
4632 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4634 $result = $qfh_ret[0];
4635 $feed_title = $qfh_ret[1];
4636 $feed_site_url = $qfh_ret[2];
4637 $last_error = $qfh_ret[3];
4640 $feed_site_url = article_publish_url($link);
4643 /// STOP //////////////////////////////////////////////////////////////////////////////////
4646 print "<div id=\"headlinesContainer\" $rtl_tag>";
4649 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4653 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4654 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4657 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4660 $headlines_count = db_num_rows($result);
4662 if (db_num_rows($result) > 0) {
4664 # print "\{$offset}";
4666 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4667 print "<table class=\"headlinesList\" id=\"headlinesList\"
4668 cellspacing=\"0\">";
4671 $lnum = $limit*$offset;
4673 error_reporting (DEFAULT_ERROR_LEVEL
);
4677 while ($line = db_fetch_assoc($result)) {
4679 $class = ($lnum %
2) ?
"even" : "odd";
4682 $feed_id = $line["feed_id"];
4684 if (count($topmost_article_ids) < 5) {
4685 array_push($topmost_article_ids, $id);
4688 if ($line["last_read"] == "" &&
4689 ($line["unread"] != "t" && $line["unread"] != "1")) {
4691 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4694 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4698 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
4706 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4709 $mark_img_ext = "gif";
4711 $mark_img_ext = "png";
4714 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
4715 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
4717 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
4719 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
4721 alt=\"Star article\" onclick='javascript:tMark($id)'>";
4724 if ($line["published"] == "t" ||
$line["published"] == "1") {
4725 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
4727 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
4729 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
4731 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
4734 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4735 # $line["title"] . "</a>";
4737 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4738 $line["title"] . "</a>";
4740 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4741 # $line["title"] . "</a>";
4743 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4744 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
4746 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4747 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
4750 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4751 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4755 $entry_author = $line["author"];
4757 if ($entry_author) {
4758 $entry_author = " - by $entry_author";
4761 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4763 print "<tr class='$class' id='RROW-$id'>";
4765 print "<td class='hlUpdPic'>$update_pic</td>";
4767 print "<td class='hlSelectRow'>
4768 <input type=\"checkbox\" onclick=\"tSR(this)\"
4772 print "<td class='hlMarkedPic'>$marked_pic</td>";
4773 print "<td class='hlMarkedPic'>$published_pic</td>";
4775 # if ($line["feed_title"]) {
4776 # print "<td class='hlContent'>$content_link</td>";
4777 # print "<td class='hlFeed'>
4778 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4779 # truncate_string($line["feed_title"],30)."</a> </td>";
4782 print "<td class='hlContent' valign='middle'>";
4784 print "<a href=\"javascript:view($id,$feed_id);\">" .
4787 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4788 if ($content_preview) {
4789 print "<span class=\"contentPreview\"> - $content_preview</span>";
4795 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4796 # $line["feed_title"]."</a>
4798 if ($line["feed_title"]) {
4799 print "<span class=\"hlFeed\">
4800 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
4801 $line["feed_title"]."</a>)
4810 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
4817 $add_class = "Unread";
4822 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
4827 $cdm_cstyle = "style=\"display : none\"";
4830 print "<div class=\"cdmArticle$add_class\"
4831 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4832 onmouseout='cdmMouseOut(this)'>";
4834 print "<div class=\"cdmHeader\">";
4836 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4838 print "<a class=\"title\"
4839 onclick=\"javascript:toggleUnread($id, 0)\"
4840 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
4842 print $entry_author;
4845 print " <a id=\"CICH-$id\"
4846 href=\"javascript:cdmExpandArticle($id)\">
4847 (".__('Show article').")</a>";
4851 if ($line["feed_title"]) {
4852 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4857 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4858 $line["content_preview"] = preg_replace("/href=/i",
4859 "target=\"_new\" href=", $line["content_preview"]);
4862 print "<div class=\"cdmContent\" id=\"CICD-$id\" $cdm_cstyle>";
4864 // print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
4865 print $line["content_preview"];
4867 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4870 if (db_num_rows($e_result) > 0) {
4871 print "<div class=\"cdmEnclosures\">";
4873 if (db_num_rows($e_result) == 1) {
4874 print __("Attachment:") . " ";
4876 print __("Attachments:") . " ";
4881 while ($e_line = db_fetch_assoc($e_result)) {
4883 $url = $e_line["content_url"];
4885 $filename = substr($url, strrpos($url, "/")+
1);
4887 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4888 $filename . " (" . $e_line["content_type"] . ")" . "</a>";
4890 array_push($entries, $entry);
4893 print join(", ", $entries);
4898 print "<br clear='both'>";
4901 /* if (!$expand_cdm) {
4902 print "<a id=\"CICH-$id\"
4903 href=\"javascript:cdmExpandArticle($id)\">
4909 print "<div class=\"cdmFooter\"><span class='s0'>";
4911 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
4913 print __("Select:").
4914 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
4915 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4917 print "</span><span class='s1'>$marked_pic</span> ";
4918 print "<span class='s1'>$published_pic</span> ";
4920 $tags = get_article_tags($link, $id);
4923 $full_tags_str = "";
4926 foreach ($tags as $tag) {
4928 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4929 if ($num_tags < 5) {
4930 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4931 } else if ($num_tags == 5) {
4932 $tags_str .= "…";
4936 $tags_str = preg_replace("/, $/", "", $tags_str);
4937 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4939 $all_tags_div = "<span class='cdmAllTagsCtr'>…<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4941 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4944 if ($tags_str == "") $tags_str = "no tags";
4946 // print "<img src='images/tag.png' class='markedPic'>";
4948 print "<span class='s1'>
4949 <img class='tagsPic' src='images/tag.png' alt='Tags'
4950 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
4951 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4955 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4956 href=\"javascript:toggleUnread($id)\">
4967 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4971 // print_headline_subtoolbar($link,
4972 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4976 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
4984 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache);
4987 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
4989 function printTagCloud($link) {
4991 /* get first ref_id to count from */
4997 if (DB_TYPE == "pgsql") {
4998 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4999 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5000 AND date_entered > NOW() - INTERVAL '30 days'";
5002 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
5003 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
5004 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
5007 $result = db_query($link, $query);
5008 $first_id = db_fetch_result($result, 0, "id"); */
5010 //AND post_int_id >= '$first_id'
5011 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5012 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
5013 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
5015 $result = db_query($link, $query);
5019 while ($line = db_fetch_assoc($result)) {
5020 $tags[$line["tag_name"]] = $line["count"];
5025 $max_size = 32; // max font size in pixels
5026 $min_size = 11; // min font size in pixels
5028 // largest and smallest array values
5029 $max_qty = max(array_values($tags));
5030 $min_qty = min(array_values($tags));
5032 // find the range of values
5033 $spread = $max_qty - $min_qty;
5034 if ($spread == 0) { // we don't want to divide by zero
5038 // set the font-size increment
5039 $step = ($max_size - $min_size) / ($spread);
5041 // loop through the tag array
5042 foreach ($tags as $key => $value) {
5043 // calculate font-size
5044 // find the $value in excess of $min_qty
5045 // multiply by the font-size increment ($size)
5046 // and add the $min_size set above
5047 $size = round($min_size +
(($value - $min_qty) * $step));
5049 $key_escaped = str_replace("'", "\\'", $key);
5051 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
5052 $size . "px\" title=\"$value articles tagged with " .
5053 $key . '">' . $key . '</a> ';
5057 function print_checkpoint($n, $s) {
5058 $ts = getmicrotime();
5059 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5063 function sanitize_tag($tag) {
5066 $tag = mb_strtolower($tag, 'utf-8');
5068 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5070 // $tag = str_replace('"', "", $tag);
5071 // $tag = str_replace("+", " ", $tag);
5072 $tag = str_replace("technorati tag: ", "", $tag);
5077 function generate_publish_key() {
5078 return sha1(uniqid(rand(), true));
5081 function article_publish_url($link) {
5083 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
5085 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
5090 function clear_feed_articles($link, $id) {
5091 $result = db_query($link, "DELETE FROM ttrss_user_entries
5092 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
5094 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5095 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
5098 function add_feed_url() {
5099 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH
);
5100 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5104 function encrypt_password($pass, $login = '') {
5106 return "SHA1X:" . sha1("$login:$pass");
5108 return "SHA1:" . sha1($pass);