]>
git.wh0rd.org - tt-rss.git/blob - functions.php
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';
10 require_once 'db-prefs.php';
11 require_once 'compat.php';
12 require_once 'errors.php';
13 require_once 'version.php';
15 if (RSS_BACKEND_TYPE
== "magpie") {
16 require_once "magpierss/rss_fetch.inc";
17 require_once 'magpierss/rss_utils.inc';
18 } else if (RSS_BACKEND_TYPE
== "simplepie") {
19 require_once 'simplepie/simplepie.inc';
22 if (!DISABLE_SESSIONS
) {
23 require_once "feedcreator.class.php";
26 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
28 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
32 if (DB_TYPE
== "pgsql") {
33 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
34 marked = false AND feed_id = '$feed_id' AND
35 (SELECT date_entered FROM ttrss_entries WHERE
36 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
38 $pg_version = get_pgsql_version($link);
40 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
42 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
43 ttrss_entries.id = ref_id AND
45 feed_id = '$feed_id' AND
46 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
50 $result = db_query($link, "DELETE FROM ttrss_user_entries
52 WHERE ttrss_entries.id = ref_id AND
54 feed_id = '$feed_id' AND
55 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
58 $rows = pg_affected_rows($result);
62 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
63 marked = false AND feed_id = '$feed_id' AND
64 (SELECT date_entered FROM ttrss_entries WHERE
65 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
67 $result = db_query($link, "DELETE FROM ttrss_user_entries
68 USING ttrss_user_entries, ttrss_entries
69 WHERE ttrss_entries.id = ref_id AND
71 feed_id = '$feed_id' AND
72 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
74 $rows = mysql_affected_rows($link);
79 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
83 function global_purge_old_posts($link, $do_output = false, $limit = false) {
85 $random_qpart = sql_random_function();
88 $limit_qpart = "LIMIT $limit";
93 $result = db_query($link,
94 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
95 ORDER BY $random_qpart $limit_qpart");
97 while ($line = db_fetch_assoc($result)) {
99 $feed_id = $line["id"];
100 $purge_interval = $line["purge_interval"];
101 $owner_uid = $line["owner_uid"];
103 if ($purge_interval == 0) {
105 $tmp_result = db_query($link,
106 "SELECT value FROM ttrss_user_prefs WHERE
107 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
109 if (db_num_rows($tmp_result) != 0) {
110 $purge_interval = db_fetch_result($tmp_result, 0, "value");
115 // print "Feed $feed_id: purge interval = $purge_interval\n";
118 if ($purge_interval > 0) {
119 purge_feed($link, $feed_id, $purge_interval, $do_output);
123 // purge orphaned posts in main content table
124 db_query($link, "DELETE FROM ttrss_entries WHERE
125 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
129 function purge_old_posts($link) {
131 $user_id = $_SESSION["uid"];
133 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
134 WHERE owner_uid = '$user_id'");
136 while ($line = db_fetch_assoc($result)) {
138 $feed_id = $line["id"];
139 $purge_interval = $line["purge_interval"];
141 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
143 if ($purge_interval > 0) {
144 purge_feed($link, $feed_id, $purge_interval);
148 // purge orphaned posts in main content table
149 db_query($link, "DELETE FROM ttrss_entries WHERE
150 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
153 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
155 if (WEB_DEMO_MODE
) return;
158 $user_id = $_SESSION["uid"];
159 purge_old_posts($link);
162 // db_query($link, "BEGIN");
164 if (MAX_UPDATE_TIME
> 0) {
165 if (DB_TYPE
== "mysql") {
168 $q_order = "RANDOM()";
171 $q_order = "last_updated DESC";
174 $result = db_query($link, "SELECT feed_url,id,
175 SUBSTRING(last_updated,1,19) AS last_updated,
176 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
181 while ($line = db_fetch_assoc($result)) {
182 $upd_intl = $line["update_interval"];
184 if (!$upd_intl ||
$upd_intl == 0) {
185 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
189 // Updates for this feed are disabled
193 if ($fetch ||
(!$line["last_updated"] ||
194 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
196 // print "<!-- feed: ".$line["feed_url"]." -->";
198 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
200 $upd_elapsed = time() - $upd_start;
202 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
208 // db_query($link, "COMMIT");
212 function fetch_file_contents($url) {
213 if (USE_CURL_FOR_ICONS
) {
214 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
216 $ch = curl_init($url);
217 $fp = fopen($tmpfile, "w");
220 curl_setopt($ch, CURLOPT_FILE
, $fp);
226 $contents = file_get_contents($tmpfile);
232 return file_get_contents($url);
237 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
238 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
240 function get_favicon_url($url) {
242 if ($html = @fetch_file_contents
($url)) {
244 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
245 // Attempt to grab a favicon link from their webpage url
246 $linkUrl = html_entity_decode($matches[1]);
248 if (substr($linkUrl, 0, 1) == '/') {
249 $urlParts = parse_url($url);
250 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
251 } else if (substr($linkUrl, 0, 7) == 'http://') {
252 $faviconURL = $linkUrl;
253 } else if (substr($url, -1, 1) == '/') {
254 $faviconURL = $url.$linkUrl;
256 $faviconURL = $url.'/'.$linkUrl;
260 // If unsuccessful, attempt to "guess" the favicon location
261 $urlParts = parse_url($url);
262 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
266 // Run a test to see if what we have attempted to get actually exists.
267 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
274 function url_validate($link) {
276 $url_parts = @parse_url
($link);
278 if ( empty( $url_parts["host"] ) )
281 if ( !empty( $url_parts["path"] ) ) {
282 $documentpath = $url_parts["path"];
287 if ( !empty( $url_parts["query"] ) )
288 $documentpath .= "?" . $url_parts["query"];
290 $host = $url_parts["host"];
291 $port = $url_parts["port"];
296 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
301 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
303 $http_response = fgets( $socket, 22 );
305 $responses = "/(200 OK)|(30[0-9] Moved)/";
306 if ( preg_match($responses, $http_response) ) {
315 function check_feed_favicon($site_url, $feed, $link) {
316 $favicon_url = get_favicon_url($site_url);
318 # print "FAVICON [$site_url]: $favicon_url\n";
322 $icon_file = ICONS_DIR
. "/$feed.ico";
324 if ($favicon_url && !file_exists($icon_file)) {
325 $contents = fetch_file_contents($favicon_url);
327 $fp = fopen($icon_file, "w");
330 fwrite($fp, $contents);
332 chmod($icon_file, 0644);
336 error_reporting(DEFAULT_ERROR_LEVEL
);
340 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
342 if (WEB_DEMO_MODE
) return;
344 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
348 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
349 FROM ttrss_feeds WHERE id = '$feed'");
351 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
352 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
354 $update_interval = db_fetch_result($result, 0, "update_interval");
356 if ($update_interval < 0) { return; }
358 $feed = db_escape_string($feed);
360 $fetch_url = $feed_url;
362 if ($auth_login && $auth_pass) {
363 $url_parts = array();
364 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
366 if ($url_parts[1] && $url_parts[2]) {
367 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
372 if (RSS_BACKEND_TYPE
== "magpie") {
374 $rss = fetch_rss($fetch_url);
375 error_reporting (DEFAULT_ERROR_LEVEL
);
376 } else if (RSS_BACKEND_TYPE
== "simplepie") {
378 if (!file_exists(SIMPLEPIE_CACHE_DIR
)) {
379 mkdir(SIMPLEPIE_CACHE_DIR
);
382 $rss = new SimplePie();
383 $rss->feed_url($fetch_url);
384 $rss->cache_location(SIMPLEPIE_CACHE_DIR
);
388 $feed = db_escape_string($feed);
392 if (RSS_BACKEND_TYPE
== "simplepie") {
393 $rss_check = $rss->data
;
398 // db_query($link, "BEGIN");
400 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
401 FROM ttrss_feeds WHERE id = '$feed'");
403 $registered_title = db_fetch_result($result, 0, "title");
404 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
405 $orig_site_url = db_fetch_result($result, 0, "site_url");
407 $owner_uid = db_fetch_result($result, 0, "owner_uid");
409 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
410 check_feed_favicon($rss->channel
["link"], $feed, $link);
413 if (!$registered_title ||
$registered_title == "[Unknown]") {
415 if (RSS_BACKEND_TYPE
== "magpie") {
416 $feed_title = db_escape_string($rss->channel
["title"]);
418 $feed_title = $rss->get_feed_title();
421 db_query($link, "UPDATE ttrss_feeds SET
422 title = '$feed_title' WHERE id = '$feed'");
425 if (RSS_BACKEND_TYPE
== "magpie") {
426 $site_url = $rss->channel
["link"];
427 // weird, weird Magpie
428 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
430 $site_url = $rss->get_feed_link();
433 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
434 db_query($link, "UPDATE ttrss_feeds SET
435 site_url = '$site_url' WHERE id = '$feed'");
438 // print "I: " . $rss->channel["image"]["url"];
440 if (RSS_BACKEND_TYPE
== "magpie") {
441 $icon_url = $rss->image
["url"];
443 $icon_url = $rss->get_image_url(); # FIXME
446 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
447 $icon_url = db_escape_string($icon_url);
448 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
454 $result = db_query($link, "SELECT reg_exp,
455 ttrss_filter_types.name AS name,
456 ttrss_filter_actions.name AS action
457 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
459 owner_uid = $owner_uid AND
460 ttrss_filter_types.id = filter_type AND
461 ttrss_filter_actions.id = action_id AND
462 (feed_id IS NULL OR feed_id = '$feed')");
464 while ($line = db_fetch_assoc($result)) {
465 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
467 $filter["reg_exp"] = $line["reg_exp"];
468 $filter["action"] = $line["action"];
470 array_push($filters[$line["name"]], $filter);
473 if (RSS_BACKEND_TYPE
== "magpie") {
474 $iterator = $rss->items
;
476 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
477 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
480 $iterator = $rss->get_items();
483 if (!is_array($iterator)) {
484 /* db_query($link, "UPDATE ttrss_feeds
485 SET last_error = 'Parse error: can\'t find any articles.'
486 WHERE id = '$feed'"); */
490 foreach ($iterator as $item) {
492 if (RSS_BACKEND_TYPE
== "magpie") {
494 $entry_guid = $item["id"];
496 if (!$entry_guid) $entry_guid = $item["guid"];
497 if (!$entry_guid) $entry_guid = $item["link"];
498 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
500 if (!$entry_guid) continue;
502 $entry_timestamp = "";
504 $rss_2_date = $item['pubdate'];
505 $rss_1_date = $item['dc']['date'];
506 $atom_date = $item['issued'];
507 if (!$atom_date) $atom_date = $item['updated'];
509 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
510 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
511 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
513 if ($entry_timestamp == "") {
514 $entry_timestamp = time();
515 $no_orig_date = 'true';
517 $no_orig_date = 'false';
520 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
522 $entry_title = trim(strip_tags($item["title"]));
524 // strange Magpie workaround
525 $entry_link = $item["link_"];
526 if (!$entry_link) $entry_link = $item["link"];
528 if (!$entry_title) continue;
529 # if (!$entry_link) continue;
531 $entry_link = strip_tags($entry_link);
533 $entry_content = $item["content:escaped"];
535 if (!$entry_content) $entry_content = $item["content:encoded"];
536 if (!$entry_content) $entry_content = $item["content"];
537 if (!$entry_content) $entry_content = $item["atom_content"];
538 if (!$entry_content) $entry_content = $item["summary"];
539 if (!$entry_content) $entry_content = $item["description"];
541 // if (!$entry_content) continue;
544 if (is_array($entry_content)) {
545 $entry_content = $entry_content["encoded"];
546 if (!$entry_content) $entry_content = $entry_content["escaped"];
550 // print_r(htmlspecialchars($entry_content));
553 $entry_content_unescaped = $entry_content;
554 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
556 $entry_comments = strip_tags($item["comments"]);
558 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
559 $entry_guid = db_escape_string(strip_tags($entry_guid));
561 $result = db_query($link, "SELECT id FROM ttrss_entries
562 WHERE guid = '$entry_guid'");
564 $entry_content = db_escape_string($entry_content);
565 $entry_title = db_escape_string($entry_title);
566 $entry_link = db_escape_string($entry_link);
567 $entry_comments = db_escape_string($entry_comments);
569 $num_comments = db_escape_string($item["slash"]["comments"]);
571 if (!$num_comments) $num_comments = 0;
576 $entry_content = sanitize_rss($entry_content);
578 db_query($link, "BEGIN");
580 if (db_num_rows($result) == 0) {
582 // base post entry does not exist, create it
584 $result = db_query($link,
585 "INSERT INTO ttrss_entries
601 '$entry_timestamp_fmt',
610 // we keep encountering the entry in feeds, so we need to
611 // update date_entered column so that we don't get horrible
612 // dupes when the entry gets purged and reinserted again e.g.
613 // in the case of SLOW SLOW OMG SLOW updating feeds
615 $base_entry_id = db_fetch_result($result, 0, "id");
617 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
618 WHERE id = '$base_entry_id'");
621 // now it should exist, if not - bad luck then
623 $result = db_query($link, "SELECT
624 id,content_hash,no_orig_date,title,
625 substring(date_entered,1,19) as date_entered,
626 substring(updated,1,19) as updated,
630 WHERE guid = '$entry_guid'");
632 if (db_num_rows($result) == 1) {
634 // this will be used below in update handler
635 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
636 $orig_title = db_fetch_result($result, 0, "title");
637 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
638 $orig_date_entered = strtotime(db_fetch_result($result,
641 $ref_id = db_fetch_result($result, 0, "id");
643 // check for user post link to main table
645 // do we allow duplicate posts with same GUID in different feeds?
646 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
647 $dupcheck_qpart = "AND feed_id = '$feed'";
649 $dupcheck_qpart = "";
652 // error_reporting(0);
654 $filter_name = get_filter_name($entry_title, $entry_content,
655 $entry_link, $filters);
657 if ($filter_name == "filter") {
661 // error_reporting (DEFAULT_ERROR_LEVEL);
663 $result = db_query($link,
664 "SELECT ref_id FROM ttrss_user_entries WHERE
665 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
668 // okay it doesn't exist - create user entry
669 if (db_num_rows($result) == 0) {
671 if ($filter_name != 'catchup') {
673 $last_read_qpart = 'NULL';
676 $last_read_qpart = 'NOW()';
679 if ($filter_name == 'mark') {
685 $result = db_query($link,
686 "INSERT INTO ttrss_user_entries
687 (ref_id, owner_uid, feed_id, unread, last_read, marked)
688 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
689 $last_read_qpart, $marked)");
692 $post_needs_update = false;
694 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
695 ($content_hash != $orig_content_hash)) {
696 $post_needs_update = true;
699 if ($orig_title != $entry_title) {
700 $post_needs_update = true;
703 if ($orig_num_comments != $num_comments) {
704 $post_needs_update = true;
707 // this doesn't seem to be very reliable
709 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
710 // $post_needs_update = true;
713 // if post needs update, update it and mark all user entries
714 // linking to this post as updated
715 if ($post_needs_update) {
717 // print "<!-- post $orig_title needs update : $post_needs_update -->";
719 db_query($link, "UPDATE ttrss_entries
720 SET title = '$entry_title', content = '$entry_content',
721 num_comments = '$num_comments'
722 WHERE id = '$ref_id'");
724 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
725 db_query($link, "UPDATE ttrss_user_entries
726 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
728 db_query($link, "UPDATE ttrss_user_entries
729 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
735 db_query($link, "COMMIT");
738 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
742 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
743 $entry_content_unescaped, $entry_tags);
745 // print "<br>$entry_title : $entry_content_unescaped<br>";
746 // print_r($entry_tags);
749 $entry_tags = $entry_tags[1];
751 if (count($entry_tags) > 0) {
753 db_query($link, "BEGIN");
755 $result = db_query($link, "SELECT id,int_id
756 FROM ttrss_entries,ttrss_user_entries
757 WHERE guid = '$entry_guid'
758 AND feed_id = '$feed' AND ref_id = id
759 AND owner_uid = '$owner_uid'");
761 if (db_num_rows($result) == 1) {
763 $entry_id = db_fetch_result($result, 0, "id");
764 $entry_int_id = db_fetch_result($result, 0, "int_id");
766 foreach ($entry_tags as $tag) {
767 $tag = db_escape_string(strtolower($tag));
769 $tag = str_replace("+", " ", $tag);
770 $tag = str_replace("technorati tag: ", "", $tag);
772 $result = db_query($link, "SELECT id FROM ttrss_tags
773 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
774 owner_uid = '$owner_uid' LIMIT 1");
776 // print db_fetch_result($result, 0, "id");
778 if ($result && db_num_rows($result) == 0) {
780 // print "tagging $entry_id as $tag<br>";
782 db_query($link, "INSERT INTO ttrss_tags
783 (owner_uid,tag_name,post_int_id)
784 VALUES ('$owner_uid','$tag', '$entry_int_id')");
788 db_query($link, "COMMIT");
792 db_query($link, "UPDATE ttrss_feeds
793 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
795 // db_query($link, "COMMIT");
798 $error_msg = db_escape_string(magpie_error());
800 "UPDATE ttrss_feeds SET last_error = '$error_msg',
801 last_updated = NOW() WHERE id = '$feed'");
806 function print_select($id, $default, $values, $attributes = "") {
807 print "<select name=\"$id\" id=\"$id\" $attributes>";
808 foreach ($values as $v) {
814 print "<option$sel>$v</option>";
819 function print_select_hash($id, $default, $values, $attributes = "") {
820 print "<select name=\"$id\" id='$id' $attributes>";
821 foreach (array_keys($values) as $v) {
827 print "<option $sel value=\"$v\">".$values[$v]."</option>";
833 function get_filter_name($title, $content, $link, $filters) {
835 if ($filters["title"]) {
836 foreach ($filters["title"] as $filter) {
837 $reg_exp = $filter["reg_exp"];
838 if (preg_match("/$reg_exp/i", $title)) {
839 return $filter["action"];
844 if ($filters["content"]) {
845 foreach ($filters["content"] as $filter) {
846 $reg_exp = $filter["reg_exp"];
847 if (preg_match("/$reg_exp/i", $content)) {
848 return $filter["action"];
853 if ($filters["both"]) {
854 foreach ($filters["both"] as $filter) {
855 $reg_exp = $filter["reg_exp"];
856 if (preg_match("/$reg_exp/i", $title) ||
857 preg_match("/$reg_exp/i", $content)) {
858 return $filter["action"];
863 if ($filters["link"]) {
864 $reg_exp = $filter["reg_exp"];
865 foreach ($filters["link"] as $filter) {
866 $reg_exp = $filter["reg_exp"];
867 if (preg_match("/$reg_exp/i", $link)) {
868 return $filter["action"];
876 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
877 $rtl_content = false, $last_updated = false, $last_error = false) {
879 if (file_exists($icon_file) && filesize($icon_file) > 0) {
880 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
882 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
886 $rtl_tag = "dir=\"rtl\"";
888 $rtl_tag = "dir=\"ltr\"";
891 $error_notify_msg = "";
894 $link_title = "Error: $last_error ($last_updated)";
895 $error_notify_msg = "(Error)";
896 } else if ($last_updated) {
897 $link_title = "Updated: $last_updated";
900 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
901 href=\"#\" onclick=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
903 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
904 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
908 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
913 $fctr_class = "class=\"invisible\"";
916 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
917 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
919 if (get_pref($link, "EXTENDED_FEEDLIST")) {
920 print "<div class=\"feedExtInfo\">
921 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
928 function getmicrotime() {
929 list($usec, $sec) = explode(" ",microtime());
930 return ((float)$usec +
(float)$sec);
933 function print_radio($id, $default, $values, $attributes = "") {
934 foreach ($values as $v) {
942 $sel .= " value=\"1\"";
944 $sel .= " value=\"0\"";
947 print "<input class=\"noborder\"
948 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
953 function initialize_user_prefs($link, $uid) {
955 $uid = db_escape_string($uid);
957 db_query($link, "BEGIN");
959 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
961 $u_result = db_query($link, "SELECT pref_name
962 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
964 $active_prefs = array();
966 while ($line = db_fetch_assoc($u_result)) {
967 array_push($active_prefs, $line["pref_name"]);
970 while ($line = db_fetch_assoc($result)) {
971 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
972 // print "adding " . $line["pref_name"] . "<br>";
974 db_query($link, "INSERT INTO ttrss_user_prefs
975 (owner_uid,pref_name,value) VALUES
976 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
981 db_query($link, "COMMIT");
985 function lookup_user_id($link, $user) {
987 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
990 if (db_num_rows($result) == 1) {
991 return db_fetch_result($result, 0, "id");
997 function http_authenticate_user($link) {
999 if (!$_SERVER["PHP_AUTH_USER"]) {
1001 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1002 header('HTTP/1.0 401 Unauthorized');
1006 $auth_result = authenticate_user($link,
1007 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1009 if (!$auth_result) {
1010 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1011 header('HTTP/1.0 401 Unauthorized');
1019 function authenticate_user($link, $login, $password, $force_auth = false) {
1021 if (!SINGLE_USER_MODE
) {
1023 $pwd_hash = 'SHA1:' . sha1($password);
1025 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1026 $query = "SELECT id,login,access_level
1027 FROM ttrss_users WHERE
1030 $query = "SELECT id,login,access_level
1031 FROM ttrss_users WHERE
1032 login = '$login' AND pwd_hash = '$pwd_hash'";
1035 $result = db_query($link, $query);
1037 if (db_num_rows($result) == 1) {
1038 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1039 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1040 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1042 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1045 $user_theme = get_user_theme_path($link);
1047 $_SESSION["theme"] = $user_theme;
1048 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1050 initialize_user_prefs($link, $_SESSION["uid"]);
1059 $_SESSION["uid"] = 1;
1060 $_SESSION["name"] = "admin";
1062 $user_theme = get_user_theme_path($link);
1064 $_SESSION["theme"] = $user_theme;
1065 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1067 initialize_user_prefs($link, $_SESSION["uid"]);
1073 function make_password($length = 8) {
1076 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1080 while ($i < $length) {
1081 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1083 if (!strstr($password, $char)) {
1091 // this is called after user is created to initialize default feeds, labels
1094 // user preferences are checked on every login, not here
1096 function initialize_user($link, $uid) {
1098 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1099 values ('$uid','unread = true', 'Unread articles')");
1101 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1102 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1104 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1105 values ('$uid', 'Tiny Tiny RSS: New Releases',
1106 'http://tt-rss.spb.ru/releases.rss')");
1110 function logout_user() {
1112 if (isset($_COOKIE[session_name()])) {
1113 setcookie(session_name(), '', time()-42000, '/');
1117 function get_script_urlpath() {
1118 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1121 function get_login_redirect() {
1122 $server = $_SERVER["SERVER_NAME"];
1124 if (ENABLE_LOGIN_SSL
) {
1125 $protocol = "https";
1130 $url_path = get_script_urlpath();
1132 $redirect_uri = "$protocol://$server$url_path/login.php";
1134 return $redirect_uri;
1137 function validate_session($link) {
1138 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1139 if ($_SESSION["ip_address"]) {
1140 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1148 function basic_nosid_redirect_check() {
1149 if (!SINGLE_USER_MODE
) {
1150 if (!$_COOKIE[get_session_cookie_name()]) {
1151 $redirect_uri = get_login_redirect();
1152 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1153 header("Location: $redirect_uri?rt=$return_to");
1159 function login_sequence($link) {
1160 if (!SINGLE_USER_MODE
) {
1162 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1163 $swu = db_escape_string($_REQUEST["swu"]);
1165 $_SESSION["prefs_cache"] = false;
1166 return authenticate_user($link, $swu, null, true);
1170 if (!validate_session($link)) {
1172 $redirect_uri = get_login_redirect();
1173 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1174 header("Location: $redirect_uri?rt=$return_to");
1178 if (!USE_HTTP_AUTH
) {
1179 if (!$_SESSION["uid"]) {
1180 $redirect_uri = get_login_redirect();
1181 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1182 header("Location: $redirect_uri?rt=$return_to");
1186 if (!$_SESSION["uid"]) {
1187 if (!$_SERVER["PHP_AUTH_USER"]) {
1189 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1190 header('HTTP/1.0 401 Unauthorized');
1194 $auth_result = authenticate_user($link,
1195 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1197 if (!$auth_result) {
1198 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1199 header('HTTP/1.0 401 Unauthorized');
1206 return authenticate_user($link, "admin", null);
1210 function truncate_string($str, $max_len) {
1211 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1212 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1218 function get_user_theme_path($link) {
1219 $result = db_query($link, "SELECT theme_path
1221 ttrss_themes,ttrss_users
1222 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1223 if (db_num_rows($result) != 0) {
1224 return db_fetch_result($result, 0, "theme_path");
1230 function smart_date_time($timestamp) {
1231 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1232 return date("G:i", $timestamp);
1233 } else if (date("Y", $timestamp) == date("Y")) {
1234 return date("M d, G:i", $timestamp);
1236 return date("Y/m/d G:i", $timestamp);
1240 function smart_date($timestamp) {
1241 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1243 } else if (date("Y", $timestamp) == date("Y")) {
1244 return date("D m", $timestamp);
1246 return date("Y/m/d", $timestamp);
1250 function sql_bool_to_string($s) {
1251 if ($s == "t" ||
$s == "1") {
1258 function sql_bool_to_bool($s) {
1259 if ($s == "t" ||
$s == "1") {
1267 function toggleEvenOdd($a) {
1274 function sanity_check($link) {
1279 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1280 $schema_version = db_fetch_result($result, 0, "schema_version");
1282 if ($schema_version != SCHEMA_VERSION
) {
1286 if (DB_TYPE
== "mysql") {
1287 $result = db_query($link, "SELECT true", false);
1288 if (db_num_rows($result) != 1) {
1293 error_reporting (DEFAULT_ERROR_LEVEL
);
1295 if ($error_code != 0) {
1296 print_error_xml($error_code);
1303 function file_is_locked($filename) {
1305 $fp = fopen($filename, "r");
1306 error_reporting(DEFAULT_ERROR_LEVEL
);
1308 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1309 flock($fp, LOCK_UN
);
1319 function make_lockfile($filename) {
1320 $fp = fopen($filename, "w");
1322 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1329 function sql_random_function() {
1330 if (DB_TYPE
== "mysql") {
1337 function catchup_feed($link, $feed, $cat_view) {
1339 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1344 $cat_qpart = "cat_id = '$feed'";
1346 $cat_qpart = "cat_id IS NULL";
1349 $tmp_result = db_query($link, "SELECT id
1350 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1353 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1355 $tmp_feed = $tmp_line["id"];
1357 db_query($link, "UPDATE ttrss_user_entries
1358 SET unread = false,last_read = NOW()
1359 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1362 } else if ($feed > 0) {
1364 $tmp_result = db_query($link, "SELECT id
1365 FROM ttrss_feeds WHERE parent_feed = '$feed'
1366 ORDER BY cat_id,title");
1368 $parent_ids = array();
1370 if (db_num_rows($tmp_result) > 0) {
1371 while ($p = db_fetch_assoc($tmp_result)) {
1372 array_push($parent_ids, "feed_id = " . $p["id"]);
1375 $children_qpart = implode(" OR ", $parent_ids);
1377 db_query($link, "UPDATE ttrss_user_entries
1378 SET unread = false,last_read = NOW()
1379 WHERE (feed_id = '$feed' OR $children_qpart)
1380 AND owner_uid = " . $_SESSION["uid"]);
1383 db_query($link, "UPDATE ttrss_user_entries
1384 SET unread = false,last_read = NOW()
1385 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1388 } else if ($feed < 0 && $feed > -10) { // special, like starred
1391 db_query($link, "UPDATE ttrss_user_entries
1392 SET unread = false,last_read = NOW()
1393 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1396 } else if ($feed < -10) { // label
1398 // TODO make this more efficient
1400 $label_id = -$feed - 11;
1402 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1403 WHERE id = '$label_id'");
1406 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1408 db_query($link, "BEGIN");
1410 $tmp2_result = db_query($link,
1414 ttrss_user_entries,ttrss_entries,ttrss_feeds
1416 ref_id = ttrss_entries.id AND
1417 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1419 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1421 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1422 db_query($link, "UPDATE
1425 unread = false, last_read = NOW()
1427 int_id = " . $tmp_line["int_id"]);
1430 db_query($link, "COMMIT");
1432 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1433 SET unread = false,last_read = NOW()
1436 AND owner_uid = ".$_SESSION["uid"]); */
1440 db_query($link, "BEGIN");
1442 $tag_name = db_escape_string($feed);
1444 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1445 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1447 while ($line = db_fetch_assoc($result)) {
1448 db_query($link, "UPDATE ttrss_user_entries SET
1449 unread = false, last_read = NOW()
1450 WHERE int_id = " . $line["post_int_id"]);
1452 db_query($link, "COMMIT");
1456 function update_generic_feed($link, $feed, $cat_view) {
1460 $cat_qpart = "cat_id = '$feed'";
1462 $cat_qpart = "cat_id IS NULL";
1465 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1466 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1468 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1469 $feed_url = $tmp_line["feed_url"];
1470 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1474 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1475 WHERE id = '$feed'");
1476 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1477 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1481 function getAllCounters($link) {
1482 getLabelCounters($link);
1483 getFeedCounters($link);
1484 getTagCounters($link);
1485 getGlobalCounters($link);
1486 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1487 getCategoryCounters($link);
1491 function getCategoryCounters($link) {
1492 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1493 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1494 AND unread = true)) AS unread FROM ttrss_feeds
1496 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1498 while ($line = db_fetch_assoc($result)) {
1499 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1500 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1501 $line["unread"]."\"/>";
1505 function getCategoryUnread($link, $cat) {
1508 $cat_query = "cat_id = '$cat'";
1510 $cat_query = "cat_id IS NULL";
1513 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1515 AND owner_uid = " . $_SESSION["uid"]);
1517 $cat_feeds = array();
1518 while ($line = db_fetch_assoc($result)) {
1519 array_push($cat_feeds, "feed_id = " . $line["id"]);
1522 if (count($cat_feeds) == 0) return 0;
1524 $match_part = implode(" OR ", $cat_feeds);
1526 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1527 FROM ttrss_user_entries
1528 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1532 # this needs to be rewritten
1533 while ($line = db_fetch_assoc($result)) {
1534 $unread +
= $line["unread"];
1541 function getFeedUnread($link, $feed, $is_cat = false) {
1542 $n_feed = sprintf("%d", $feed);
1545 return getCategoryUnread($link, $n_feed);
1546 } else if ($n_feed == -1) {
1547 $match_part = "marked = true";
1548 } else if ($n_feed > 0) {
1550 $result = db_query($link, "SELECT id FROM ttrss_feeds
1551 WHERE parent_feed = '$n_feed'
1553 AND owner_uid = " . $_SESSION["uid"]);
1555 if (db_num_rows($result) > 0) {
1557 $linked_feeds = array();
1558 while ($line = db_fetch_assoc($result)) {
1559 array_push($linked_feeds, "feed_id = " . $line["id"]);
1562 array_push($linked_feeds, "feed_id = $n_feed");
1564 $match_part = implode(" OR ", $linked_feeds);
1566 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1567 FROM ttrss_user_entries
1568 WHERE unread = true AND ($match_part)
1569 AND owner_uid = " . $_SESSION["uid"]);
1573 # this needs to be rewritten
1574 while ($line = db_fetch_assoc($result)) {
1575 $unread +
= $line["unread"];
1581 $match_part = "feed_id = '$n_feed'";
1583 } else if ($feed < -10) {
1585 $label_id = -$feed - 11;
1587 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1588 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1590 $match_part = db_fetch_result($result, 0, "sql_exp");
1595 $result = db_query($link, "SELECT count(int_id) AS unread
1596 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1597 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1598 ttrss_user_entries.ref_id = ttrss_entries.id AND
1599 ttrss_feeds.hidden = false AND
1600 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1604 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1605 FROM ttrss_tags,ttrss_user_entries
1606 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1607 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1610 $unread = db_fetch_result($result, 0, "unread");
1615 /* FIXME this needs reworking */
1617 function getGlobalUnread($link, $user_id = false) {
1620 $user_id = $_SESSION["uid"];
1623 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1624 WHERE unread = true AND
1625 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1626 ttrss_user_entries.ref_id = ttrss_entries.id AND
1628 ttrss_user_entries.owner_uid = '$user_id'");
1629 $c_id = db_fetch_result($result, 0, "c_id");
1633 function getGlobalCounters($link, $global_unread = -1) {
1634 if ($global_unread == -1) {
1635 $global_unread = getGlobalUnread($link);
1637 print "<counter type=\"global\" id='global-unread'
1638 counter='$global_unread'/>";
1640 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1641 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1643 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1645 print "<counter type=\"global\" id='subscribed-feeds'
1646 counter='$subscribed_feeds'/>";
1650 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1653 if (!$_SESSION["tctr_last_value"]) {
1654 $_SESSION["tctr_last_value"] = array();
1658 $old_counters = $_SESSION["tctr_last_value"];
1660 $tctrs_modified = false;
1662 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1663 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1664 ttrss_user_entries.ref_id = ttrss_entries.id AND
1665 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1666 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1668 select tag_name,0 as count FROM ttrss_tags
1669 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1671 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1672 FROM ttrss_user_entries WHERE int_id = post_int_id
1673 AND unread = true)) AS count FROM ttrss_tags
1674 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1678 while ($line = db_fetch_assoc($result)) {
1679 $tags[$line["tag_name"]] +
= $line["count"];
1682 foreach (array_keys($tags) as $tag) {
1683 $unread = $tags[$tag];
1685 $tag = htmlspecialchars($tag);
1687 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1688 $old_counters[$tag] = $unread;
1689 $tctrs_modified = true;
1690 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1695 if ($smart_mode && $tctrs_modified) {
1696 $_SESSION["tctr_last_value"] = $old_counters;
1701 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1704 if (!$_SESSION["lctr_last_value"]) {
1705 $_SESSION["lctr_last_value"] = array();
1711 $old_counters = $_SESSION["lctr_last_value"];
1712 $lctrs_modified = false;
1714 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1715 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1716 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1717 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1719 $count = db_fetch_result($result, 0, "count");
1722 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1724 $ret_arr["-1"]["counter"] = $count;
1725 $ret_arr["-1"]["description"] = "Starred";
1728 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1729 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1731 while ($line = db_fetch_assoc($result)) {
1733 $id = -$line["id"] - 11;
1735 $label_name = $line["description"];
1737 error_reporting (0);
1739 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1740 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1741 ttrss_feeds.hidden = false AND
1742 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1743 ttrss_user_entries.ref_id = ttrss_entries.id AND
1744 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1746 $count = db_fetch_result($tmp_result, 0, "count");
1748 if (!$smart_mode ||
$old_counters[$id] != $count) {
1749 $old_counters[$id] = $count;
1750 $lctrs_modified = true;
1752 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1754 $ret_arr[$id]["counter"] = $count;
1755 $ret_arr[$id]["description"] = $label_name;
1759 error_reporting (DEFAULT_ERROR_LEVEL
);
1762 if ($smart_mode && $lctrs_modified) {
1763 $_SESSION["lctr_last_value"] = $old_counters;
1769 /* function getFeedCounter($link, $id) {
1771 $result = db_query($link, "SELECT
1772 count(id) as count,last_error
1773 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1774 WHERE feed_id = '$id' AND unread = true
1775 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1776 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1778 $count = db_fetch_result($result, 0, "count");
1779 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1781 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1784 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1787 if (!$_SESSION["fctr_last_value"]) {
1788 $_SESSION["fctr_last_value"] = array();
1792 $old_counters = $_SESSION["fctr_last_value"];
1794 $result = db_query($link, "SELECT id,last_error,parent_feed,
1795 SUBSTRING(last_updated,1,19) AS last_updated,
1797 FROM ttrss_entries,ttrss_user_entries
1798 WHERE feed_id = ttrss_feeds.id AND
1799 ttrss_user_entries.ref_id = ttrss_entries.id
1800 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1801 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1802 AND parent_feed IS NULL");
1804 $fctrs_modified = false;
1806 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1808 while ($line = db_fetch_assoc($result)) {
1811 $count = $line["count"];
1812 $last_error = htmlspecialchars($line["last_error"]);
1814 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1815 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1817 $last_updated = date($short_date, strtotime($line["last_updated"]));
1820 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1822 $tmp_result = db_query($link,
1823 "SELECT id,COUNT(unread) AS unread
1824 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1825 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1826 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1828 if (db_num_rows($tmp_result) > 0) {
1829 while ($l = db_fetch_assoc($tmp_result)) {
1830 $count +
= $l["unread"];
1834 if (!$smart_mode ||
$old_counters[$id] != $count) {
1835 $old_counters[$id] = $count;
1836 $fctrs_modified = true;
1839 $error_part = "error=\"$last_error\"";
1845 $has_img_part = "hi=\"$has_img\"";
1850 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1854 if ($smart_mode && $fctrs_modified) {
1855 $_SESSION["fctr_last_value"] = $old_counters;
1859 function get_script_dt_add() {
1860 if (strpos(VERSION
, "99") === false) {
1867 function get_pgsql_version($link) {
1868 $result = db_query($link, "SELECT version() AS version");
1869 $version = split(" ", db_fetch_result($result, 0, "version"));
1873 function print_error_xml($code, $add_msg = "") {
1876 $error_msg = $ERRORS[$code];
1879 $error_msg = "$error_msg; $add_msg";
1882 print "<rpc-reply>";
1883 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1884 print "</rpc-reply>";
1887 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1889 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
1891 if ($feed_link == "") return;
1893 if ($cat_id == "0" ||
!$cat_id) {
1894 $cat_qpart = "NULL";
1896 $cat_qpart = "'$cat_id'";
1899 $result = db_query($link,
1900 "SELECT id FROM ttrss_feeds
1901 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1903 if (db_num_rows($result) == 0) {
1905 $result = db_query($link,
1906 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1907 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1908 '[Unknown]', $cat_qpart)");
1910 $result = db_query($link,
1911 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1912 AND owner_uid = " . $_SESSION["uid"]);
1914 $feed_id = db_fetch_result($result, 0, "id");
1917 update_rss_feed($link, $feed_link, $feed_id, true);
1926 function print_feed_select($link, $id, $default_id = "",
1927 $attributes = "", $include_all_feeds = true) {
1929 print "<select id=\"$id\" name=\"$id\" $attributes>";
1930 if ($include_all_feeds) {
1931 print "<option value=\"0\">All feeds</option>";
1934 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1935 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1937 if (db_num_rows($result) > 0 && $include_all_feeds) {
1938 print "<option disabled>--------</option>";
1941 while ($line = db_fetch_assoc($result)) {
1942 if ($line["id"] == $default_id) {
1943 $is_selected = "selected";
1947 printf("<option $is_selected value='%d'>%s</option>",
1948 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1954 function print_feed_cat_select($link, $id, $default_id = "",
1955 $attributes = "", $include_all_cats = true) {
1957 print "<select id=\"$id\" name=\"$id\" $attributes>";
1959 if ($include_all_cats) {
1960 print "<option value=\"0\">Uncategorized</option>";
1963 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1964 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1966 if (db_num_rows($result) > 0 && $include_all_cats) {
1967 print "<option disabled>--------</option>";
1970 while ($line = db_fetch_assoc($result)) {
1971 if ($line["id"] == $default_id) {
1972 $is_selected = "selected";
1976 printf("<option $is_selected value='%d'>%s</option>",
1977 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1983 function checkbox_to_sql_bool($val) {
1984 return ($val == "on") ?
"true" : "false";
1987 function getFeedCatTitle($link, $id) {
1990 } else if ($id < -10) {
1992 } else if ($id > 0) {
1993 $result = db_query($link, "SELECT ttrss_feed_categories.title
1994 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1995 cat_id = ttrss_feed_categories.id");
1996 if (db_num_rows($result) == 1) {
1997 return db_fetch_result($result, 0, "title");
1999 return "Uncategorized";
2002 return "getFeedCatTitle($id) failed";
2007 function getFeedTitle($link, $id) {
2009 return "Starred articles";
2010 } else if ($id < -10) {
2011 $label_id = -10 - $id;
2012 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2013 if (db_num_rows($result) == 1) {
2014 return db_fetch_result($result, 0, "description");
2016 return "Unknown label ($label_id)";
2019 } else if ($id > 0) {
2020 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2021 if (db_num_rows($result) == 1) {
2022 return db_fetch_result($result, 0, "title");
2024 return "Unknown feed ($id)";
2027 return "getFeedTitle($id) failed";
2032 function get_session_cookie_name() {
2033 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2036 function print_init_params($link) {
2037 print "<init-params>";
2038 if ($_SESSION["stored-params"]) {
2039 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2041 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2042 print "<param key=\"$key\" value=\"$value\"/>";
2047 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2048 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2049 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2051 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2052 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2054 print "<param key=\"hide_read_feeds\" value=\"" .
2055 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2057 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2058 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2060 print "<param key=\"confirm_feed_catchup\" value=\"" .
2061 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2063 print "</init-params>";
2066 function print_runtime_info($link) {
2067 print "<runtime-info>";
2068 if (ENABLE_UPDATE_DAEMON
) {
2069 print "<param key=\"daemon_is_running\" value=\"".
2070 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2072 print "</runtime-info>";
2075 function getSearchSql($search, $match_on) {
2077 $search_query_part = "";
2079 $keywords = split(" ", $search);
2080 $query_keywords = array();
2082 if ($match_on == "both") {
2084 foreach ($keywords as $k) {
2085 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2086 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2089 $search_query_part = implode("AND", $query_keywords) . " AND ";
2091 } else if ($match_on == "title") {
2093 foreach ($keywords as $k) {
2094 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2097 $search_query_part = implode("AND", $query_keywords) . " AND ";
2099 } else if ($match_on == "content") {
2101 foreach ($keywords as $k) {
2102 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2106 $search_query_part = implode("AND", $query_keywords);
2108 return $search_query_part;
2111 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2114 $search_query_part = getSearchSql($search, $match_on);
2115 $search_query_part .= " AND ";
2118 $search_query_part = "";
2121 $view_query_part = "";
2123 if ($view_mode == "adaptive") {
2125 $view_query_part = " ";
2126 } else if ($feed != -1) {
2127 $unread = getFeedUnread($link, $feed, $cat_view);
2129 $view_query_part = " unread = true AND ";
2134 if ($view_mode == "marked") {
2135 $view_query_part = " marked = true AND ";
2138 if ($view_mode == "unread") {
2139 $view_query_part = " unread = true AND ";
2143 $limit_query_part = "LIMIT " . $limit;
2146 $vfeed_query_part = "";
2148 // override query strategy and enable feed display when searching globally
2149 if ($search && $search_mode == "all_feeds") {
2150 $query_strategy_part = "ttrss_entries.id > 0";
2151 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2152 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2153 $query_strategy_part = "ttrss_entries.id > 0";
2154 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2155 id = feed_id) as feed_title,";
2156 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2158 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2160 $tmp_result = false;
2163 $tmp_result = db_query($link, "SELECT id
2164 FROM ttrss_feeds WHERE cat_id = '$feed'");
2166 $tmp_result = db_query($link, "SELECT id
2167 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2168 WHERE id = '$feed') AND id != '$feed'");
2171 $cat_siblings = array();
2173 if (db_num_rows($tmp_result) > 0) {
2174 while ($p = db_fetch_assoc($tmp_result)) {
2175 array_push($cat_siblings, "feed_id = " . $p["id"]);
2178 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2179 $feed, implode(" OR ", $cat_siblings));
2182 $query_strategy_part = "ttrss_entries.id > 0";
2185 } else if ($feed >= 0) {
2190 $query_strategy_part = "cat_id = '$feed'";
2192 $query_strategy_part = "cat_id IS NULL";
2195 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2198 $tmp_result = db_query($link, "SELECT id
2199 FROM ttrss_feeds WHERE parent_feed = '$feed'
2200 ORDER BY cat_id,title");
2202 $parent_ids = array();
2204 if (db_num_rows($tmp_result) > 0) {
2205 while ($p = db_fetch_assoc($tmp_result)) {
2206 array_push($parent_ids, "feed_id = " . $p["id"]);
2209 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2210 $feed, implode(" OR ", $parent_ids));
2212 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2214 $query_strategy_part = "feed_id = '$feed'";
2217 } else if ($feed == -1) { // starred virtual feed
2218 $query_strategy_part = "marked = true";
2219 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2220 } else if ($feed <= -10) { // labels
2221 $label_id = -$feed - 11;
2223 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2224 WHERE id = '$label_id'");
2226 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2228 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2230 $query_strategy_part = "id > 0"; // dumb
2233 if (get_pref($link, 'REVERSE_HEADLINES')) {
2234 $order_by = "updated";
2236 $order_by = "updated DESC";
2239 if ($override_order) {
2240 $order_by = $override_order;
2245 if ($search && $search_mode == "all_feeds") {
2246 $feed_title = "Global search results ($search)";
2247 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2248 $feed_title = "Tag search results ($search, $feed)";
2249 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2250 $feed_title = $feed;
2251 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2256 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2257 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2258 $feed_title = db_fetch_result($result, 0, "title");
2260 $feed_title = "Uncategorized";
2264 $feed_title = "Category search results ($search, $feed_title)";
2269 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2270 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2272 $feed_title = db_fetch_result($result, 0, "title");
2273 $feed_site_url = db_fetch_result($result, 0, "site_url");
2274 $last_error = db_fetch_result($result, 0, "last_error");
2277 $feed_title = "Feed search results ($search, $feed_title)";
2281 } else if ($feed == -1) {
2282 $feed_title = "Starred articles";
2283 } else if ($feed < -10) {
2284 $label_id = -$feed - 11;
2285 $result = db_query($link, "SELECT description FROM ttrss_labels
2286 WHERE id = '$label_id'");
2287 $feed_title = db_fetch_result($result, 0, "description");
2290 $feed_title = "Label search results ($search, $feed_title)";
2296 $feed_title = db_unescape_string($feed_title);
2298 if ($feed < -10) error_reporting (0);
2300 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2303 $feed_kind = "Feeds";
2305 $feed_kind = "Labels";
2308 $content_query_part = "content as content_preview,";
2312 ttrss_entries.id,ttrss_entries.title,
2313 SUBSTRING(updated,1,16) as updated,
2314 unread,feed_id,marked,link,last_read,
2315 SUBSTRING(last_read,1,19) as last_read_noms,
2318 SUBSTRING(updated,1,19) as updated_noms
2320 ttrss_entries,ttrss_user_entries,ttrss_feeds
2322 ttrss_feeds.hidden = false AND
2323 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2324 ttrss_user_entries.ref_id = ttrss_entries.id AND
2325 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2328 $query_strategy_part ORDER BY $order_by
2331 $result = db_query($link, $query);
2333 if ($_GET["debug"]) print $query;
2338 $feed_kind = "Tags";
2340 $result = db_query($link, "SELECT
2342 ttrss_entries.id as id,title,
2343 SUBSTRING(updated,1,16) as updated,
2345 marked,link,last_read,
2346 SUBSTRING(last_read,1,19) as last_read_noms,
2349 SUBSTRING(updated,1,19) as updated_noms
2351 ttrss_entries,ttrss_user_entries,ttrss_tags
2353 ref_id = ttrss_entries.id AND
2354 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2355 post_int_id = int_id AND tag_name = '$feed' AND
2358 $query_strategy_part ORDER BY $order_by
2359 $limit_query_part");
2362 return array($result, $feed_title, $feed_site_url, $last_error);
2366 function generate_syndicated_feed($link, $feed, $is_cat,
2367 $search, $search_mode, $match_on, $output_format = "RSS2.0") {
2369 $qfh_ret = queryFeedHeadlines($link, $feed,
2370 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2372 $result = $qfh_ret[0];
2373 $feed_title = htmlspecialchars($qfh_ret[1]);
2374 $feed_site_url = $qfh_ret[2];
2375 $last_error = $qfh_ret[3];
2377 $rss = new UniversalFeedCreator();
2379 $rss->title
= $feed_title;
2380 $rss->link
= $feed_site_url;
2382 while ($line = db_fetch_assoc($result)) {
2384 $item = new FeedItem();
2385 $item->title
= $line["title"];
2386 $item->description
= $line["content_preview"];
2387 $item->date
= strtotime($line["updated"]);
2388 $item->guid
= $line["guid"];
2389 $item->link
= $line['link'];
2391 $rss->addItem($item);
2394 print $rss->createFeed($output_format);
2398 function getCategoryTitle($link, $cat_id) {
2400 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2403 if (db_num_rows($result) == 1) {
2404 return db_fetch_result($result, 0, "title");
2406 return "Uncategorized";
2410 function sanitize_rss($str) {
2413 $res = preg_replace('/<script.*?>/i',
2414 "<p class=\"scriptWarn\">Disabled script: ", $res);
2416 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2418 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2420 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2421 "<p class=\"objectWarn\">(Disabled html object
2422 - flash or other embedded content)</p>", $res); */
2427 function send_headlines_digests($link, $limit = 100) {
2429 if (!DIGEST_ENABLE
) return false;
2431 $user_limit = DIGEST_EMAIL_LIMIT
;
2434 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2436 if (DB_TYPE
== "pgsql") {
2437 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2438 } else if (DB_TYPE
== "mysql") {
2439 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2442 $result = db_query($link, "SELECT id,email FROM ttrss_users
2443 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2445 while ($line = db_fetch_assoc($result)) {
2446 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2447 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2449 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2450 $digest = $tuple[0];
2451 $headlines_count = $tuple[1];
2453 if ($headlines_count > 0) {
2454 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2455 "[tt-rss] New headlines for last 24 hours", $digest,
2456 "From: " . MAIL_FROM
. "\n".
2457 "Content-Type: text/plain; charset=\"utf-8\"\n".
2458 "Content-Transfer-Encoding: 8bit\n");
2460 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2461 WHERE id = " . $line["id"]);
2463 print "No headlines\n";
2468 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2472 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2473 $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n";
2474 $tmp .= "=======================================================\n\n";
2476 if (DB_TYPE
== "pgsql") {
2477 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2478 } else if (DB_TYPE
== "mysql") {
2479 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2482 $result = db_query($link, "SELECT ttrss_entries.title,
2483 ttrss_feeds.title AS feed_title,
2486 SUBSTRING(last_updated,1,19) AS last_updated
2488 ttrss_user_entries,ttrss_entries,ttrss_feeds
2490 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2491 AND include_in_digest = true
2493 AND ttrss_user_entries.owner_uid = $user_id
2494 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2497 $cur_feed_title = "";
2499 $headlines_count = db_num_rows($result);
2501 while ($line = db_fetch_assoc($result)) {
2502 $updated = smart_date_time(strtotime($line["last_updated"]));
2503 $feed_title = $line["feed_title"];
2505 if ($cur_feed_title != $feed_title) {
2506 $cur_feed_title = $feed_title;
2508 $tmp .= "$feed_title\n\n";
2511 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2512 $tmp .= " " . trim($line["link"]) . "\n";
2517 $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " .
2518 DIGEST_HOSTNAME
. "\n".
2519 "To unsubscribe, visit your configuration options or contact instance owner.\n";
2522 return array($tmp, $headlines_count);
2525 function check_for_update($link) {
2526 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2528 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2533 $rss = fetch_rss($releases_feed);
2534 error_reporting (DEFAULT_ERROR_LEVEL
);
2538 $items = $rss->items
;
2540 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2541 if (!$items ||
!is_array($items)) $items = $rss;
2543 if (!is_array($items) ||
count($items) == 0) {
2547 $latest_item = $items[0];
2549 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2551 $release_url = sanitize_rss($latest_item["link"]);
2552 $content = sanitize_rss($latest_item["description"]);
2554 if (version_compare(VERSION
, $latest_version) == -1) {
2555 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2556 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2557 <div id=\"milestoneDetails\">$content</div></div>";
2562 function markArticlesById($link, $ids, $cmode) {
2566 foreach ($ids as $id) {
2567 array_push($tmp_ids, "ref_id = '$id'");
2570 $ids_qpart = join(" OR ", $tmp_ids);
2573 db_query($link, "UPDATE ttrss_user_entries SET
2574 marked = false,last_read = NOW()
2575 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2576 } else if ($cmode == 1) {
2577 db_query($link, "UPDATE ttrss_user_entries SET
2579 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2581 db_query($link, "UPDATE ttrss_user_entries SET
2582 marked = NOT marked,last_read = NOW()
2583 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2587 function catchupArticlesById($link, $ids, $cmode) {
2591 foreach ($ids as $id) {
2592 array_push($tmp_ids, "ref_id = '$id'");
2595 $ids_qpart = join(" OR ", $tmp_ids);
2598 db_query($link, "UPDATE ttrss_user_entries SET
2599 unread = false,last_read = NOW()
2600 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2601 } else if ($cmode == 1) {
2602 db_query($link, "UPDATE ttrss_user_entries SET
2604 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2606 db_query($link, "UPDATE ttrss_user_entries SET
2607 unread = NOT unread,last_read = NOW()
2608 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2612 function escape_for_form($s) {
2613 return htmlspecialchars(db_unescape_string($s));
2616 function make_guid_from_title($title) {
2617 return preg_replace("/[ \"\',.:;]/", "-",
2618 mb_strtolower(strip_tags($title)));
2621 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2622 $bottom = false, $rtl_content = false, $feed_id = 0,
2623 $is_cat = false, $search = false, $match_on = false,
2624 $search_mode = false) {
2627 $class = "headlinesSubToolbar";
2628 $tid = "headlineActionsTop";
2630 $class = "headlinesSubToolbar";
2631 $tid = "headlineActionsBottom";
2634 print "<table class=\"$class\" id=\"$tid\"
2635 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2643 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2645 print "<td class=\"headlineActions$rtl_cpart\">
2647 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
2648 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
2649 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
2651 Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>,
2652 <a href=\"javascript:selectionToggleMarked()\">Starred</a>
2655 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2656 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2659 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2660 print "<td class=\"headlineActions$rtl_cpart\">
2661 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2662 '$match_on', '$feed_id', '$is_cat');\">
2663 Convert this search to label</a></td>";
2668 print "<td class=\"headlineActions$rtl_cpart\">
2670 <a href=\"javascript:cdmSelectArticles('all')\">All</a>,
2671 <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
2672 <a href=\"javascript:cdmSelectArticles('none')\">None</a>
2674 Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>,
2675 <a href=\"javascript:selectionToggleMarked(true)\">Starred</a>
2678 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2679 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2685 print "<td class=\"headlineTitle$rtl_cpart\">";
2687 if ($feed_site_url) {
2689 $target = "target=\"_blank\"";
2691 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2697 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2703 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2704 <img class=\"noborder\"
2705 alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\">
2710 print "</tr></table>";
2714 function outputFeedList($link, $tags = false) {
2716 print "<ul class=\"feedList\" id=\"feedList\">\n";
2718 $owner_uid = $_SESSION["uid"];
2724 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2725 print "<li class=\"feedCat\">Special</li>";
2726 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2729 $num_starred = getFeedUnread($link, -1);
2733 if ($num_starred > 0) $class .= "Unread";
2735 printFeedEntry(-1, $class, "Starred articles", $num_starred,
2736 "images/mark_set.png", $link);
2738 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2742 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
2744 $result = db_query($link, "SELECT id,sql_exp,description FROM
2745 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2747 if (db_num_rows($result) > 0) {
2748 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2749 print "<li class=\"feedCat\">Labels</li>";
2750 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2752 print "<li><hr></li>";
2756 while ($line = db_fetch_assoc($result)) {
2758 error_reporting (0);
2760 $label_id = -$line['id'] - 11;
2761 $count = getFeedUnread($link, $label_id);
2769 error_reporting (DEFAULT_ERROR_LEVEL
);
2771 printFeedEntry($label_id,
2772 $class, db_unescape_string($line["description"]),
2773 $count, "images/label.png", $link);
2777 if (db_num_rows($result) > 0) {
2778 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2785 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2786 print "<li><hr></li>";
2789 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2790 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2791 $order_by_qpart = "category,unread DESC,title";
2793 $order_by_qpart = "category,title";
2796 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2797 $order_by_qpart = "unread DESC,title";
2799 $order_by_qpart = "title";
2803 $result = db_query($link, "SELECT ttrss_feeds.*,
2804 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2805 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2806 WHERE feed_id = ttrss_feeds.id AND unread = true
2807 AND ttrss_user_entries.ref_id = ttrss_entries.id
2808 AND owner_uid = '$owner_uid') as unread,
2810 ttrss_feed_categories.title AS category,
2811 ttrss_feed_categories.collapsed
2812 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2813 ON (ttrss_feed_categories.id = cat_id)
2815 ttrss_feeds.hidden = false AND
2816 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2817 ORDER BY $order_by_qpart");
2819 $actid = $_GET["actid"];
2829 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2831 while ($line = db_fetch_assoc($result)) {
2833 $feed = db_unescape_string($line["title"]);
2834 $feed_id = $line["id"];
2836 $subop = $_GET["subop"];
2838 $unread = $line["unread"];
2840 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2841 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2843 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2846 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2849 $rtl_tag = "dir=\"RTL\"";
2854 $tmp_result = db_query($link,
2855 "SELECT id,COUNT(unread) AS unread
2856 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2857 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2858 WHERE parent_feed = '$feed_id' AND unread = true
2859 GROUP BY ttrss_feeds.id");
2861 if (db_num_rows($tmp_result) > 0) {
2862 while ($l = db_fetch_assoc($tmp_result)) {
2863 $unread +
= $l["unread"];
2867 $cat_id = $line["cat_id"];
2869 $tmp_category = $line["category"];
2871 if (!$tmp_category) {
2872 $tmp_category = "Uncategorized";
2875 // $class = ($lnum % 2) ? "even" : "odd";
2877 if ($line["last_error"]) {
2883 if ($unread > 0) $class .= "Unread";
2885 if ($actid == $feed_id) {
2886 $class .= "Selected";
2889 $total_unread +
= $unread;
2891 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
2897 $category = $tmp_category;
2899 $collapsed = $line["collapsed"];
2901 // workaround for NULL category
2902 if ($category == "Uncategorized") {
2903 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
2908 if ($collapsed == "t" ||
$collapsed == "1") {
2909 $holder_class = "invisible";
2916 $cat_id = sprintf("%d", $cat_id);
2918 $cat_unread = getCategoryUnread($link, $cat_id);
2920 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
2921 <a id=\"FCATN-$cat_id\" href=\"#\" onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
2922 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
2923 <span id=\"FCATCTR-$cat_id\"
2924 class=\"$catctr_class\">($cat_unread unread)$ellipsis</span>
2927 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
2928 // -> keyboard navigation, etc.
2929 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
2932 printFeedEntry($feed_id, $class, $feed, $unread,
2933 "icons/$feed_id.ico", $link, $rtl_content,
2934 $last_updated, $line["last_error"]);
2939 if (db_num_rows($result) == 0) {
2940 print "<li>No feeds to display.</li>";
2947 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2948 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2949 post_int_id = ttrss_user_entries.int_id AND
2950 unread = true AND ref_id = ttrss_entries.id
2951 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
2953 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
2954 ORDER BY tag_name"); */
2956 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2957 print "<li class=\"feedCat\">Tags</li>";
2958 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2961 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2962 FROM ttrss_user_entries WHERE int_id = post_int_id
2963 AND unread = true)) AS count FROM ttrss_tags
2964 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
2968 while ($line = db_fetch_assoc($result)) {
2969 $tags[$line["tag_name"]] +
= $line["count"];
2972 foreach (array_keys($tags) as $tag) {
2974 $unread = $tags[$tag];
2982 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
2986 if (db_num_rows($result) == 0) {
2987 print "<li>No tags to display.</li>";
2990 if (get_pref($link, 'ENABLE_FEED_CATS')) {