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 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
16 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
18 require_once "magpierss/rss_fetch.inc";
19 require_once 'magpierss/rss_utils.inc';
21 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
25 if (DB_TYPE
== "pgsql") {
26 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
27 marked = false AND feed_id = '$feed_id' AND
28 (SELECT date_entered FROM ttrss_entries WHERE
29 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
31 $pg_version = get_pgsql_version($link);
33 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
35 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
36 ttrss_entries.id = ref_id AND
38 feed_id = '$feed_id' AND
39 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
43 $result = db_query($link, "DELETE FROM ttrss_user_entries
45 WHERE ttrss_entries.id = ref_id AND
47 feed_id = '$feed_id' AND
48 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
51 $rows = pg_affected_rows($result);
55 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
56 marked = false AND feed_id = '$feed_id' AND
57 (SELECT date_entered FROM ttrss_entries WHERE
58 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
60 $result = db_query($link, "DELETE FROM ttrss_user_entries
61 USING ttrss_user_entries, ttrss_entries
62 WHERE ttrss_entries.id = ref_id AND
64 feed_id = '$feed_id' AND
65 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
67 $rows = mysql_affected_rows($link);
72 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
76 function global_purge_old_posts($link, $do_output = false, $limit = false) {
78 $random_qpart = sql_random_function();
81 $limit_qpart = "LIMIT $limit";
86 $result = db_query($link,
87 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
88 ORDER BY $random_qpart $limit_qpart");
90 while ($line = db_fetch_assoc($result)) {
92 $feed_id = $line["id"];
93 $purge_interval = $line["purge_interval"];
94 $owner_uid = $line["owner_uid"];
96 if ($purge_interval == 0) {
98 $tmp_result = db_query($link,
99 "SELECT value FROM ttrss_user_prefs WHERE
100 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
102 if (db_num_rows($tmp_result) != 0) {
103 $purge_interval = db_fetch_result($tmp_result, 0, "value");
108 // print "Feed $feed_id: purge interval = $purge_interval\n";
111 if ($purge_interval > 0) {
112 purge_feed($link, $feed_id, $purge_interval, $do_output);
116 // purge orphaned posts in main content table
117 db_query($link, "DELETE FROM ttrss_entries WHERE
118 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
122 function purge_old_posts($link) {
124 $user_id = $_SESSION["uid"];
126 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
127 WHERE owner_uid = '$user_id'");
129 while ($line = db_fetch_assoc($result)) {
131 $feed_id = $line["id"];
132 $purge_interval = $line["purge_interval"];
134 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
136 if ($purge_interval > 0) {
137 purge_feed($link, $feed_id, $purge_interval);
141 // purge orphaned posts in main content table
142 db_query($link, "DELETE FROM ttrss_entries WHERE
143 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
146 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
148 if (WEB_DEMO_MODE
) return;
151 $user_id = $_SESSION["uid"];
152 purge_old_posts($link);
155 // db_query($link, "BEGIN");
157 if (MAX_UPDATE_TIME
> 0) {
158 if (DB_TYPE
== "mysql") {
161 $q_order = "RANDOM()";
164 $q_order = "last_updated DESC";
167 $result = db_query($link, "SELECT feed_url,id,
168 SUBSTRING(last_updated,1,19) AS last_updated,
169 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
174 while ($line = db_fetch_assoc($result)) {
175 $upd_intl = $line["update_interval"];
177 if (!$upd_intl ||
$upd_intl == 0) {
178 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
182 // Updates for this feed are disabled
186 if ($fetch ||
(!$line["last_updated"] ||
187 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
189 // print "<!-- feed: ".$line["feed_url"]." -->";
191 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
193 $upd_elapsed = time() - $upd_start;
195 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
201 // db_query($link, "COMMIT");
205 function fetch_file_contents($url) {
206 if (USE_CURL_FOR_ICONS
) {
207 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
209 $ch = curl_init($url);
210 $fp = fopen($tmpfile, "w");
213 curl_setopt($ch, CURLOPT_FILE
, $fp);
219 $contents = file_get_contents($tmpfile);
225 return file_get_contents($url);
230 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
231 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
233 function get_favicon_url($url) {
235 if ($html = @fetch_file_contents
($url)) {
237 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
238 // Attempt to grab a favicon link from their webpage url
239 $linkUrl = html_entity_decode($matches[1]);
241 if (substr($linkUrl, 0, 1) == '/') {
242 $urlParts = parse_url($url);
243 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
244 } else if (substr($linkUrl, 0, 7) == 'http://') {
245 $faviconURL = $linkUrl;
246 } else if (substr($url, -1, 1) == '/') {
247 $faviconURL = $url.$linkUrl;
249 $faviconURL = $url.'/'.$linkUrl;
253 // If unsuccessful, attempt to "guess" the favicon location
254 $urlParts = parse_url($url);
255 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
259 // Run a test to see if what we have attempted to get actually exists.
260 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
267 function url_validate($link) {
269 $url_parts = @parse_url
($link);
271 if ( empty( $url_parts["host"] ) )
274 if ( !empty( $url_parts["path"] ) ) {
275 $documentpath = $url_parts["path"];
280 if ( !empty( $url_parts["query"] ) )
281 $documentpath .= "?" . $url_parts["query"];
283 $host = $url_parts["host"];
284 $port = $url_parts["port"];
289 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
294 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
296 $http_response = fgets( $socket, 22 );
298 $responses = "/(200 OK)|(30[0-9] Moved)/";
299 if ( preg_match($responses, $http_response) ) {
308 function check_feed_favicon($site_url, $feed, $link) {
309 $favicon_url = get_favicon_url($site_url);
311 # print "FAVICON [$site_url]: $favicon_url\n";
315 $icon_file = ICONS_DIR
. "/$feed.ico";
317 if ($favicon_url && !file_exists($icon_file)) {
318 $contents = fetch_file_contents($favicon_url);
320 $fp = fopen($icon_file, "w");
323 fwrite($fp, $contents);
325 chmod($icon_file, 0644);
329 error_reporting(DEFAULT_ERROR_LEVEL
);
333 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
335 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
339 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
340 FROM ttrss_feeds WHERE id = '$feed'");
342 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
343 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
345 $update_interval = db_fetch_result($result, 0, "update_interval");
347 if ($update_interval < 0) { return; }
349 $feed = db_escape_string($feed);
351 $fetch_url = $feed_url;
353 if ($auth_login && $auth_pass) {
354 $url_parts = array();
355 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
357 if ($url_parts[1] && $url_parts[2]) {
358 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
364 $rss = fetch_rss($fetch_url);
365 error_reporting (DEFAULT_ERROR_LEVEL
);
367 $feed = db_escape_string($feed);
371 // db_query($link, "BEGIN");
373 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
374 FROM ttrss_feeds WHERE id = '$feed'");
376 $registered_title = db_fetch_result($result, 0, "title");
377 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
378 $orig_site_url = db_fetch_result($result, 0, "site_url");
380 $owner_uid = db_fetch_result($result, 0, "owner_uid");
382 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
383 check_feed_favicon($rss->channel
["link"], $feed, $link);
386 if (!$registered_title ||
$registered_title == "[Unknown]") {
388 $feed_title = db_escape_string($rss->channel
["title"]);
390 db_query($link, "UPDATE ttrss_feeds SET
391 title = '$feed_title' WHERE id = '$feed'");
394 $site_url = $rss->channel
["link"];
395 // weird, weird Magpie
396 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
398 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
399 db_query($link, "UPDATE ttrss_feeds SET
400 site_url = '$site_url' WHERE id = '$feed'");
403 // print "I: " . $rss->channel["image"]["url"];
405 $icon_url = $rss->image
["url"];
407 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
408 $icon_url = db_escape_string($icon_url);
409 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
415 $result = db_query($link, "SELECT reg_exp,
416 ttrss_filter_types.name AS name,
417 ttrss_filter_actions.name AS action,
420 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
422 owner_uid = $owner_uid AND
423 ttrss_filter_types.id = filter_type AND
424 ttrss_filter_actions.id = action_id AND
425 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
427 while ($line = db_fetch_assoc($result)) {
428 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
430 $filter["reg_exp"] = $line["reg_exp"];
431 $filter["action"] = $line["action"];
432 $filter["action_param"] = $line["action_param"];
433 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
435 array_push($filters[$line["name"]], $filter);
438 $iterator = $rss->items
;
440 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
441 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
443 if (!is_array($iterator)) {
444 /* db_query($link, "UPDATE ttrss_feeds
445 SET last_error = 'Parse error: can\'t find any articles.'
446 WHERE id = '$feed'"); */
450 foreach ($iterator as $item) {
452 $entry_guid = $item["id"];
454 if (!$entry_guid) $entry_guid = $item["guid"];
455 if (!$entry_guid) $entry_guid = $item["link"];
456 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
458 if (!$entry_guid) continue;
460 $entry_timestamp = "";
462 $rss_2_date = $item['pubdate'];
463 $rss_1_date = $item['dc']['date'];
464 $atom_date = $item['issued'];
465 if (!$atom_date) $atom_date = $item['updated'];
467 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
468 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
469 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
471 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
472 $entry_timestamp = time();
473 $no_orig_date = 'true';
475 $no_orig_date = 'false';
478 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
480 $entry_title = trim(strip_tags($item["title"]));
482 // strange Magpie workaround
483 $entry_link = $item["link_"];
484 if (!$entry_link) $entry_link = $item["link"];
486 if (!$entry_title) continue;
487 # if (!$entry_link) continue;
489 $entry_link = strip_tags($entry_link);
491 $entry_content = $item["content:escaped"];
493 if (!$entry_content) $entry_content = $item["content:encoded"];
494 if (!$entry_content) $entry_content = $item["content"];
495 if (!$entry_content) $entry_content = $item["atom_content"];
496 if (!$entry_content) $entry_content = $item["summary"];
497 if (!$entry_content) $entry_content = $item["description"];
499 // if (!$entry_content) continue;
502 if (is_array($entry_content)) {
503 $entry_content = $entry_content["encoded"];
504 if (!$entry_content) $entry_content = $entry_content["escaped"];
508 // print_r(htmlspecialchars($entry_content));
511 $entry_content_unescaped = $entry_content;
512 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
514 $entry_comments = strip_tags($item["comments"]);
516 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
518 if ($item['author']) {
520 if (is_array($item['author'])) {
522 if (!$entry_author) {
523 $entry_author = db_escape_string(strip_tags($item['author']['name']));
526 if (!$entry_author) {
527 $entry_author = db_escape_string(strip_tags($item['author']['email']));
531 if (!$entry_author) {
532 $entry_author = db_escape_string(strip_tags($item['author']));
536 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
538 $entry_guid = db_escape_string(strip_tags($entry_guid));
540 $result = db_query($link, "SELECT id FROM ttrss_entries
541 WHERE guid = '$entry_guid'");
543 $entry_content = db_escape_string($entry_content);
544 $entry_title = db_escape_string($entry_title);
545 $entry_link = db_escape_string($entry_link);
546 $entry_comments = db_escape_string($entry_comments);
548 $num_comments = db_escape_string($item["slash"]["comments"]);
550 if (!$num_comments) $num_comments = 0;
552 /* $dc_subject = $item['dc']['subject'];
554 $subject_tags = false;
556 if (is_array($dc_subject)) {
557 $subject_tags = $dc_subject;
558 } else if ($dc_subject) {
559 $subject_tags = array($dc_subject);
564 $entry_content = sanitize_rss($entry_content);
566 db_query($link, "BEGIN");
568 if (db_num_rows($result) == 0) {
570 // base post entry does not exist, create it
572 $result = db_query($link,
573 "INSERT INTO ttrss_entries
589 '$entry_timestamp_fmt',
598 // we keep encountering the entry in feeds, so we need to
599 // update date_entered column so that we don't get horrible
600 // dupes when the entry gets purged and reinserted again e.g.
601 // in the case of SLOW SLOW OMG SLOW updating feeds
603 $base_entry_id = db_fetch_result($result, 0, "id");
605 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
606 WHERE id = '$base_entry_id'");
609 // now it should exist, if not - bad luck then
611 $result = db_query($link, "SELECT
612 id,content_hash,no_orig_date,title,
613 substring(date_entered,1,19) as date_entered,
614 substring(updated,1,19) as updated,
618 WHERE guid = '$entry_guid'");
620 if (db_num_rows($result) == 1) {
622 // this will be used below in update handler
623 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
624 $orig_title = db_fetch_result($result, 0, "title");
625 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
626 $orig_date_entered = strtotime(db_fetch_result($result,
629 $ref_id = db_fetch_result($result, 0, "id");
631 // check for user post link to main table
633 // do we allow duplicate posts with same GUID in different feeds?
634 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
635 $dupcheck_qpart = "AND feed_id = '$feed'";
637 $dupcheck_qpart = "";
640 // error_reporting(0);
642 $article_filters = get_article_filters($filters, $entry_title,
643 $entry_content, $entry_link);
645 if (find_article_filter($article_filters, "filter")) {
649 // error_reporting (DEFAULT_ERROR_LEVEL);
651 $result = db_query($link,
652 "SELECT ref_id FROM ttrss_user_entries WHERE
653 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
656 // okay it doesn't exist - create user entry
657 if (db_num_rows($result) == 0) {
659 if (!find_article_filter($article_filters, 'catchup')) {
661 $last_read_qpart = 'NULL';
664 $last_read_qpart = 'NOW()';
667 if (find_article_filter($article_filters, 'mark')) {
673 $result = db_query($link,
674 "INSERT INTO ttrss_user_entries
675 (ref_id, owner_uid, feed_id, unread, last_read, marked)
676 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
677 $last_read_qpart, $marked)");
680 $post_needs_update = false;
682 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
683 ($content_hash != $orig_content_hash)) {
684 $post_needs_update = true;
687 if ($orig_title != $entry_title) {
688 $post_needs_update = true;
691 if ($orig_num_comments != $num_comments) {
692 $post_needs_update = true;
695 // this doesn't seem to be very reliable
697 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
698 // $post_needs_update = true;
701 // if post needs update, update it and mark all user entries
702 // linking to this post as updated
703 if ($post_needs_update) {
705 // print "<!-- post $orig_title needs update : $post_needs_update -->";
707 db_query($link, "UPDATE ttrss_entries
708 SET title = '$entry_title', content = '$entry_content',
709 num_comments = '$num_comments'
710 WHERE id = '$ref_id'");
712 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
713 db_query($link, "UPDATE ttrss_user_entries
714 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
716 db_query($link, "UPDATE ttrss_user_entries
717 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
723 db_query($link, "COMMIT");
726 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
730 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
731 $entry_content_unescaped, $entry_tags);
733 // print "<br>$entry_title : $entry_content_unescaped<br>";
734 // print_r($entry_tags);
737 $entry_tags = $entry_tags[1];
739 # check for manual tags
741 $tag_filter = find_article_filter($article_filters, "tag");
745 $manual_tags = trim_array(split(",", $tag_filter[1]));
747 foreach ($manual_tags as $tag) {
748 if (tag_is_valid($tag)) {
749 array_push($entry_tags, $tag);
754 /* if ($subject_tags) {
755 foreach ($subject_tags as $tag) {
756 if (tag_is_valid($tag)) {
757 array_push($entry_tags, $tag);
762 if (count($entry_tags) > 0) {
764 db_query($link, "BEGIN");
766 $result = db_query($link, "SELECT id,int_id
767 FROM ttrss_entries,ttrss_user_entries
768 WHERE guid = '$entry_guid'
769 AND feed_id = '$feed' AND ref_id = id
770 AND owner_uid = '$owner_uid'");
772 if (db_num_rows($result) == 1) {
774 $entry_id = db_fetch_result($result, 0, "id");
775 $entry_int_id = db_fetch_result($result, 0, "int_id");
777 foreach ($entry_tags as $tag) {
778 $tag = db_escape_string(mb_strtolower(strip_tags($tag)));
780 $tag = str_replace("+", " ", $tag);
781 $tag = str_replace("technorati tag: ", "", $tag);
783 if (!tag_is_valid($tag)) continue;
785 $result = db_query($link, "SELECT id FROM ttrss_tags
786 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
787 owner_uid = '$owner_uid' LIMIT 1");
789 // print db_fetch_result($result, 0, "id");
791 if ($result && db_num_rows($result) == 0) {
793 // print "tagging $entry_id as $tag<br>";
795 db_query($link, "INSERT INTO ttrss_tags
796 (owner_uid,tag_name,post_int_id)
797 VALUES ('$owner_uid','$tag', '$entry_int_id')");
801 db_query($link, "COMMIT");
805 db_query($link, "UPDATE ttrss_feeds
806 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
808 // db_query($link, "COMMIT");
811 $error_msg = db_escape_string(magpie_error());
813 "UPDATE ttrss_feeds SET last_error = '$error_msg',
814 last_updated = NOW() WHERE id = '$feed'");
819 function print_select($id, $default, $values, $attributes = "") {
820 print "<select name=\"$id\" id=\"$id\" $attributes>";
821 foreach ($values as $v) {
827 print "<option$sel>$v</option>";
832 function print_select_hash($id, $default, $values, $attributes = "") {
833 print "<select name=\"$id\" id='$id' $attributes>";
834 foreach (array_keys($values) as $v) {
840 print "<option $sel value=\"$v\">".$values[$v]."</option>";
846 function get_article_filters($filters, $title, $content, $link) {
849 if ($filters["title"]) {
850 foreach ($filters["title"] as $filter) {
851 $reg_exp = $filter["reg_exp"];
852 $inverse = $filter["inverse"];
853 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
854 ($inverse && !preg_match("/$reg_exp/i", $title))) {
856 array_push($matches, array($filter["action"], $filter["action_param"]));
861 if ($filters["content"]) {
862 foreach ($filters["content"] as $filter) {
863 $reg_exp = $filter["reg_exp"];
864 $inverse = $filter["inverse"];
866 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
867 ($inverse && !preg_match("/$reg_exp/i", $content))) {
869 array_push($matches, array($filter["action"], $filter["action_param"]));
874 if ($filters["both"]) {
875 foreach ($filters["both"] as $filter) {
876 $reg_exp = $filter["reg_exp"];
877 $inverse = $filter["inverse"];
880 if (!preg_match("/$reg_exp/i", $title) ||
!preg_match("/$reg_exp/i", $content)) {
881 array_push($matches, array($filter["action"], $filter["action_param"]));
884 if (preg_match("/$reg_exp/i", $title) ||
preg_match("/$reg_exp/i", $content)) {
885 array_push($matches, array($filter["action"], $filter["action_param"]));
891 if ($filters["link"]) {
892 $reg_exp = $filter["reg_exp"];
893 foreach ($filters["link"] as $filter) {
894 $reg_exp = $filter["reg_exp"];
895 $inverse = $filter["inverse"];
897 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
898 ($inverse && !preg_match("/$reg_exp/i", $link))) {
900 array_push($matches, array($filter["action"], $filter["action_param"]));
908 function find_article_filter($filters, $filter_name) {
909 foreach ($filters as $f) {
910 if ($f[0] == $filter_name) {
917 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
918 $rtl_content = false, $last_updated = false, $last_error = false) {
920 if (file_exists($icon_file) && filesize($icon_file) > 0) {
921 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
923 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
927 $rtl_tag = "dir=\"rtl\"";
929 $rtl_tag = "dir=\"ltr\"";
932 $error_notify_msg = "";
935 $link_title = "Error: $last_error ($last_updated)";
936 $error_notify_msg = "(Error)";
937 } else if ($last_updated) {
938 $link_title = "Updated: $last_updated";
941 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
942 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
944 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
945 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
949 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
954 $fctr_class = "class=\"invisible\"";
957 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
958 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
960 if (get_pref($link, "EXTENDED_FEEDLIST")) {
961 print "<div class=\"feedExtInfo\">
962 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
969 function getmicrotime() {
970 list($usec, $sec) = explode(" ",microtime());
971 return ((float)$usec +
(float)$sec);
974 function print_radio($id, $default, $values, $attributes = "") {
975 foreach ($values as $v) {
983 $sel .= " value=\"1\"";
985 $sel .= " value=\"0\"";
988 print "<input class=\"noborder\"
989 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
994 function initialize_user_prefs($link, $uid) {
996 $uid = db_escape_string($uid);
998 db_query($link, "BEGIN");
1000 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1002 $u_result = db_query($link, "SELECT pref_name
1003 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1005 $active_prefs = array();
1007 while ($line = db_fetch_assoc($u_result)) {
1008 array_push($active_prefs, $line["pref_name"]);
1011 while ($line = db_fetch_assoc($result)) {
1012 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1013 // print "adding " . $line["pref_name"] . "<br>";
1015 db_query($link, "INSERT INTO ttrss_user_prefs
1016 (owner_uid,pref_name,value) VALUES
1017 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1022 db_query($link, "COMMIT");
1026 function lookup_user_id($link, $user) {
1028 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1031 if (db_num_rows($result) == 1) {
1032 return db_fetch_result($result, 0, "id");
1038 function http_authenticate_user($link) {
1040 if (!$_SERVER["PHP_AUTH_USER"]) {
1042 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1043 header('HTTP/1.0 401 Unauthorized');
1047 $auth_result = authenticate_user($link,
1048 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1050 if (!$auth_result) {
1051 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1052 header('HTTP/1.0 401 Unauthorized');
1060 function authenticate_user($link, $login, $password, $force_auth = false) {
1062 if (!SINGLE_USER_MODE
) {
1064 $pwd_hash = 'SHA1:' . sha1($password);
1066 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1067 $query = "SELECT id,login,access_level
1068 FROM ttrss_users WHERE
1071 $query = "SELECT id,login,access_level
1072 FROM ttrss_users WHERE
1073 login = '$login' AND pwd_hash = '$pwd_hash'";
1076 $result = db_query($link, $query);
1078 if (db_num_rows($result) == 1) {
1079 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1080 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1081 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1083 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1086 $user_theme = get_user_theme_path($link);
1088 $_SESSION["theme"] = $user_theme;
1089 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1091 initialize_user_prefs($link, $_SESSION["uid"]);
1100 $_SESSION["uid"] = 1;
1101 $_SESSION["name"] = "admin";
1103 $user_theme = get_user_theme_path($link);
1105 $_SESSION["theme"] = $user_theme;
1106 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1108 initialize_user_prefs($link, $_SESSION["uid"]);
1114 function make_password($length = 8) {
1117 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1121 while ($i < $length) {
1122 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1124 if (!strstr($password, $char)) {
1132 // this is called after user is created to initialize default feeds, labels
1135 // user preferences are checked on every login, not here
1137 function initialize_user($link, $uid) {
1139 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1140 values ('$uid','unread = true', 'Unread articles')");
1142 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1143 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1145 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1146 values ('$uid', 'Tiny Tiny RSS: New Releases',
1147 'http://tt-rss.spb.ru/releases.rss')");
1151 function logout_user() {
1153 if (isset($_COOKIE[session_name()])) {
1154 setcookie(session_name(), '', time()-42000, '/');
1158 function get_script_urlpath() {
1159 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1162 function validate_session($link) {
1163 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1164 if ($_SESSION["ip_address"]) {
1165 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1171 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1173 //print_r($_SESSION);
1175 if (time() > $_SESSION["cookie_lifetime"]) {
1183 function login_sequence($link) {
1184 if (!SINGLE_USER_MODE
) {
1186 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1187 $swu = db_escape_string($_REQUEST["swu"]);
1189 $_SESSION["prefs_cache"] = false;
1190 return authenticate_user($link, $swu, null, true);
1194 if ($_COOKIE["ttrss_sid"]) {
1195 require_once "sessions.php";
1198 if (!validate_session($link)) {
1200 render_login_form($link);
1204 $login_action = $_POST["login_action"];
1206 # try to authenticate user if called from login form
1207 if ($login_action == "do_login") {
1208 $login = $_POST["login"];
1209 $password = $_POST["password"];
1210 $remember_me = $_POST["remember_me"];
1213 session_set_cookie_params(SESSION_COOKIE_LIFETIME_REMEMBER
);
1215 session_set_cookie_params(SESSION_COOKIE_LIFETIME
);
1218 require_once "sessions.php";
1220 if (authenticate_user($link, $login, $password)) {
1221 $_POST["password"] = "";
1224 $_SESSION["cookie_lifetime"] = time() +
1225 SESSION_COOKIE_LIFETIME_REMEMBER
;
1226 } else if (SESSION_COOKIE_LIFETIME
) {
1227 $_SESSION["cookie_lifetime"] = time() + SESSION_COOKIE_LIFETIME
;
1230 setcookie("ttrss_cltime", $_SESSION["cookie_lifetime"],
1231 $_SESSION["cookie_lifetime"]);
1233 header("Location: " . $_SERVER["REQUEST_URI"]);
1240 if (!$_SESSION["uid"]) {
1241 render_login_form($link);
1246 require_once "sessions.php";
1247 return authenticate_user($link, "admin", null);
1251 function truncate_string($str, $max_len) {
1252 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1253 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1259 function get_user_theme_path($link) {
1260 $result = db_query($link, "SELECT theme_path
1262 ttrss_themes,ttrss_users
1263 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1264 if (db_num_rows($result) != 0) {
1265 return db_fetch_result($result, 0, "theme_path");
1271 function smart_date_time($timestamp) {
1272 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1273 return date("G:i", $timestamp);
1274 } else if (date("Y", $timestamp) == date("Y")) {
1275 return date("M d, G:i", $timestamp);
1277 return date("Y/m/d G:i", $timestamp);
1281 function smart_date($timestamp) {
1282 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1284 } else if (date("Y", $timestamp) == date("Y")) {
1285 return date("D m", $timestamp);
1287 return date("Y/m/d", $timestamp);
1291 function sql_bool_to_string($s) {
1292 if ($s == "t" ||
$s == "1") {
1299 function sql_bool_to_bool($s) {
1300 if ($s == "t" ||
$s == "1") {
1308 function toggleEvenOdd($a) {
1315 function sanity_check($link) {
1320 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1321 $schema_version = db_fetch_result($result, 0, "schema_version");
1323 if ($schema_version != SCHEMA_VERSION
) {
1327 if (DB_TYPE
== "mysql") {
1328 $result = db_query($link, "SELECT true", false);
1329 if (db_num_rows($result) != 1) {
1334 error_reporting (DEFAULT_ERROR_LEVEL
);
1336 if ($error_code != 0) {
1337 print_error_xml($error_code);
1344 function file_is_locked($filename) {
1346 $fp = fopen($filename, "r");
1347 error_reporting(DEFAULT_ERROR_LEVEL
);
1349 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1350 flock($fp, LOCK_UN
);
1360 function make_lockfile($filename) {
1361 $fp = fopen($filename, "w");
1363 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1370 function sql_random_function() {
1371 if (DB_TYPE
== "mysql") {
1378 function catchup_feed($link, $feed, $cat_view) {
1380 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1385 $cat_qpart = "cat_id = '$feed'";
1387 $cat_qpart = "cat_id IS NULL";
1390 $tmp_result = db_query($link, "SELECT id
1391 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1394 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1396 $tmp_feed = $tmp_line["id"];
1398 db_query($link, "UPDATE ttrss_user_entries
1399 SET unread = false,last_read = NOW()
1400 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1403 } else if ($feed > 0) {
1405 $tmp_result = db_query($link, "SELECT id
1406 FROM ttrss_feeds WHERE parent_feed = '$feed'
1407 ORDER BY cat_id,title");
1409 $parent_ids = array();
1411 if (db_num_rows($tmp_result) > 0) {
1412 while ($p = db_fetch_assoc($tmp_result)) {
1413 array_push($parent_ids, "feed_id = " . $p["id"]);
1416 $children_qpart = implode(" OR ", $parent_ids);
1418 db_query($link, "UPDATE ttrss_user_entries
1419 SET unread = false,last_read = NOW()
1420 WHERE (feed_id = '$feed' OR $children_qpart)
1421 AND owner_uid = " . $_SESSION["uid"]);
1424 db_query($link, "UPDATE ttrss_user_entries
1425 SET unread = false,last_read = NOW()
1426 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1429 } else if ($feed < 0 && $feed > -10) { // special, like starred
1432 db_query($link, "UPDATE ttrss_user_entries
1433 SET unread = false,last_read = NOW()
1434 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1437 } else if ($feed < -10) { // label
1439 // TODO make this more efficient
1441 $label_id = -$feed - 11;
1443 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1444 WHERE id = '$label_id'");
1447 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1449 db_query($link, "BEGIN");
1451 $tmp2_result = db_query($link,
1455 ttrss_user_entries,ttrss_entries,ttrss_feeds
1457 ref_id = ttrss_entries.id AND
1458 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1460 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1462 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1463 db_query($link, "UPDATE
1466 unread = false, last_read = NOW()
1468 int_id = " . $tmp_line["int_id"]);
1471 db_query($link, "COMMIT");
1473 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1474 SET unread = false,last_read = NOW()
1477 AND owner_uid = ".$_SESSION["uid"]); */
1481 db_query($link, "BEGIN");
1483 $tag_name = db_escape_string($feed);
1485 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1486 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1488 while ($line = db_fetch_assoc($result)) {
1489 db_query($link, "UPDATE ttrss_user_entries SET
1490 unread = false, last_read = NOW()
1491 WHERE int_id = " . $line["post_int_id"]);
1493 db_query($link, "COMMIT");
1497 function update_generic_feed($link, $feed, $cat_view) {
1501 $cat_qpart = "cat_id = '$feed'";
1503 $cat_qpart = "cat_id IS NULL";
1506 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1507 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1509 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1510 $feed_url = $tmp_line["feed_url"];
1511 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1515 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1516 WHERE id = '$feed'");
1517 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1518 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1522 function getAllCounters($link, $omode = "tflc") {
1523 /* getLabelCounters($link);
1524 getFeedCounters($link);
1525 getTagCounters($link);
1526 getGlobalCounters($link);
1527 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1528 getCategoryCounters($link);
1531 if (!$omode) $omode = "tflc";
1533 getGlobalCounters($link);
1535 if (strchr($omode, "l")) getLabelCounters($link);
1536 if (strchr($omode, "f")) getFeedCounters($link);
1537 if (strchr($omode, "t")) getTagCounters($link);
1538 if (strchr($omode, "c")) {
1539 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1540 getCategoryCounters($link);
1545 function getCategoryCounters($link) {
1546 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1547 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1548 AND unread = true)) AS unread FROM ttrss_feeds
1550 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1552 while ($line = db_fetch_assoc($result)) {
1553 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1554 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1555 $line["unread"]."\"/>";
1559 function getCategoryUnread($link, $cat) {
1562 $cat_query = "cat_id = '$cat'";
1564 $cat_query = "cat_id IS NULL";
1567 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1569 AND owner_uid = " . $_SESSION["uid"]);
1571 $cat_feeds = array();
1572 while ($line = db_fetch_assoc($result)) {
1573 array_push($cat_feeds, "feed_id = " . $line["id"]);
1576 if (count($cat_feeds) == 0) return 0;
1578 $match_part = implode(" OR ", $cat_feeds);
1580 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1581 FROM ttrss_user_entries
1582 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1586 # this needs to be rewritten
1587 while ($line = db_fetch_assoc($result)) {
1588 $unread +
= $line["unread"];
1595 function getFeedUnread($link, $feed, $is_cat = false) {
1596 $n_feed = sprintf("%d", $feed);
1599 return getCategoryUnread($link, $n_feed);
1600 } else if ($n_feed == -1) {
1601 $match_part = "marked = true";
1602 } else if ($n_feed > 0) {
1604 $result = db_query($link, "SELECT id FROM ttrss_feeds
1605 WHERE parent_feed = '$n_feed'
1607 AND owner_uid = " . $_SESSION["uid"]);
1609 if (db_num_rows($result) > 0) {
1611 $linked_feeds = array();
1612 while ($line = db_fetch_assoc($result)) {
1613 array_push($linked_feeds, "feed_id = " . $line["id"]);
1616 array_push($linked_feeds, "feed_id = $n_feed");
1618 $match_part = implode(" OR ", $linked_feeds);
1620 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1621 FROM ttrss_user_entries
1622 WHERE unread = true AND ($match_part)
1623 AND owner_uid = " . $_SESSION["uid"]);
1627 # this needs to be rewritten
1628 while ($line = db_fetch_assoc($result)) {
1629 $unread +
= $line["unread"];
1635 $match_part = "feed_id = '$n_feed'";
1637 } else if ($feed < -10) {
1639 $label_id = -$feed - 11;
1641 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1642 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1644 $match_part = db_fetch_result($result, 0, "sql_exp");
1649 $result = db_query($link, "SELECT count(int_id) AS unread
1650 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1651 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1652 ttrss_user_entries.ref_id = ttrss_entries.id AND
1653 ttrss_feeds.hidden = false AND
1654 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1658 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1659 FROM ttrss_tags,ttrss_user_entries
1660 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1661 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1664 $unread = db_fetch_result($result, 0, "unread");
1669 /* FIXME this needs reworking */
1671 function getGlobalUnread($link, $user_id = false) {
1674 $user_id = $_SESSION["uid"];
1677 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1678 WHERE unread = true AND
1679 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1680 ttrss_user_entries.ref_id = ttrss_entries.id AND
1682 ttrss_user_entries.owner_uid = '$user_id'");
1683 $c_id = db_fetch_result($result, 0, "c_id");
1687 function getGlobalCounters($link, $global_unread = -1) {
1688 if ($global_unread == -1) {
1689 $global_unread = getGlobalUnread($link);
1691 print "<counter type=\"global\" id='global-unread'
1692 counter='$global_unread'/>";
1694 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1695 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1697 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1699 print "<counter type=\"global\" id='subscribed-feeds'
1700 counter='$subscribed_feeds'/>";
1704 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1707 if (!$_SESSION["tctr_last_value"]) {
1708 $_SESSION["tctr_last_value"] = array();
1712 $old_counters = $_SESSION["tctr_last_value"];
1714 $tctrs_modified = false;
1716 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1717 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1718 ttrss_user_entries.ref_id = ttrss_entries.id AND
1719 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1720 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1722 select tag_name,0 as count FROM ttrss_tags
1723 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1725 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1726 FROM ttrss_user_entries WHERE int_id = post_int_id
1727 AND unread = true)) AS count FROM ttrss_tags
1728 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
1732 while ($line = db_fetch_assoc($result)) {
1733 $tags[$line["tag_name"]] +
= $line["count"];
1736 foreach (array_keys($tags) as $tag) {
1737 $unread = $tags[$tag];
1739 $tag = htmlspecialchars($tag);
1741 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1742 $old_counters[$tag] = $unread;
1743 $tctrs_modified = true;
1744 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1749 if ($smart_mode && $tctrs_modified) {
1750 $_SESSION["tctr_last_value"] = $old_counters;
1755 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1758 if (!$_SESSION["lctr_last_value"]) {
1759 $_SESSION["lctr_last_value"] = array();
1765 $old_counters = $_SESSION["lctr_last_value"];
1766 $lctrs_modified = false;
1768 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1769 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1770 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1771 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1773 $count = db_fetch_result($result, 0, "count");
1776 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1778 $ret_arr["-1"]["counter"] = $count;
1779 $ret_arr["-1"]["description"] = "Starred";
1782 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1783 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1785 while ($line = db_fetch_assoc($result)) {
1787 $id = -$line["id"] - 11;
1789 $label_name = $line["description"];
1791 error_reporting (0);
1793 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1794 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1795 ttrss_feeds.hidden = false AND
1796 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1797 ttrss_user_entries.ref_id = ttrss_entries.id AND
1798 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1800 $count = db_fetch_result($tmp_result, 0, "count");
1802 if (!$smart_mode ||
$old_counters[$id] != $count) {
1803 $old_counters[$id] = $count;
1804 $lctrs_modified = true;
1806 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1808 $ret_arr[$id]["counter"] = $count;
1809 $ret_arr[$id]["description"] = $label_name;
1813 error_reporting (DEFAULT_ERROR_LEVEL
);
1816 if ($smart_mode && $lctrs_modified) {
1817 $_SESSION["lctr_last_value"] = $old_counters;
1823 /* function getFeedCounter($link, $id) {
1825 $result = db_query($link, "SELECT
1826 count(id) as count,last_error
1827 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1828 WHERE feed_id = '$id' AND unread = true
1829 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1830 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1832 $count = db_fetch_result($result, 0, "count");
1833 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1835 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1838 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1841 if (!$_SESSION["fctr_last_value"]) {
1842 $_SESSION["fctr_last_value"] = array();
1846 $old_counters = $_SESSION["fctr_last_value"];
1848 $result = db_query($link, "SELECT id,last_error,parent_feed,
1849 SUBSTRING(last_updated,1,19) AS last_updated,
1851 FROM ttrss_entries,ttrss_user_entries
1852 WHERE feed_id = ttrss_feeds.id AND
1853 ttrss_user_entries.ref_id = ttrss_entries.id
1854 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1855 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1856 AND parent_feed IS NULL");
1858 $fctrs_modified = false;
1860 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1862 while ($line = db_fetch_assoc($result)) {
1865 $count = $line["count"];
1866 $last_error = htmlspecialchars($line["last_error"]);
1868 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1869 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1871 $last_updated = date($short_date, strtotime($line["last_updated"]));
1874 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1876 $tmp_result = db_query($link,
1877 "SELECT id,COUNT(unread) AS unread
1878 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1879 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1880 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1882 if (db_num_rows($tmp_result) > 0) {
1883 while ($l = db_fetch_assoc($tmp_result)) {
1884 $count +
= $l["unread"];
1888 if (!$smart_mode ||
$old_counters[$id] != $count) {
1889 $old_counters[$id] = $count;
1890 $fctrs_modified = true;
1893 $error_part = "error=\"$last_error\"";
1899 $has_img_part = "hi=\"$has_img\"";
1904 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1908 if ($smart_mode && $fctrs_modified) {
1909 $_SESSION["fctr_last_value"] = $old_counters;
1913 function get_script_dt_add() {
1914 if (strpos(VERSION
, "99") === false) {
1921 function get_pgsql_version($link) {
1922 $result = db_query($link, "SELECT version() AS version");
1923 $version = split(" ", db_fetch_result($result, 0, "version"));
1927 function print_error_xml($code, $add_msg = "") {
1930 $error_msg = $ERRORS[$code];
1933 $error_msg = "$error_msg; $add_msg";
1936 print "<rpc-reply>";
1937 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1938 print "</rpc-reply>";
1941 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1943 # check for feed:http://url
1944 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
1946 # check for feed://URL
1947 if (strpos($feed_link, "//") === 0) {
1948 $feed_link = "http:$feed_link";
1951 if ($feed_link == "") return;
1953 if ($cat_id == "0" ||
!$cat_id) {
1954 $cat_qpart = "NULL";
1956 $cat_qpart = "'$cat_id'";
1959 $result = db_query($link,
1960 "SELECT id FROM ttrss_feeds
1961 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1963 if (db_num_rows($result) == 0) {
1965 $result = db_query($link,
1966 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1967 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1968 '[Unknown]', $cat_qpart)");
1970 $result = db_query($link,
1971 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1972 AND owner_uid = " . $_SESSION["uid"]);
1974 $feed_id = db_fetch_result($result, 0, "id");
1977 update_rss_feed($link, $feed_link, $feed_id, true);
1986 function print_feed_select($link, $id, $default_id = "",
1987 $attributes = "", $include_all_feeds = true) {
1989 print "<select id=\"$id\" name=\"$id\" $attributes>";
1990 if ($include_all_feeds) {
1991 print "<option value=\"0\">All feeds</option>";
1994 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1995 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1997 if (db_num_rows($result) > 0 && $include_all_feeds) {
1998 print "<option disabled>--------</option>";
2001 while ($line = db_fetch_assoc($result)) {
2002 if ($line["id"] == $default_id) {
2003 $is_selected = "selected";
2007 printf("<option $is_selected value='%d'>%s</option>",
2008 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2014 function print_feed_cat_select($link, $id, $default_id = "",
2015 $attributes = "", $include_all_cats = true) {
2017 print "<select id=\"$id\" name=\"$id\" $attributes>";
2019 if ($include_all_cats) {
2020 print "<option value=\"0\">"._('Uncategorized')."</option>";
2023 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2024 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2026 if (db_num_rows($result) > 0 && $include_all_cats) {
2027 print "<option disabled>--------</option>";
2030 while ($line = db_fetch_assoc($result)) {
2031 if ($line["id"] == $default_id) {
2032 $is_selected = "selected";
2036 printf("<option $is_selected value='%d'>%s</option>",
2037 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2043 function checkbox_to_sql_bool($val) {
2044 return ($val == "on") ?
"true" : "false";
2047 function getFeedCatTitle($link, $id) {
2049 return _("Special");
2050 } else if ($id < -10) {
2052 } else if ($id > 0) {
2053 $result = db_query($link, "SELECT ttrss_feed_categories.title
2054 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2055 cat_id = ttrss_feed_categories.id");
2056 if (db_num_rows($result) == 1) {
2057 return db_fetch_result($result, 0, "title");
2059 return _("Uncategorized");
2062 return "getFeedCatTitle($id) failed";
2067 function getFeedTitle($link, $id) {
2069 return _("Starred articles");
2070 } else if ($id < -10) {
2071 $label_id = -10 - $id;
2072 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2073 if (db_num_rows($result) == 1) {
2074 return db_fetch_result($result, 0, "description");
2076 return "Unknown label ($label_id)";
2079 } else if ($id > 0) {
2080 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2081 if (db_num_rows($result) == 1) {
2082 return db_fetch_result($result, 0, "title");
2084 return "Unknown feed ($id)";
2087 return "getFeedTitle($id) failed";
2092 function get_session_cookie_name() {
2093 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2096 function print_init_params($link) {
2097 print "<init-params>";
2098 if ($_SESSION["stored-params"]) {
2099 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2101 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2102 print "<param key=\"$key\" value=\"$value\"/>";
2107 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2108 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2109 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2111 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2112 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2114 print "<param key=\"hide_read_feeds\" value=\"" .
2115 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2117 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2118 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2120 print "<param key=\"confirm_feed_catchup\" value=\"" .
2121 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2123 print "</init-params>";
2126 function print_runtime_info($link) {
2127 print "<runtime-info>";
2128 if (ENABLE_UPDATE_DAEMON
) {
2129 print "<param key=\"daemon_is_running\" value=\"".
2130 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2132 if (CHECK_FOR_NEW_VERSION
&& $_SESSION["access_level"] >= 10) {
2134 if ($_SESSION["last_version_check"] +
600 < time()) {
2135 $new_version_details = check_for_update($link);
2137 print "<param key=\"new_version_available\" value=\"".
2138 sprintf("%d", $new_version_details != ""). "\"/>";
2140 $_SESSION["last_version_check"] = time();
2144 print "</runtime-info>";
2147 function getSearchSql($search, $match_on) {
2149 $search_query_part = "";
2151 $keywords = split(" ", $search);
2152 $query_keywords = array();
2154 if ($match_on == "both") {
2156 foreach ($keywords as $k) {
2157 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2158 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2161 $search_query_part = implode("AND", $query_keywords) . " AND ";
2163 } else if ($match_on == "title") {
2165 foreach ($keywords as $k) {
2166 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2169 $search_query_part = implode("AND", $query_keywords) . " AND ";
2171 } else if ($match_on == "content") {
2173 foreach ($keywords as $k) {
2174 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2178 $search_query_part = implode("AND", $query_keywords);
2180 return $search_query_part;
2183 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2187 $search_query_part = getSearchSql($search, $match_on);
2188 $search_query_part .= " AND ";
2191 $search_query_part = "";
2194 $view_query_part = "";
2196 if ($view_mode == "adaptive") {
2198 $view_query_part = " ";
2199 } else if ($feed != -1) {
2200 $unread = getFeedUnread($link, $feed, $cat_view);
2202 $view_query_part = " unread = true AND ";
2207 if ($view_mode == "marked") {
2208 $view_query_part = " marked = true AND ";
2211 if ($view_mode == "unread") {
2212 $view_query_part = " unread = true AND ";
2216 $limit_query_part = "LIMIT " . $limit;
2219 $vfeed_query_part = "";
2221 // override query strategy and enable feed display when searching globally
2222 if ($search && $search_mode == "all_feeds") {
2223 $query_strategy_part = "ttrss_entries.id > 0";
2224 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2225 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2226 $query_strategy_part = "ttrss_entries.id > 0";
2227 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2228 id = feed_id) as feed_title,";
2229 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2231 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2233 $tmp_result = false;
2236 $tmp_result = db_query($link, "SELECT id
2237 FROM ttrss_feeds WHERE cat_id = '$feed'");
2239 $tmp_result = db_query($link, "SELECT id
2240 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2241 WHERE id = '$feed') AND id != '$feed'");
2244 $cat_siblings = array();
2246 if (db_num_rows($tmp_result) > 0) {
2247 while ($p = db_fetch_assoc($tmp_result)) {
2248 array_push($cat_siblings, "feed_id = " . $p["id"]);
2251 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2252 $feed, implode(" OR ", $cat_siblings));
2255 $query_strategy_part = "ttrss_entries.id > 0";
2258 } else if ($feed >= 0) {
2263 $query_strategy_part = "cat_id = '$feed'";
2265 $query_strategy_part = "cat_id IS NULL";
2268 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2271 $tmp_result = db_query($link, "SELECT id
2272 FROM ttrss_feeds WHERE parent_feed = '$feed'
2273 ORDER BY cat_id,title");
2275 $parent_ids = array();
2277 if (db_num_rows($tmp_result) > 0) {
2278 while ($p = db_fetch_assoc($tmp_result)) {
2279 array_push($parent_ids, "feed_id = " . $p["id"]);
2282 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2283 $feed, implode(" OR ", $parent_ids));
2285 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2287 $query_strategy_part = "feed_id = '$feed'";
2290 } else if ($feed == -1) { // starred virtual feed
2291 $query_strategy_part = "marked = true";
2292 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2293 } else if ($feed <= -10) { // labels
2294 $label_id = -$feed - 11;
2296 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2297 WHERE id = '$label_id'");
2299 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2301 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2303 $query_strategy_part = "id > 0"; // dumb
2306 if (get_pref($link, 'REVERSE_HEADLINES')) {
2307 $order_by = "updated";
2309 $order_by = "updated DESC";
2312 if ($override_order) {
2313 $order_by = $override_order;
2318 if ($search && $search_mode == "all_feeds") {
2319 $feed_title = _("Global search results")." ($search)";
2320 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2321 $feed_title = _("Tag search results")." ($search, $feed)";
2322 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2323 $feed_title = $feed;
2324 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2329 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2330 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2331 $feed_title = db_fetch_result($result, 0, "title");
2333 $feed_title = _("Uncategorized");
2337 $feed_title = _("Category search results")." ($search, $feed_title)";
2342 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2343 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2345 $feed_title = db_fetch_result($result, 0, "title");
2346 $feed_site_url = db_fetch_result($result, 0, "site_url");
2347 $last_error = db_fetch_result($result, 0, "last_error");
2350 $feed_title = _("Feed search results") . " ($search, $feed_title)";
2354 } else if ($feed == -1) {
2355 $feed_title = _("Starred articles");
2356 } else if ($feed < -10) {
2357 $label_id = -$feed - 11;
2358 $result = db_query($link, "SELECT description FROM ttrss_labels
2359 WHERE id = '$label_id'");
2360 $feed_title = db_fetch_result($result, 0, "description");
2363 $feed_title = _("Label search results") . " ($search, $feed_title)";
2369 $feed_title = db_unescape_string($feed_title);
2371 if ($feed < -10) error_reporting (0);
2373 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2376 $feed_kind = "Feeds";
2378 $feed_kind = "Labels";
2381 $content_query_part = "content as content_preview,";
2383 if ($limit_query_part) {
2384 $offset_query_part = "OFFSET $offset";
2389 ttrss_entries.id,ttrss_entries.title,
2390 SUBSTRING(updated,1,16) as updated,
2391 unread,feed_id,marked,link,last_read,
2392 SUBSTRING(last_read,1,19) as last_read_noms,
2395 SUBSTRING(updated,1,19) as updated_noms,
2398 ttrss_entries,ttrss_user_entries,ttrss_feeds
2400 ttrss_feeds.hidden = false AND
2401 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2402 ttrss_user_entries.ref_id = ttrss_entries.id AND
2403 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2406 $query_strategy_part ORDER BY $order_by
2407 $limit_query_part $offset_query_part";
2409 $result = db_query($link, $query);
2411 if ($_GET["debug"]) print $query;
2416 $feed_kind = "Tags";
2418 $result = db_query($link, "SELECT
2420 ttrss_entries.id as id,title,
2421 SUBSTRING(updated,1,16) as updated,
2423 marked,link,last_read,
2424 SUBSTRING(last_read,1,19) as last_read_noms,
2427 SUBSTRING(updated,1,19) as updated_noms
2429 ttrss_entries,ttrss_user_entries,ttrss_tags
2431 ref_id = ttrss_entries.id AND
2432 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2433 post_int_id = int_id AND tag_name = '$feed' AND
2436 $query_strategy_part ORDER BY $order_by
2437 $limit_query_part");
2440 return array($result, $feed_title, $feed_site_url, $last_error);
2444 function generate_syndicated_feed($link, $feed, $is_cat,
2445 $search, $search_mode, $match_on) {
2447 $qfh_ret = queryFeedHeadlines($link, $feed,
2448 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2450 $result = $qfh_ret[0];
2451 $feed_title = htmlspecialchars($qfh_ret[1]);
2452 $feed_site_url = $qfh_ret[2];
2453 $last_error = $qfh_ret[3];
2455 print "<rss version=\"2.0\">
2457 <title>$feed_title</title>
2458 <link>$feed_site_url</link>
2459 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2461 while ($line = db_fetch_assoc($result)) {
2463 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2464 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2466 $rfc822_date = date('r', strtotime($line["updated"]));
2468 print "<pubDate>$rfc822_date</pubDate>";
2471 htmlspecialchars($line["title"]) . "</title>";
2473 print "<description>" .
2474 htmlspecialchars($line["content_preview"]) . "</description>";
2479 print "</channel></rss>";
2483 function getCategoryTitle($link, $cat_id) {
2485 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2488 if (db_num_rows($result) == 1) {
2489 return db_fetch_result($result, 0, "title");
2491 return "Uncategorized";
2495 function sanitize_rss($str) {
2498 $res = preg_replace('/<script.*?>/i',
2499 "<p class=\"scriptWarn\">Disabled script: ", $res);
2501 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2503 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2505 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2506 "<p class=\"objectWarn\">(Disabled html object
2507 - flash or other embedded content)</p>", $res); */
2512 function send_headlines_digests($link, $limit = 100) {
2514 if (!DIGEST_ENABLE
) return false;
2516 $user_limit = DIGEST_EMAIL_LIMIT
;
2519 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2521 if (DB_TYPE
== "pgsql") {
2522 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2523 } else if (DB_TYPE
== "mysql") {
2524 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2527 $result = db_query($link, "SELECT id,email FROM ttrss_users
2528 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2530 while ($line = db_fetch_assoc($result)) {
2531 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2532 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2534 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2535 $digest = $tuple[0];
2536 $headlines_count = $tuple[1];
2538 if ($headlines_count > 0) {
2539 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2540 "[tt-rss] New headlines for last 24 hours", $digest,
2541 "From: " . MAIL_FROM
. "\n".
2542 "Content-Type: text/plain; charset=\"utf-8\"\n".
2543 "Content-Transfer-Encoding: 8bit\n");
2545 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2546 WHERE id = " . $line["id"]);
2548 print "No headlines\n";
2553 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2557 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2558 $tmp = _("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
2559 $tmp .= "=======================================================\n\n";
2561 if (DB_TYPE
== "pgsql") {
2562 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2563 } else if (DB_TYPE
== "mysql") {
2564 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2567 $result = db_query($link, "SELECT ttrss_entries.title,
2568 ttrss_feeds.title AS feed_title,
2571 SUBSTRING(last_updated,1,19) AS last_updated
2573 ttrss_user_entries,ttrss_entries,ttrss_feeds
2575 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2576 AND include_in_digest = true
2578 AND ttrss_user_entries.owner_uid = $user_id
2579 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2582 $cur_feed_title = "";
2584 $headlines_count = db_num_rows($result);
2586 while ($line = db_fetch_assoc($result)) {
2587 $updated = smart_date_time(strtotime($line["last_updated"]));
2588 $feed_title = $line["feed_title"];
2590 if ($cur_feed_title != $feed_title) {
2591 $cur_feed_title = $feed_title;
2593 $tmp .= "$feed_title\n\n";
2596 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2597 $tmp .= " " . trim($line["link"]) . "\n";
2602 $tmp .= _("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
2603 DIGEST_HOSTNAME
. "\n".
2604 _("To unsubscribe, visit your configuration options or contact instance owner.\n");
2607 return array($tmp, $headlines_count);
2610 function check_for_update($link, $brief_fmt = true) {
2611 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2613 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2618 $rss = fetch_rss($releases_feed);
2619 error_reporting (DEFAULT_ERROR_LEVEL
);
2623 $items = $rss->items
;
2625 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2626 if (!$items ||
!is_array($items)) $items = $rss;
2628 if (!is_array($items) ||
count($items) == 0) {
2632 $latest_item = $items[0];
2634 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2636 $release_url = sanitize_rss($latest_item["link"]);
2637 $content = sanitize_rss($latest_item["description"]);
2639 if (version_compare(VERSION
, $latest_version) == -1) {
2641 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2642 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2643 <div id=\"milestoneDetails\">$content</div></div>";
2645 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2646 <div class='milestoneDetails'>$content</div>
2647 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2648 download and update information.";
2655 function markArticlesById($link, $ids, $cmode) {
2659 foreach ($ids as $id) {
2660 array_push($tmp_ids, "ref_id = '$id'");
2663 $ids_qpart = join(" OR ", $tmp_ids);
2666 db_query($link, "UPDATE ttrss_user_entries SET
2667 marked = false,last_read = NOW()
2668 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2669 } else if ($cmode == 1) {
2670 db_query($link, "UPDATE ttrss_user_entries SET
2672 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2674 db_query($link, "UPDATE ttrss_user_entries SET
2675 marked = NOT marked,last_read = NOW()
2676 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2680 function catchupArticlesById($link, $ids, $cmode) {
2684 foreach ($ids as $id) {
2685 array_push($tmp_ids, "ref_id = '$id'");
2688 $ids_qpart = join(" OR ", $tmp_ids);
2691 db_query($link, "UPDATE ttrss_user_entries SET
2692 unread = false,last_read = NOW()
2693 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2694 } else if ($cmode == 1) {
2695 db_query($link, "UPDATE ttrss_user_entries SET
2697 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2699 db_query($link, "UPDATE ttrss_user_entries SET
2700 unread = NOT unread,last_read = NOW()
2701 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2705 function escape_for_form($s) {
2706 return htmlspecialchars(db_unescape_string($s));
2709 function make_guid_from_title($title) {
2710 return preg_replace("/[ \"\',.:;]/", "-",
2711 mb_strtolower(strip_tags($title)));
2714 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2715 $bottom = false, $rtl_content = false, $feed_id = 0,
2716 $is_cat = false, $search = false, $match_on = false,
2717 $search_mode = false, $offset = 0, $limit = 0) {
2719 $user_page_offset = $offset +
1;
2722 $class = "headlinesSubToolbar";
2723 $tid = "headlineActionsTop";
2725 $class = "headlinesSubToolbar";
2726 $tid = "headlineActionsBottom";
2729 print "<table class=\"$class\" id=\"$tid\"
2730 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2738 $page_prev_link = "javascript:viewFeedGoPage(-1)";
2739 $page_next_link = "javascript:viewFeedGoPage(1)";
2740 $page_first_link = "javascript:viewFeedGoPage(0)";
2742 $catchup_page_link = "javascript:catchupPage()";
2743 $catchup_feed_link = "javascript:catchupCurrentFeed()";
2745 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2747 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
2748 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
2749 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
2751 $tog_unread_link = "javascript:selectionToggleUnread()";
2752 $tog_marked_link = "javascript:selectionToggleMarked()";
2756 $sel_all_link = "javascript:cdmSelectArticles('all')";
2757 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
2758 $sel_none_link = "javascript:cdmSelectArticles('none')";
2760 $tog_unread_link = "javascript:selectionToggleUnread(true)";
2761 $tog_marked_link = "javascript:selectionToggleMarked(true)";
2765 if (!strstr($_SESSION["client.userAgent"], "MSIE")) {
2767 print "<td class=\"headlineActions$rtl_cpart\">
2768 <ul class=\"headlineDropdownMenu\">
2771 <a href=\"$sel_all_link\">All</a>,
2772 <a href=\"$sel_unread_link\">Unread</a>,
2773 <a href=\"$sel_none_link\">None</a></li>
2774 <li class=\"vsep\"> </li>
2775 <li class=\"top\">Selection<ul>
2776 <li onclick=\"$tog_unread_link\">Toggle unread</li>
2777 <li onclick=\"$tog_marked_link\">Toggle starred</li></ul></li>
2778 <li class=\"vsep\"> </li>
2779 <li class=\"top\"><a href=\"$catchup_page_link\">Mark as read</a><ul>
2780 <li onclick=\"$catchup_page_link\">This page</li>
2781 <li onclick=\"$catchup_feed_link\">Entire feed</li></ul></li>
2782 <li class=\"vsep\"> </li>
2783 <!-- <li class=\"top2\">
2785 <a href=\"$page_prev_link\">Previous</a>,
2786 <a href=\"$page_next_link\">Next</a></li> -->";
2790 <li class=\"top\"><a href=\"$page_next_link\">Next page</a><ul>
2791 <li onclick=\"$page_prev_link\">Previous page</li>
2792 <li onclick=\"$page_first_link\">First page</li></ul></li>
2800 // old style subtoolbar:
2802 print "<td class=\"headlineActions$rtl_cpart\">".
2804 <a href=\"$sel_all_link\">All</a>,
2805 <a href=\"$sel_unread_link\">Unread</a>,
2806 <a href=\"$sel_none_link\">None</a>
2808 _('Toggle:')." <a href=\"$tog_unread_link\">Unread</a>,
2809 <a href=\"$tog_marked_link\">Starred</a>
2811 _('Mark as read:')."
2812 <a href=\"#\" onclick=\"$catchup_page_link\">Page</a>,
2813 <a href=\"#\" onclick=\"$catchup_feed_link\">Feed</a>";
2818 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2819 print "<td class=\"headlineActions$rtl_cpart\">
2820 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2821 '$match_on', '$feed_id', '$is_cat');\">
2822 "._('Convert to Label')."</a></td>";
2825 print "<td class=\"headlineTitle$rtl_cpart\">";
2827 if ($feed_site_url) {
2829 $target = "target=\"_blank\"";
2831 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2837 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2840 if ($user_page_offset > 1) {
2841 print " [$user_page_offset] ";
2847 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2848 <img class=\"noborder\"
2849 alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\">
2854 print "</tr></table>";
2858 function outputFeedList($link, $tags = false) {
2860 print "<ul class=\"feedList\" id=\"feedList\">\n";
2862 $owner_uid = $_SESSION["uid"];
2866 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2867 print "<li class=\"feedCat\">"._('Special')."</li>";
2868 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2871 $num_starred = getFeedUnread($link, -1);
2875 if ($num_starred > 0) $class .= "Unread";
2877 printFeedEntry(-1, $class, _("Starred articles"), $num_starred,
2878 "images/mark_set.png", $link);
2880 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2886 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
2888 $result = db_query($link, "SELECT id,sql_exp,description FROM
2889 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2891 if (db_num_rows($result) > 0) {
2892 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2893 print "<li class=\"feedCat\">"._('Labels')."</li>";
2894 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2896 print "<li><hr></li>";
2900 while ($line = db_fetch_assoc($result)) {
2902 error_reporting (0);
2904 $label_id = -$line['id'] - 11;
2905 $count = getFeedUnread($link, $label_id);
2913 error_reporting (DEFAULT_ERROR_LEVEL
);
2915 printFeedEntry($label_id,
2916 $class, db_unescape_string($line["description"]),
2917 $count, "images/label.png", $link);
2921 if (db_num_rows($result) > 0) {
2922 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2929 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2930 print "<li><hr></li>";
2933 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2934 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2935 $order_by_qpart = "category,unread DESC,title";
2937 $order_by_qpart = "category,title";
2940 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2941 $order_by_qpart = "unread DESC,title";
2943 $order_by_qpart = "title";
2947 $result = db_query($link, "SELECT ttrss_feeds.*,
2948 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2949 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2950 WHERE feed_id = ttrss_feeds.id AND unread = true
2951 AND ttrss_user_entries.ref_id = ttrss_entries.id
2952 AND owner_uid = '$owner_uid') as unread,
2954 ttrss_feed_categories.title AS category,
2955 ttrss_feed_categories.collapsed
2956 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2957 ON (ttrss_feed_categories.id = cat_id)
2959 ttrss_feeds.hidden = false AND
2960 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2961 ORDER BY $order_by_qpart");
2963 $actid = $_GET["actid"];
2973 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2975 while ($line = db_fetch_assoc($result)) {
2977 $feed = db_unescape_string($line["title"]);
2978 $feed_id = $line["id"];
2980 $subop = $_GET["subop"];
2982 $unread = $line["unread"];
2984 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2985 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2987 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2990 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2993 $rtl_tag = "dir=\"RTL\"";
2998 $tmp_result = db_query($link,
2999 "SELECT id,COUNT(unread) AS unread
3000 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3001 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3002 WHERE parent_feed = '$feed_id' AND unread = true
3003 GROUP BY ttrss_feeds.id");
3005 if (db_num_rows($tmp_result) > 0) {
3006 while ($l = db_fetch_assoc($tmp_result)) {
3007 $unread +
= $l["unread"];
3011 $cat_id = $line["cat_id"];
3013 $tmp_category = $line["category"];
3015 if (!$tmp_category) {
3016 $tmp_category = _("Uncategorized");
3019 // $class = ($lnum % 2) ? "even" : "odd";
3021 if ($line["last_error"]) {
3027 if ($unread > 0) $class .= "Unread";
3029 if ($actid == $feed_id) {
3030 $class .= "Selected";
3033 $total_unread +
= $unread;
3035 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3041 $category = $tmp_category;
3043 $collapsed = $line["collapsed"];
3045 // workaround for NULL category
3046 if ($category == _("Uncategorized")) {
3047 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3052 if ($collapsed == "t" ||
$collapsed == "1") {
3053 $holder_class = "invisible";
3060 $cat_id = sprintf("%d", $cat_id);
3062 $cat_unread = getCategoryUnread($link, $cat_id);
3064 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
3066 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3067 <a id=\"FCATN-$cat_id\" href=\"#\"onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3068 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3069 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3070 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3073 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3074 // -> keyboard navigation, etc.
3075 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3078 printFeedEntry($feed_id, $class, $feed, $unread,
3079 "icons/$feed_id.ico", $link, $rtl_content,
3080 $last_updated, $line["last_error"]);
3085 if (db_num_rows($result) == 0) {
3086 print "<li>"._('No feeds to display.')."</li>";
3093 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3094 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3095 post_int_id = ttrss_user_entries.int_id AND
3096 unread = true AND ref_id = ttrss_entries.id
3097 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3099 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3100 ORDER BY tag_name"); */
3102 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3103 print "<li class=\"feedCat\">"._('Tags')."</li>";
3104 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3107 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3108 FROM ttrss_user_entries WHERE int_id = post_int_id
3109 AND unread = true)) AS count FROM ttrss_tags
3110 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
3114 while ($line = db_fetch_assoc($result)) {
3115 $tags[$line["tag_name"]] +
= $line["count"];
3118 foreach (array_keys($tags) as $tag) {
3120 $unread = $tags[$tag];
3128 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3132 if (db_num_rows($result) == 0) {
3133 print "<li>No tags to display.</li>";
3136 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3146 function get_article_tags($link, $id) {
3148 $a_id = db_escape_string($id);
3150 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3151 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3152 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
3156 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3157 array_push($tags, $tmp_line["tag_name"]);
3163 function trim_value(&$value) {
3164 $value = trim($value);
3167 function trim_array($array) {
3169 array_walk($tmp, 'trim_value');
3173 function tag_is_valid($tag) {
3174 if ($tag == '') return false;
3175 if (preg_match("/^[0-9]*$/", $tag)) return false;
3177 $tag = iconv("utf-8", "utf-8", $tag);
3178 if (!$tag) return false;
3183 function render_login_form($link) {
3184 require_once "login_form.php";