]>
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 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION
. ')');
17 require_once "magpierss/rss_fetch.inc";
18 require_once 'magpierss/rss_utils.inc';
19 } else if (RSS_BACKEND_TYPE
== "simplepie") {
20 require_once 'simplepie/simplepie.inc';
23 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
25 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
29 if (DB_TYPE
== "pgsql") {
30 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
31 marked = false AND feed_id = '$feed_id' AND
32 (SELECT date_entered FROM ttrss_entries WHERE
33 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
35 $pg_version = get_pgsql_version($link);
37 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
39 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
40 ttrss_entries.id = ref_id AND
42 feed_id = '$feed_id' AND
43 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
47 $result = db_query($link, "DELETE FROM ttrss_user_entries
49 WHERE ttrss_entries.id = ref_id AND
51 feed_id = '$feed_id' AND
52 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
55 $rows = pg_affected_rows($result);
59 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
60 marked = false AND feed_id = '$feed_id' AND
61 (SELECT date_entered FROM ttrss_entries WHERE
62 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
64 $result = db_query($link, "DELETE FROM ttrss_user_entries
65 USING ttrss_user_entries, ttrss_entries
66 WHERE ttrss_entries.id = ref_id AND
68 feed_id = '$feed_id' AND
69 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
71 $rows = mysql_affected_rows($link);
76 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
80 function global_purge_old_posts($link, $do_output = false, $limit = false) {
82 $random_qpart = sql_random_function();
85 $limit_qpart = "LIMIT $limit";
90 $result = db_query($link,
91 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
92 ORDER BY $random_qpart $limit_qpart");
94 while ($line = db_fetch_assoc($result)) {
96 $feed_id = $line["id"];
97 $purge_interval = $line["purge_interval"];
98 $owner_uid = $line["owner_uid"];
100 if ($purge_interval == 0) {
102 $tmp_result = db_query($link,
103 "SELECT value FROM ttrss_user_prefs WHERE
104 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
106 if (db_num_rows($tmp_result) != 0) {
107 $purge_interval = db_fetch_result($tmp_result, 0, "value");
112 // print "Feed $feed_id: purge interval = $purge_interval\n";
115 if ($purge_interval > 0) {
116 purge_feed($link, $feed_id, $purge_interval, $do_output);
120 // purge orphaned posts in main content table
121 db_query($link, "DELETE FROM ttrss_entries WHERE
122 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
126 function purge_old_posts($link) {
128 $user_id = $_SESSION["uid"];
130 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
131 WHERE owner_uid = '$user_id'");
133 while ($line = db_fetch_assoc($result)) {
135 $feed_id = $line["id"];
136 $purge_interval = $line["purge_interval"];
138 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
140 if ($purge_interval > 0) {
141 purge_feed($link, $feed_id, $purge_interval);
145 // purge orphaned posts in main content table
146 db_query($link, "DELETE FROM ttrss_entries WHERE
147 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
150 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
152 if (WEB_DEMO_MODE
) return;
155 $user_id = $_SESSION["uid"];
156 purge_old_posts($link);
159 // db_query($link, "BEGIN");
161 if (MAX_UPDATE_TIME
> 0) {
162 if (DB_TYPE
== "mysql") {
165 $q_order = "RANDOM()";
168 $q_order = "last_updated DESC";
171 $result = db_query($link, "SELECT feed_url,id,
172 SUBSTRING(last_updated,1,19) AS last_updated,
173 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
178 while ($line = db_fetch_assoc($result)) {
179 $upd_intl = $line["update_interval"];
181 if (!$upd_intl ||
$upd_intl == 0) {
182 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
186 // Updates for this feed are disabled
190 if ($fetch ||
(!$line["last_updated"] ||
191 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
193 // print "<!-- feed: ".$line["feed_url"]." -->";
195 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
197 $upd_elapsed = time() - $upd_start;
199 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
205 // db_query($link, "COMMIT");
209 function fetch_file_contents($url) {
210 if (USE_CURL_FOR_ICONS
) {
211 $tmpfile = tempnam(TMP_DIRECTORY
, "ttrss-tmp");
213 $ch = curl_init($url);
214 $fp = fopen($tmpfile, "w");
217 curl_setopt($ch, CURLOPT_FILE
, $fp);
223 $contents = file_get_contents($tmpfile);
229 return file_get_contents($url);
234 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
235 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
237 function get_favicon_url($url) {
239 if ($html = @fetch_file_contents
($url)) {
241 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
242 // Attempt to grab a favicon link from their webpage url
243 $linkUrl = html_entity_decode($matches[1]);
245 if (substr($linkUrl, 0, 1) == '/') {
246 $urlParts = parse_url($url);
247 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
248 } else if (substr($linkUrl, 0, 7) == 'http://') {
249 $faviconURL = $linkUrl;
250 } else if (substr($url, -1, 1) == '/') {
251 $faviconURL = $url.$linkUrl;
253 $faviconURL = $url.'/'.$linkUrl;
257 // If unsuccessful, attempt to "guess" the favicon location
258 $urlParts = parse_url($url);
259 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
263 // Run a test to see if what we have attempted to get actually exists.
264 if(USE_CURL_FOR_ICONS ||
url_validate($faviconURL)) {
271 function url_validate($link) {
273 $url_parts = @parse_url
($link);
275 if ( empty( $url_parts["host"] ) )
278 if ( !empty( $url_parts["path"] ) ) {
279 $documentpath = $url_parts["path"];
284 if ( !empty( $url_parts["query"] ) )
285 $documentpath .= "?" . $url_parts["query"];
287 $host = $url_parts["host"];
288 $port = $url_parts["port"];
293 $socket = @fsockopen
( $host, $port, $errno, $errstr, 30 );
298 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
300 $http_response = fgets( $socket, 22 );
302 $responses = "/(200 OK)|(30[0-9] Moved)/";
303 if ( preg_match($responses, $http_response) ) {
312 function check_feed_favicon($site_url, $feed, $link) {
313 $favicon_url = get_favicon_url($site_url);
315 # print "FAVICON [$site_url]: $favicon_url\n";
319 $icon_file = ICONS_DIR
. "/$feed.ico";
321 if ($favicon_url && !file_exists($icon_file)) {
322 $contents = fetch_file_contents($favicon_url);
324 $fp = fopen($icon_file, "w");
327 fwrite($fp, $contents);
329 chmod($icon_file, 0644);
333 error_reporting(DEFAULT_ERROR_LEVEL
);
337 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
339 if (WEB_DEMO_MODE
) return;
341 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
345 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
346 FROM ttrss_feeds WHERE id = '$feed'");
348 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
349 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
351 $update_interval = db_fetch_result($result, 0, "update_interval");
353 if ($update_interval < 0) { return; }
355 $feed = db_escape_string($feed);
357 $fetch_url = $feed_url;
359 if ($auth_login && $auth_pass) {
360 $url_parts = array();
361 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
363 if ($url_parts[1] && $url_parts[2]) {
364 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
369 if (RSS_BACKEND_TYPE
== "magpie") {
371 $rss = fetch_rss($fetch_url);
372 error_reporting (DEFAULT_ERROR_LEVEL
);
373 } else if (RSS_BACKEND_TYPE
== "simplepie") {
375 if (!file_exists(SIMPLEPIE_CACHE_DIR
)) {
376 mkdir(SIMPLEPIE_CACHE_DIR
);
379 $rss = new SimplePie();
380 $rss->feed_url($fetch_url);
381 $rss->cache_location(SIMPLEPIE_CACHE_DIR
);
385 $feed = db_escape_string($feed);
389 if (RSS_BACKEND_TYPE
== "simplepie") {
390 $rss_check = $rss->data
;
395 // db_query($link, "BEGIN");
397 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
398 FROM ttrss_feeds WHERE id = '$feed'");
400 $registered_title = db_fetch_result($result, 0, "title");
401 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
402 $orig_site_url = db_fetch_result($result, 0, "site_url");
404 $owner_uid = db_fetch_result($result, 0, "owner_uid");
406 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
407 check_feed_favicon($rss->channel
["link"], $feed, $link);
410 if (!$registered_title ||
$registered_title == "[Unknown]") {
412 if (RSS_BACKEND_TYPE
== "magpie") {
413 $feed_title = db_escape_string($rss->channel
["title"]);
415 $feed_title = $rss->get_feed_title();
418 db_query($link, "UPDATE ttrss_feeds SET
419 title = '$feed_title' WHERE id = '$feed'");
422 if (RSS_BACKEND_TYPE
== "magpie") {
423 $site_url = $rss->channel
["link"];
424 // weird, weird Magpie
425 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
427 $site_url = $rss->get_feed_link();
430 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
431 db_query($link, "UPDATE ttrss_feeds SET
432 site_url = '$site_url' WHERE id = '$feed'");
435 // print "I: " . $rss->channel["image"]["url"];
437 if (RSS_BACKEND_TYPE
== "magpie") {
438 $icon_url = $rss->image
["url"];
440 $icon_url = $rss->get_image_url(); # FIXME
443 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
444 $icon_url = db_escape_string($icon_url);
445 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
451 $result = db_query($link, "SELECT reg_exp,
452 ttrss_filter_types.name AS name,
453 ttrss_filter_actions.name AS action
454 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
456 owner_uid = $owner_uid AND
457 ttrss_filter_types.id = filter_type AND
458 ttrss_filter_actions.id = action_id AND
459 (feed_id IS NULL OR feed_id = '$feed')");
461 while ($line = db_fetch_assoc($result)) {
462 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
464 $filter["reg_exp"] = $line["reg_exp"];
465 $filter["action"] = $line["action"];
467 array_push($filters[$line["name"]], $filter);
470 if (RSS_BACKEND_TYPE
== "magpie") {
471 $iterator = $rss->items
;
473 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
474 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
477 $iterator = $rss->get_items();
480 if (!is_array($iterator)) {
481 /* db_query($link, "UPDATE ttrss_feeds
482 SET last_error = 'Parse error: can\'t find any articles.'
483 WHERE id = '$feed'"); */
487 foreach ($iterator as $item) {
489 if (RSS_BACKEND_TYPE
== "magpie") {
491 $entry_guid = $item["id"];
493 if (!$entry_guid) $entry_guid = $item["guid"];
494 if (!$entry_guid) $entry_guid = $item["link"];
495 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
497 if (!$entry_guid) continue;
499 $entry_timestamp = "";
501 $rss_2_date = $item['pubdate'];
502 $rss_1_date = $item['dc']['date'];
503 $atom_date = $item['issued'];
504 if (!$atom_date) $atom_date = $item['updated'];
506 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
507 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
508 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
510 if ($entry_timestamp == "") {
511 $entry_timestamp = time();
512 $no_orig_date = 'true';
514 $no_orig_date = 'false';
517 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
519 $entry_title = trim(strip_tags($item["title"]));
521 // strange Magpie workaround
522 $entry_link = $item["link_"];
523 if (!$entry_link) $entry_link = $item["link"];
525 if (!$entry_title) continue;
526 # if (!$entry_link) continue;
528 $entry_link = strip_tags($entry_link);
530 $entry_content = $item["content:escaped"];
532 if (!$entry_content) $entry_content = $item["content:encoded"];
533 if (!$entry_content) $entry_content = $item["content"];
534 if (!$entry_content) $entry_content = $item["atom_content"];
535 if (!$entry_content) $entry_content = $item["summary"];
536 if (!$entry_content) $entry_content = $item["description"];
538 // if (!$entry_content) continue;
541 if (is_array($entry_content)) {
542 $entry_content = $entry_content["encoded"];
543 if (!$entry_content) $entry_content = $entry_content["escaped"];
547 // print_r(htmlspecialchars($entry_content));
550 $entry_content_unescaped = $entry_content;
551 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
553 $entry_comments = strip_tags($item["comments"]);
555 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
557 if (!$entry_author) {
558 $entry_author = db_escape_string(strip_tags($item['author']));
561 $entry_guid = db_escape_string(strip_tags($entry_guid));
563 $result = db_query($link, "SELECT id FROM ttrss_entries
564 WHERE guid = '$entry_guid'");
566 $entry_content = db_escape_string($entry_content);
567 $entry_title = db_escape_string($entry_title);
568 $entry_link = db_escape_string($entry_link);
569 $entry_comments = db_escape_string($entry_comments);
571 $num_comments = db_escape_string($item["slash"]["comments"]);
573 if (!$num_comments) $num_comments = 0;
578 $entry_content = sanitize_rss($entry_content);
580 db_query($link, "BEGIN");
582 if (db_num_rows($result) == 0) {
584 // base post entry does not exist, create it
586 $result = db_query($link,
587 "INSERT INTO ttrss_entries
603 '$entry_timestamp_fmt',
612 // we keep encountering the entry in feeds, so we need to
613 // update date_entered column so that we don't get horrible
614 // dupes when the entry gets purged and reinserted again e.g.
615 // in the case of SLOW SLOW OMG SLOW updating feeds
617 $base_entry_id = db_fetch_result($result, 0, "id");
619 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
620 WHERE id = '$base_entry_id'");
623 // now it should exist, if not - bad luck then
625 $result = db_query($link, "SELECT
626 id,content_hash,no_orig_date,title,
627 substring(date_entered,1,19) as date_entered,
628 substring(updated,1,19) as updated,
632 WHERE guid = '$entry_guid'");
634 if (db_num_rows($result) == 1) {
636 // this will be used below in update handler
637 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
638 $orig_title = db_fetch_result($result, 0, "title");
639 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
640 $orig_date_entered = strtotime(db_fetch_result($result,
643 $ref_id = db_fetch_result($result, 0, "id");
645 // check for user post link to main table
647 // do we allow duplicate posts with same GUID in different feeds?
648 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
649 $dupcheck_qpart = "AND feed_id = '$feed'";
651 $dupcheck_qpart = "";
654 // error_reporting(0);
656 $filter_name = get_filter_name($entry_title, $entry_content,
657 $entry_link, $filters);
659 if ($filter_name == "filter") {
663 // error_reporting (DEFAULT_ERROR_LEVEL);
665 $result = db_query($link,
666 "SELECT ref_id FROM ttrss_user_entries WHERE
667 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
670 // okay it doesn't exist - create user entry
671 if (db_num_rows($result) == 0) {
673 if ($filter_name != 'catchup') {
675 $last_read_qpart = 'NULL';
678 $last_read_qpart = 'NOW()';
681 if ($filter_name == 'mark') {
687 $result = db_query($link,
688 "INSERT INTO ttrss_user_entries
689 (ref_id, owner_uid, feed_id, unread, last_read, marked)
690 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
691 $last_read_qpart, $marked)");
694 $post_needs_update = false;
696 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
697 ($content_hash != $orig_content_hash)) {
698 $post_needs_update = true;
701 if ($orig_title != $entry_title) {
702 $post_needs_update = true;
705 if ($orig_num_comments != $num_comments) {
706 $post_needs_update = true;
709 // this doesn't seem to be very reliable
711 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
712 // $post_needs_update = true;
715 // if post needs update, update it and mark all user entries
716 // linking to this post as updated
717 if ($post_needs_update) {
719 // print "<!-- post $orig_title needs update : $post_needs_update -->";
721 db_query($link, "UPDATE ttrss_entries
722 SET title = '$entry_title', content = '$entry_content',
723 num_comments = '$num_comments'
724 WHERE id = '$ref_id'");
726 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
727 db_query($link, "UPDATE ttrss_user_entries
728 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
730 db_query($link, "UPDATE ttrss_user_entries
731 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
737 db_query($link, "COMMIT");
740 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
744 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
745 $entry_content_unescaped, $entry_tags);
747 // print "<br>$entry_title : $entry_content_unescaped<br>";
748 // print_r($entry_tags);
751 $entry_tags = $entry_tags[1];
753 if (count($entry_tags) > 0) {
755 db_query($link, "BEGIN");
757 $result = db_query($link, "SELECT id,int_id
758 FROM ttrss_entries,ttrss_user_entries
759 WHERE guid = '$entry_guid'
760 AND feed_id = '$feed' AND ref_id = id
761 AND owner_uid = '$owner_uid'");
763 if (db_num_rows($result) == 1) {
765 $entry_id = db_fetch_result($result, 0, "id");
766 $entry_int_id = db_fetch_result($result, 0, "int_id");
768 foreach ($entry_tags as $tag) {
769 $tag = db_escape_string(strtolower($tag));
771 $tag = str_replace("+", " ", $tag);
772 $tag = str_replace("technorati tag: ", "", $tag);
774 $result = db_query($link, "SELECT id FROM ttrss_tags
775 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
776 owner_uid = '$owner_uid' LIMIT 1");
778 // print db_fetch_result($result, 0, "id");
780 if ($result && db_num_rows($result) == 0) {
782 // print "tagging $entry_id as $tag<br>";
784 db_query($link, "INSERT INTO ttrss_tags
785 (owner_uid,tag_name,post_int_id)
786 VALUES ('$owner_uid','$tag', '$entry_int_id')");
790 db_query($link, "COMMIT");
794 db_query($link, "UPDATE ttrss_feeds
795 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
797 // db_query($link, "COMMIT");
800 $error_msg = db_escape_string(magpie_error());
802 "UPDATE ttrss_feeds SET last_error = '$error_msg',
803 last_updated = NOW() WHERE id = '$feed'");
808 function print_select($id, $default, $values, $attributes = "") {
809 print "<select name=\"$id\" id=\"$id\" $attributes>";
810 foreach ($values as $v) {
816 print "<option$sel>$v</option>";
821 function print_select_hash($id, $default, $values, $attributes = "") {
822 print "<select name=\"$id\" id='$id' $attributes>";
823 foreach (array_keys($values) as $v) {
829 print "<option $sel value=\"$v\">".$values[$v]."</option>";
835 function get_filter_name($title, $content, $link, $filters) {
837 if ($filters["title"]) {
838 foreach ($filters["title"] as $filter) {
839 $reg_exp = $filter["reg_exp"];
840 if (preg_match("/$reg_exp/i", $title)) {
841 return $filter["action"];
846 if ($filters["content"]) {
847 foreach ($filters["content"] as $filter) {
848 $reg_exp = $filter["reg_exp"];
849 if (preg_match("/$reg_exp/i", $content)) {
850 return $filter["action"];
855 if ($filters["both"]) {
856 foreach ($filters["both"] as $filter) {
857 $reg_exp = $filter["reg_exp"];
858 if (preg_match("/$reg_exp/i", $title) ||
859 preg_match("/$reg_exp/i", $content)) {
860 return $filter["action"];
865 if ($filters["link"]) {
866 $reg_exp = $filter["reg_exp"];
867 foreach ($filters["link"] as $filter) {
868 $reg_exp = $filter["reg_exp"];
869 if (preg_match("/$reg_exp/i", $link)) {
870 return $filter["action"];
878 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
879 $rtl_content = false, $last_updated = false, $last_error = false) {
881 if (file_exists($icon_file) && filesize($icon_file) > 0) {
882 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
884 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
888 $rtl_tag = "dir=\"rtl\"";
890 $rtl_tag = "dir=\"ltr\"";
893 $error_notify_msg = "";
896 $link_title = "Error: $last_error ($last_updated)";
897 $error_notify_msg = "(Error)";
898 } else if ($last_updated) {
899 $link_title = "Updated: $last_updated";
902 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
903 href=\"#\" onclick=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
905 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
906 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
910 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
915 $fctr_class = "class=\"invisible\"";
918 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
919 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
921 if (get_pref($link, "EXTENDED_FEEDLIST")) {
922 print "<div class=\"feedExtInfo\">
923 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
930 function getmicrotime() {
931 list($usec, $sec) = explode(" ",microtime());
932 return ((float)$usec +
(float)$sec);
935 function print_radio($id, $default, $values, $attributes = "") {
936 foreach ($values as $v) {
944 $sel .= " value=\"1\"";
946 $sel .= " value=\"0\"";
949 print "<input class=\"noborder\"
950 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
955 function initialize_user_prefs($link, $uid) {
957 $uid = db_escape_string($uid);
959 db_query($link, "BEGIN");
961 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
963 $u_result = db_query($link, "SELECT pref_name
964 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
966 $active_prefs = array();
968 while ($line = db_fetch_assoc($u_result)) {
969 array_push($active_prefs, $line["pref_name"]);
972 while ($line = db_fetch_assoc($result)) {
973 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
974 // print "adding " . $line["pref_name"] . "<br>";
976 db_query($link, "INSERT INTO ttrss_user_prefs
977 (owner_uid,pref_name,value) VALUES
978 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
983 db_query($link, "COMMIT");
987 function lookup_user_id($link, $user) {
989 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
992 if (db_num_rows($result) == 1) {
993 return db_fetch_result($result, 0, "id");
999 function http_authenticate_user($link) {
1001 if (!$_SERVER["PHP_AUTH_USER"]) {
1003 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1004 header('HTTP/1.0 401 Unauthorized');
1008 $auth_result = authenticate_user($link,
1009 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1011 if (!$auth_result) {
1012 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1013 header('HTTP/1.0 401 Unauthorized');
1021 function authenticate_user($link, $login, $password, $force_auth = false) {
1023 if (!SINGLE_USER_MODE
) {
1025 $pwd_hash = 'SHA1:' . sha1($password);
1027 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1028 $query = "SELECT id,login,access_level
1029 FROM ttrss_users WHERE
1032 $query = "SELECT id,login,access_level
1033 FROM ttrss_users WHERE
1034 login = '$login' AND pwd_hash = '$pwd_hash'";
1037 $result = db_query($link, $query);
1039 if (db_num_rows($result) == 1) {
1040 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1041 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1042 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1044 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1047 $user_theme = get_user_theme_path($link);
1049 $_SESSION["theme"] = $user_theme;
1050 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1052 initialize_user_prefs($link, $_SESSION["uid"]);
1061 $_SESSION["uid"] = 1;
1062 $_SESSION["name"] = "admin";
1064 $user_theme = get_user_theme_path($link);
1066 $_SESSION["theme"] = $user_theme;
1067 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1069 initialize_user_prefs($link, $_SESSION["uid"]);
1075 function make_password($length = 8) {
1078 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1082 while ($i < $length) {
1083 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1085 if (!strstr($password, $char)) {
1093 // this is called after user is created to initialize default feeds, labels
1096 // user preferences are checked on every login, not here
1098 function initialize_user($link, $uid) {
1100 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1101 values ('$uid','unread = true', 'Unread articles')");
1103 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1104 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1106 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1107 values ('$uid', 'Tiny Tiny RSS: New Releases',
1108 'http://tt-rss.spb.ru/releases.rss')");
1112 function logout_user() {
1114 if (isset($_COOKIE[session_name()])) {
1115 setcookie(session_name(), '', time()-42000, '/');
1119 function get_script_urlpath() {
1120 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1123 function get_login_redirect() {
1124 $server = $_SERVER["SERVER_NAME"];
1126 if (ENABLE_LOGIN_SSL
) {
1127 $protocol = "https";
1132 $url_path = get_script_urlpath();
1134 $redirect_uri = "$protocol://$server$url_path/login.php";
1136 return $redirect_uri;
1139 function validate_session($link) {
1140 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1141 if ($_SESSION["ip_address"]) {
1142 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1150 function basic_nosid_redirect_check() {
1151 if (!SINGLE_USER_MODE
) {
1152 if (!$_COOKIE[get_session_cookie_name()]) {
1153 $redirect_uri = get_login_redirect();
1154 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1155 header("Location: $redirect_uri?rt=$return_to");
1161 function login_sequence($link) {
1162 if (!SINGLE_USER_MODE
) {
1164 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1165 $swu = db_escape_string($_REQUEST["swu"]);
1167 $_SESSION["prefs_cache"] = false;
1168 return authenticate_user($link, $swu, null, true);
1172 if (!validate_session($link)) {
1174 $redirect_uri = get_login_redirect();
1175 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1176 header("Location: $redirect_uri?rt=$return_to");
1180 if (!USE_HTTP_AUTH
) {
1181 if (!$_SESSION["uid"]) {
1182 $redirect_uri = get_login_redirect();
1183 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1184 header("Location: $redirect_uri?rt=$return_to");
1188 if (!$_SESSION["uid"]) {
1189 if (!$_SERVER["PHP_AUTH_USER"]) {
1191 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1192 header('HTTP/1.0 401 Unauthorized');
1196 $auth_result = authenticate_user($link,
1197 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1199 if (!$auth_result) {
1200 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1201 header('HTTP/1.0 401 Unauthorized');
1208 return authenticate_user($link, "admin", null);
1212 function truncate_string($str, $max_len) {
1213 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1214 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1220 function get_user_theme_path($link) {
1221 $result = db_query($link, "SELECT theme_path
1223 ttrss_themes,ttrss_users
1224 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1225 if (db_num_rows($result) != 0) {
1226 return db_fetch_result($result, 0, "theme_path");
1232 function smart_date_time($timestamp) {
1233 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1234 return date("G:i", $timestamp);
1235 } else if (date("Y", $timestamp) == date("Y")) {
1236 return date("M d, G:i", $timestamp);
1238 return date("Y/m/d G:i", $timestamp);
1242 function smart_date($timestamp) {
1243 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1245 } else if (date("Y", $timestamp) == date("Y")) {
1246 return date("D m", $timestamp);
1248 return date("Y/m/d", $timestamp);
1252 function sql_bool_to_string($s) {
1253 if ($s == "t" ||
$s == "1") {
1260 function sql_bool_to_bool($s) {
1261 if ($s == "t" ||
$s == "1") {
1269 function toggleEvenOdd($a) {
1276 function sanity_check($link) {
1281 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1282 $schema_version = db_fetch_result($result, 0, "schema_version");
1284 if ($schema_version != SCHEMA_VERSION
) {
1288 if (DB_TYPE
== "mysql") {
1289 $result = db_query($link, "SELECT true", false);
1290 if (db_num_rows($result) != 1) {
1295 error_reporting (DEFAULT_ERROR_LEVEL
);
1297 if ($error_code != 0) {
1298 print_error_xml($error_code);
1305 function file_is_locked($filename) {
1307 $fp = fopen($filename, "r");
1308 error_reporting(DEFAULT_ERROR_LEVEL
);
1310 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1311 flock($fp, LOCK_UN
);
1321 function make_lockfile($filename) {
1322 $fp = fopen($filename, "w");
1324 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1331 function sql_random_function() {
1332 if (DB_TYPE
== "mysql") {
1339 function catchup_feed($link, $feed, $cat_view) {
1341 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1346 $cat_qpart = "cat_id = '$feed'";
1348 $cat_qpart = "cat_id IS NULL";
1351 $tmp_result = db_query($link, "SELECT id
1352 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1355 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1357 $tmp_feed = $tmp_line["id"];
1359 db_query($link, "UPDATE ttrss_user_entries
1360 SET unread = false,last_read = NOW()
1361 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1364 } else if ($feed > 0) {
1366 $tmp_result = db_query($link, "SELECT id
1367 FROM ttrss_feeds WHERE parent_feed = '$feed'
1368 ORDER BY cat_id,title");
1370 $parent_ids = array();
1372 if (db_num_rows($tmp_result) > 0) {
1373 while ($p = db_fetch_assoc($tmp_result)) {
1374 array_push($parent_ids, "feed_id = " . $p["id"]);
1377 $children_qpart = implode(" OR ", $parent_ids);
1379 db_query($link, "UPDATE ttrss_user_entries
1380 SET unread = false,last_read = NOW()
1381 WHERE (feed_id = '$feed' OR $children_qpart)
1382 AND owner_uid = " . $_SESSION["uid"]);
1385 db_query($link, "UPDATE ttrss_user_entries
1386 SET unread = false,last_read = NOW()
1387 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1390 } else if ($feed < 0 && $feed > -10) { // special, like starred
1393 db_query($link, "UPDATE ttrss_user_entries
1394 SET unread = false,last_read = NOW()
1395 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1398 } else if ($feed < -10) { // label
1400 // TODO make this more efficient
1402 $label_id = -$feed - 11;
1404 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1405 WHERE id = '$label_id'");
1408 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1410 db_query($link, "BEGIN");
1412 $tmp2_result = db_query($link,
1416 ttrss_user_entries,ttrss_entries,ttrss_feeds
1418 ref_id = ttrss_entries.id AND
1419 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1421 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1423 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1424 db_query($link, "UPDATE
1427 unread = false, last_read = NOW()
1429 int_id = " . $tmp_line["int_id"]);
1432 db_query($link, "COMMIT");
1434 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1435 SET unread = false,last_read = NOW()
1438 AND owner_uid = ".$_SESSION["uid"]); */
1442 db_query($link, "BEGIN");
1444 $tag_name = db_escape_string($feed);
1446 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1447 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1449 while ($line = db_fetch_assoc($result)) {
1450 db_query($link, "UPDATE ttrss_user_entries SET
1451 unread = false, last_read = NOW()
1452 WHERE int_id = " . $line["post_int_id"]);
1454 db_query($link, "COMMIT");
1458 function update_generic_feed($link, $feed, $cat_view) {
1462 $cat_qpart = "cat_id = '$feed'";
1464 $cat_qpart = "cat_id IS NULL";
1467 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1468 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1470 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1471 $feed_url = $tmp_line["feed_url"];
1472 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1476 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1477 WHERE id = '$feed'");
1478 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1479 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1483 function getAllCounters($link) {
1484 getLabelCounters($link);
1485 getFeedCounters($link);
1486 getTagCounters($link);
1487 getGlobalCounters($link);
1488 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1489 getCategoryCounters($link);
1493 function getCategoryCounters($link) {
1494 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1495 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1496 AND unread = true)) AS unread FROM ttrss_feeds
1498 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1500 while ($line = db_fetch_assoc($result)) {
1501 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1502 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1503 $line["unread"]."\"/>";
1507 function getCategoryUnread($link, $cat) {
1510 $cat_query = "cat_id = '$cat'";
1512 $cat_query = "cat_id IS NULL";
1515 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1517 AND owner_uid = " . $_SESSION["uid"]);
1519 $cat_feeds = array();
1520 while ($line = db_fetch_assoc($result)) {
1521 array_push($cat_feeds, "feed_id = " . $line["id"]);
1524 if (count($cat_feeds) == 0) return 0;
1526 $match_part = implode(" OR ", $cat_feeds);
1528 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1529 FROM ttrss_user_entries
1530 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1534 # this needs to be rewritten
1535 while ($line = db_fetch_assoc($result)) {
1536 $unread +
= $line["unread"];
1543 function getFeedUnread($link, $feed, $is_cat = false) {
1544 $n_feed = sprintf("%d", $feed);
1547 return getCategoryUnread($link, $n_feed);
1548 } else if ($n_feed == -1) {
1549 $match_part = "marked = true";
1550 } else if ($n_feed > 0) {
1552 $result = db_query($link, "SELECT id FROM ttrss_feeds
1553 WHERE parent_feed = '$n_feed'
1555 AND owner_uid = " . $_SESSION["uid"]);
1557 if (db_num_rows($result) > 0) {
1559 $linked_feeds = array();
1560 while ($line = db_fetch_assoc($result)) {
1561 array_push($linked_feeds, "feed_id = " . $line["id"]);
1564 array_push($linked_feeds, "feed_id = $n_feed");
1566 $match_part = implode(" OR ", $linked_feeds);
1568 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1569 FROM ttrss_user_entries
1570 WHERE unread = true AND ($match_part)
1571 AND owner_uid = " . $_SESSION["uid"]);
1575 # this needs to be rewritten
1576 while ($line = db_fetch_assoc($result)) {
1577 $unread +
= $line["unread"];
1583 $match_part = "feed_id = '$n_feed'";
1585 } else if ($feed < -10) {
1587 $label_id = -$feed - 11;
1589 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1590 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1592 $match_part = db_fetch_result($result, 0, "sql_exp");
1597 $result = db_query($link, "SELECT count(int_id) AS unread
1598 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1599 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1600 ttrss_user_entries.ref_id = ttrss_entries.id AND
1601 ttrss_feeds.hidden = false AND
1602 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1606 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1607 FROM ttrss_tags,ttrss_user_entries
1608 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1609 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1612 $unread = db_fetch_result($result, 0, "unread");
1617 /* FIXME this needs reworking */
1619 function getGlobalUnread($link, $user_id = false) {
1622 $user_id = $_SESSION["uid"];
1625 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1626 WHERE unread = true AND
1627 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1628 ttrss_user_entries.ref_id = ttrss_entries.id AND
1630 ttrss_user_entries.owner_uid = '$user_id'");
1631 $c_id = db_fetch_result($result, 0, "c_id");
1635 function getGlobalCounters($link, $global_unread = -1) {
1636 if ($global_unread == -1) {
1637 $global_unread = getGlobalUnread($link);
1639 print "<counter type=\"global\" id='global-unread'
1640 counter='$global_unread'/>";
1642 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1643 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1645 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1647 print "<counter type=\"global\" id='subscribed-feeds'
1648 counter='$subscribed_feeds'/>";
1652 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1655 if (!$_SESSION["tctr_last_value"]) {
1656 $_SESSION["tctr_last_value"] = array();
1660 $old_counters = $_SESSION["tctr_last_value"];
1662 $tctrs_modified = false;
1664 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1665 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1666 ttrss_user_entries.ref_id = ttrss_entries.id AND
1667 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1668 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1670 select tag_name,0 as count FROM ttrss_tags
1671 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1673 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1674 FROM ttrss_user_entries WHERE int_id = post_int_id
1675 AND unread = true)) AS count FROM ttrss_tags
1676 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1680 while ($line = db_fetch_assoc($result)) {
1681 $tags[$line["tag_name"]] +
= $line["count"];
1684 foreach (array_keys($tags) as $tag) {
1685 $unread = $tags[$tag];
1687 $tag = htmlspecialchars($tag);
1689 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1690 $old_counters[$tag] = $unread;
1691 $tctrs_modified = true;
1692 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1697 if ($smart_mode && $tctrs_modified) {
1698 $_SESSION["tctr_last_value"] = $old_counters;
1703 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1706 if (!$_SESSION["lctr_last_value"]) {
1707 $_SESSION["lctr_last_value"] = array();
1713 $old_counters = $_SESSION["lctr_last_value"];
1714 $lctrs_modified = false;
1716 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1717 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1718 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1719 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1721 $count = db_fetch_result($result, 0, "count");
1724 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1726 $ret_arr["-1"]["counter"] = $count;
1727 $ret_arr["-1"]["description"] = "Starred";
1730 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1731 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1733 while ($line = db_fetch_assoc($result)) {
1735 $id = -$line["id"] - 11;
1737 $label_name = $line["description"];
1739 error_reporting (0);
1741 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1742 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1743 ttrss_feeds.hidden = false AND
1744 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1745 ttrss_user_entries.ref_id = ttrss_entries.id AND
1746 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1748 $count = db_fetch_result($tmp_result, 0, "count");
1750 if (!$smart_mode ||
$old_counters[$id] != $count) {
1751 $old_counters[$id] = $count;
1752 $lctrs_modified = true;
1754 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1756 $ret_arr[$id]["counter"] = $count;
1757 $ret_arr[$id]["description"] = $label_name;
1761 error_reporting (DEFAULT_ERROR_LEVEL
);
1764 if ($smart_mode && $lctrs_modified) {
1765 $_SESSION["lctr_last_value"] = $old_counters;
1771 /* function getFeedCounter($link, $id) {
1773 $result = db_query($link, "SELECT
1774 count(id) as count,last_error
1775 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1776 WHERE feed_id = '$id' AND unread = true
1777 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1778 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1780 $count = db_fetch_result($result, 0, "count");
1781 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1783 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1786 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1789 if (!$_SESSION["fctr_last_value"]) {
1790 $_SESSION["fctr_last_value"] = array();
1794 $old_counters = $_SESSION["fctr_last_value"];
1796 $result = db_query($link, "SELECT id,last_error,parent_feed,
1797 SUBSTRING(last_updated,1,19) AS last_updated,
1799 FROM ttrss_entries,ttrss_user_entries
1800 WHERE feed_id = ttrss_feeds.id AND
1801 ttrss_user_entries.ref_id = ttrss_entries.id
1802 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1803 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1804 AND parent_feed IS NULL");
1806 $fctrs_modified = false;
1808 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1810 while ($line = db_fetch_assoc($result)) {
1813 $count = $line["count"];
1814 $last_error = htmlspecialchars($line["last_error"]);
1816 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1817 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1819 $last_updated = date($short_date, strtotime($line["last_updated"]));
1822 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1824 $tmp_result = db_query($link,
1825 "SELECT id,COUNT(unread) AS unread
1826 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1827 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1828 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1830 if (db_num_rows($tmp_result) > 0) {
1831 while ($l = db_fetch_assoc($tmp_result)) {
1832 $count +
= $l["unread"];
1836 if (!$smart_mode ||
$old_counters[$id] != $count) {
1837 $old_counters[$id] = $count;
1838 $fctrs_modified = true;
1841 $error_part = "error=\"$last_error\"";
1847 $has_img_part = "hi=\"$has_img\"";
1852 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1856 if ($smart_mode && $fctrs_modified) {
1857 $_SESSION["fctr_last_value"] = $old_counters;
1861 function get_script_dt_add() {
1862 if (strpos(VERSION
, "99") === false) {
1869 function get_pgsql_version($link) {
1870 $result = db_query($link, "SELECT version() AS version");
1871 $version = split(" ", db_fetch_result($result, 0, "version"));
1875 function print_error_xml($code, $add_msg = "") {
1878 $error_msg = $ERRORS[$code];
1881 $error_msg = "$error_msg; $add_msg";
1884 print "<rpc-reply>";
1885 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1886 print "</rpc-reply>";
1889 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1891 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
1893 if ($feed_link == "") return;
1895 if ($cat_id == "0" ||
!$cat_id) {
1896 $cat_qpart = "NULL";
1898 $cat_qpart = "'$cat_id'";
1901 $result = db_query($link,
1902 "SELECT id FROM ttrss_feeds
1903 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1905 if (db_num_rows($result) == 0) {
1907 $result = db_query($link,
1908 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1909 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1910 '[Unknown]', $cat_qpart)");
1912 $result = db_query($link,
1913 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1914 AND owner_uid = " . $_SESSION["uid"]);
1916 $feed_id = db_fetch_result($result, 0, "id");
1919 update_rss_feed($link, $feed_link, $feed_id, true);
1928 function print_feed_select($link, $id, $default_id = "",
1929 $attributes = "", $include_all_feeds = true) {
1931 print "<select id=\"$id\" name=\"$id\" $attributes>";
1932 if ($include_all_feeds) {
1933 print "<option value=\"0\">All feeds</option>";
1936 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1937 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1939 if (db_num_rows($result) > 0 && $include_all_feeds) {
1940 print "<option disabled>--------</option>";
1943 while ($line = db_fetch_assoc($result)) {
1944 if ($line["id"] == $default_id) {
1945 $is_selected = "selected";
1949 printf("<option $is_selected value='%d'>%s</option>",
1950 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1956 function print_feed_cat_select($link, $id, $default_id = "",
1957 $attributes = "", $include_all_cats = true) {
1959 print "<select id=\"$id\" name=\"$id\" $attributes>";
1961 if ($include_all_cats) {
1962 print "<option value=\"0\">Uncategorized</option>";
1965 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1966 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1968 if (db_num_rows($result) > 0 && $include_all_cats) {
1969 print "<option disabled>--------</option>";
1972 while ($line = db_fetch_assoc($result)) {
1973 if ($line["id"] == $default_id) {
1974 $is_selected = "selected";
1978 printf("<option $is_selected value='%d'>%s</option>",
1979 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1985 function checkbox_to_sql_bool($val) {
1986 return ($val == "on") ?
"true" : "false";
1989 function getFeedCatTitle($link, $id) {
1992 } else if ($id < -10) {
1994 } else if ($id > 0) {
1995 $result = db_query($link, "SELECT ttrss_feed_categories.title
1996 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1997 cat_id = ttrss_feed_categories.id");
1998 if (db_num_rows($result) == 1) {
1999 return db_fetch_result($result, 0, "title");
2001 return "Uncategorized";
2004 return "getFeedCatTitle($id) failed";
2009 function getFeedTitle($link, $id) {
2011 return "Starred articles";
2012 } else if ($id < -10) {
2013 $label_id = -10 - $id;
2014 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2015 if (db_num_rows($result) == 1) {
2016 return db_fetch_result($result, 0, "description");
2018 return "Unknown label ($label_id)";
2021 } else if ($id > 0) {
2022 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2023 if (db_num_rows($result) == 1) {
2024 return db_fetch_result($result, 0, "title");
2026 return "Unknown feed ($id)";
2029 return "getFeedTitle($id) failed";
2034 function get_session_cookie_name() {
2035 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
2038 function print_init_params($link) {
2039 print "<init-params>";
2040 if ($_SESSION["stored-params"]) {
2041 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2043 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2044 print "<param key=\"$key\" value=\"$value\"/>";
2049 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
2050 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
2051 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
2053 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2054 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2056 print "<param key=\"hide_read_feeds\" value=\"" .
2057 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2059 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2060 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2062 print "<param key=\"confirm_feed_catchup\" value=\"" .
2063 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2065 print "</init-params>";
2068 function print_runtime_info($link) {
2069 print "<runtime-info>";
2070 if (ENABLE_UPDATE_DAEMON
) {
2071 print "<param key=\"daemon_is_running\" value=\"".
2072 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2074 print "</runtime-info>";
2077 function getSearchSql($search, $match_on) {
2079 $search_query_part = "";
2081 $keywords = split(" ", $search);
2082 $query_keywords = array();
2084 if ($match_on == "both") {
2086 foreach ($keywords as $k) {
2087 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2088 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2091 $search_query_part = implode("AND", $query_keywords) . " AND ";
2093 } else if ($match_on == "title") {
2095 foreach ($keywords as $k) {
2096 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2099 $search_query_part = implode("AND", $query_keywords) . " AND ";
2101 } else if ($match_on == "content") {
2103 foreach ($keywords as $k) {
2104 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2108 $search_query_part = implode("AND", $query_keywords);
2110 return $search_query_part;
2113 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2116 $search_query_part = getSearchSql($search, $match_on);
2117 $search_query_part .= " AND ";
2120 $search_query_part = "";
2123 $view_query_part = "";
2125 if ($view_mode == "adaptive") {
2127 $view_query_part = " ";
2128 } else if ($feed != -1) {
2129 $unread = getFeedUnread($link, $feed, $cat_view);
2131 $view_query_part = " unread = true AND ";
2136 if ($view_mode == "marked") {
2137 $view_query_part = " marked = true AND ";
2140 if ($view_mode == "unread") {
2141 $view_query_part = " unread = true AND ";
2145 $limit_query_part = "LIMIT " . $limit;
2148 $vfeed_query_part = "";
2150 // override query strategy and enable feed display when searching globally
2151 if ($search && $search_mode == "all_feeds") {
2152 $query_strategy_part = "ttrss_entries.id > 0";
2153 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2154 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2155 $query_strategy_part = "ttrss_entries.id > 0";
2156 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2157 id = feed_id) as feed_title,";
2158 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2160 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2162 $tmp_result = false;
2165 $tmp_result = db_query($link, "SELECT id
2166 FROM ttrss_feeds WHERE cat_id = '$feed'");
2168 $tmp_result = db_query($link, "SELECT id
2169 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2170 WHERE id = '$feed') AND id != '$feed'");
2173 $cat_siblings = array();
2175 if (db_num_rows($tmp_result) > 0) {
2176 while ($p = db_fetch_assoc($tmp_result)) {
2177 array_push($cat_siblings, "feed_id = " . $p["id"]);
2180 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2181 $feed, implode(" OR ", $cat_siblings));
2184 $query_strategy_part = "ttrss_entries.id > 0";
2187 } else if ($feed >= 0) {
2192 $query_strategy_part = "cat_id = '$feed'";
2194 $query_strategy_part = "cat_id IS NULL";
2197 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2200 $tmp_result = db_query($link, "SELECT id
2201 FROM ttrss_feeds WHERE parent_feed = '$feed'
2202 ORDER BY cat_id,title");
2204 $parent_ids = array();
2206 if (db_num_rows($tmp_result) > 0) {
2207 while ($p = db_fetch_assoc($tmp_result)) {
2208 array_push($parent_ids, "feed_id = " . $p["id"]);
2211 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2212 $feed, implode(" OR ", $parent_ids));
2214 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2216 $query_strategy_part = "feed_id = '$feed'";
2219 } else if ($feed == -1) { // starred virtual feed
2220 $query_strategy_part = "marked = true";
2221 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2222 } else if ($feed <= -10) { // labels
2223 $label_id = -$feed - 11;
2225 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2226 WHERE id = '$label_id'");
2228 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2230 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2232 $query_strategy_part = "id > 0"; // dumb
2235 if (get_pref($link, 'REVERSE_HEADLINES')) {
2236 $order_by = "updated";
2238 $order_by = "updated DESC";
2241 if ($override_order) {
2242 $order_by = $override_order;
2247 if ($search && $search_mode == "all_feeds") {
2248 $feed_title = "Global search results ($search)";
2249 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2250 $feed_title = "Tag search results ($search, $feed)";
2251 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2252 $feed_title = $feed;
2253 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2258 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2259 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2260 $feed_title = db_fetch_result($result, 0, "title");
2262 $feed_title = "Uncategorized";
2266 $feed_title = "Category search results ($search, $feed_title)";
2271 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2272 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2274 $feed_title = db_fetch_result($result, 0, "title");
2275 $feed_site_url = db_fetch_result($result, 0, "site_url");
2276 $last_error = db_fetch_result($result, 0, "last_error");
2279 $feed_title = "Feed search results ($search, $feed_title)";
2283 } else if ($feed == -1) {
2284 $feed_title = "Starred articles";
2285 } else if ($feed < -10) {
2286 $label_id = -$feed - 11;
2287 $result = db_query($link, "SELECT description FROM ttrss_labels
2288 WHERE id = '$label_id'");
2289 $feed_title = db_fetch_result($result, 0, "description");
2292 $feed_title = "Label search results ($search, $feed_title)";
2298 $feed_title = db_unescape_string($feed_title);
2300 if ($feed < -10) error_reporting (0);
2302 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2305 $feed_kind = "Feeds";
2307 $feed_kind = "Labels";
2310 $content_query_part = "content as content_preview,";
2314 ttrss_entries.id,ttrss_entries.title,
2315 SUBSTRING(updated,1,16) as updated,
2316 unread,feed_id,marked,link,last_read,
2317 SUBSTRING(last_read,1,19) as last_read_noms,
2320 SUBSTRING(updated,1,19) as updated_noms,
2323 ttrss_entries,ttrss_user_entries,ttrss_feeds
2325 ttrss_feeds.hidden = false AND
2326 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2327 ttrss_user_entries.ref_id = ttrss_entries.id AND
2328 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2331 $query_strategy_part ORDER BY $order_by
2334 $result = db_query($link, $query);
2336 if ($_GET["debug"]) print $query;
2341 $feed_kind = "Tags";
2343 $result = db_query($link, "SELECT
2345 ttrss_entries.id as id,title,
2346 SUBSTRING(updated,1,16) as updated,
2348 marked,link,last_read,
2349 SUBSTRING(last_read,1,19) as last_read_noms,
2352 SUBSTRING(updated,1,19) as updated_noms
2354 ttrss_entries,ttrss_user_entries,ttrss_tags
2356 ref_id = ttrss_entries.id AND
2357 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2358 post_int_id = int_id AND tag_name = '$feed' AND
2361 $query_strategy_part ORDER BY $order_by
2362 $limit_query_part");
2365 return array($result, $feed_title, $feed_site_url, $last_error);
2369 function generate_syndicated_feed($link, $feed, $is_cat,
2370 $search, $search_mode, $match_on) {
2372 $qfh_ret = queryFeedHeadlines($link, $feed,
2373 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2375 $result = $qfh_ret[0];
2376 $feed_title = htmlspecialchars($qfh_ret[1]);
2377 $feed_site_url = $qfh_ret[2];
2378 $last_error = $qfh_ret[3];
2380 print "<rss version=\"2.0\">
2382 <title>$feed_title</title>
2383 <link>$feed_site_url</link>
2384 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2386 while ($line = db_fetch_assoc($result)) {
2388 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2389 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2391 $rfc822_date = date('r', strtotime($line["updated"]));
2393 print "<pubDate>$rfc822_date</pubDate>";
2396 htmlspecialchars($line["title"]) . "</title>";
2398 print "<description>" .
2399 htmlspecialchars($line["content_preview"]) . "</description>";
2404 print "</channel></rss>";
2408 function getCategoryTitle($link, $cat_id) {
2410 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2413 if (db_num_rows($result) == 1) {
2414 return db_fetch_result($result, 0, "title");
2416 return "Uncategorized";
2420 function sanitize_rss($str) {
2423 $res = preg_replace('/<script.*?>/i',
2424 "<p class=\"scriptWarn\">Disabled script: ", $res);
2426 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2428 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2430 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2431 "<p class=\"objectWarn\">(Disabled html object
2432 - flash or other embedded content)</p>", $res); */
2437 function send_headlines_digests($link, $limit = 100) {
2439 if (!DIGEST_ENABLE
) return false;
2441 $user_limit = DIGEST_EMAIL_LIMIT
;
2444 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2446 if (DB_TYPE
== "pgsql") {
2447 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2448 } else if (DB_TYPE
== "mysql") {
2449 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2452 $result = db_query($link, "SELECT id,email FROM ttrss_users
2453 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2455 while ($line = db_fetch_assoc($result)) {
2456 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2457 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2459 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2460 $digest = $tuple[0];
2461 $headlines_count = $tuple[1];
2463 if ($headlines_count > 0) {
2464 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2465 "[tt-rss] New headlines for last 24 hours", $digest,
2466 "From: " . MAIL_FROM
. "\n".
2467 "Content-Type: text/plain; charset=\"utf-8\"\n".
2468 "Content-Transfer-Encoding: 8bit\n");
2470 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2471 WHERE id = " . $line["id"]);
2473 print "No headlines\n";
2478 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2482 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2483 $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n";
2484 $tmp .= "=======================================================\n\n";
2486 if (DB_TYPE
== "pgsql") {
2487 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2488 } else if (DB_TYPE
== "mysql") {
2489 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2492 $result = db_query($link, "SELECT ttrss_entries.title,
2493 ttrss_feeds.title AS feed_title,
2496 SUBSTRING(last_updated,1,19) AS last_updated
2498 ttrss_user_entries,ttrss_entries,ttrss_feeds
2500 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2501 AND include_in_digest = true
2503 AND ttrss_user_entries.owner_uid = $user_id
2504 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2507 $cur_feed_title = "";
2509 $headlines_count = db_num_rows($result);
2511 while ($line = db_fetch_assoc($result)) {
2512 $updated = smart_date_time(strtotime($line["last_updated"]));
2513 $feed_title = $line["feed_title"];
2515 if ($cur_feed_title != $feed_title) {
2516 $cur_feed_title = $feed_title;
2518 $tmp .= "$feed_title\n\n";
2521 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2522 $tmp .= " " . trim($line["link"]) . "\n";
2527 $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " .
2528 DIGEST_HOSTNAME
. "\n".
2529 "To unsubscribe, visit your configuration options or contact instance owner.\n";
2532 return array($tmp, $headlines_count);
2535 function check_for_update($link) {
2536 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2538 if (!CHECK_FOR_NEW_VERSION ||
$_SESSION["access_level"] < 10) {
2543 $rss = fetch_rss($releases_feed);
2544 error_reporting (DEFAULT_ERROR_LEVEL
);
2548 $items = $rss->items
;
2550 if (!$items ||
!is_array($items)) $items = $rss->entries
;
2551 if (!$items ||
!is_array($items)) $items = $rss;
2553 if (!is_array($items) ||
count($items) == 0) {
2557 $latest_item = $items[0];
2559 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2561 $release_url = sanitize_rss($latest_item["link"]);
2562 $content = sanitize_rss($latest_item["description"]);
2564 if (version_compare(VERSION
, $latest_version) == -1) {
2565 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2566 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2567 <div id=\"milestoneDetails\">$content</div></div>";
2572 function markArticlesById($link, $ids, $cmode) {
2576 foreach ($ids as $id) {
2577 array_push($tmp_ids, "ref_id = '$id'");
2580 $ids_qpart = join(" OR ", $tmp_ids);
2583 db_query($link, "UPDATE ttrss_user_entries SET
2584 marked = false,last_read = NOW()
2585 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2586 } else if ($cmode == 1) {
2587 db_query($link, "UPDATE ttrss_user_entries SET
2589 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2591 db_query($link, "UPDATE ttrss_user_entries SET
2592 marked = NOT marked,last_read = NOW()
2593 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2597 function catchupArticlesById($link, $ids, $cmode) {
2601 foreach ($ids as $id) {
2602 array_push($tmp_ids, "ref_id = '$id'");
2605 $ids_qpart = join(" OR ", $tmp_ids);
2608 db_query($link, "UPDATE ttrss_user_entries SET
2609 unread = false,last_read = NOW()
2610 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2611 } else if ($cmode == 1) {
2612 db_query($link, "UPDATE ttrss_user_entries SET
2614 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2616 db_query($link, "UPDATE ttrss_user_entries SET
2617 unread = NOT unread,last_read = NOW()
2618 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2622 function escape_for_form($s) {
2623 return htmlspecialchars(db_unescape_string($s));
2626 function make_guid_from_title($title) {
2627 return preg_replace("/[ \"\',.:;]/", "-",
2628 mb_strtolower(strip_tags($title)));
2631 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2632 $bottom = false, $rtl_content = false, $feed_id = 0,
2633 $is_cat = false, $search = false, $match_on = false,
2634 $search_mode = false) {
2637 $class = "headlinesSubToolbar";
2638 $tid = "headlineActionsTop";
2640 $class = "headlinesSubToolbar";
2641 $tid = "headlineActionsBottom";
2644 print "<table class=\"$class\" id=\"$tid\"
2645 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2653 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2655 print "<td class=\"headlineActions$rtl_cpart\">
2657 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
2658 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
2659 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
2661 Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>,
2662 <a href=\"javascript:selectionToggleMarked()\">Starred</a>
2665 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2666 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2669 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS
) {
2670 print "<td class=\"headlineActions$rtl_cpart\">
2671 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2672 '$match_on', '$feed_id', '$is_cat');\">
2673 Convert this search to label</a></td>";
2678 print "<td class=\"headlineActions$rtl_cpart\">
2680 <a href=\"javascript:cdmSelectArticles('all')\">All</a>,
2681 <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
2682 <a href=\"javascript:cdmSelectArticles('none')\">None</a>
2684 Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>,
2685 <a href=\"javascript:selectionToggleMarked(true)\">Starred</a>
2688 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2689 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2695 print "<td class=\"headlineTitle$rtl_cpart\">";
2697 if ($feed_site_url) {
2699 $target = "target=\"_blank\"";
2701 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2707 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2713 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2714 <img class=\"noborder\"
2715 alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\">
2720 print "</tr></table>";
2724 function outputFeedList($link, $tags = false) {
2726 print "<ul class=\"feedList\" id=\"feedList\">\n";
2728 $owner_uid = $_SESSION["uid"];
2734 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2735 print "<li class=\"feedCat\">Special</li>";
2736 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2739 $num_starred = getFeedUnread($link, -1);
2743 if ($num_starred > 0) $class .= "Unread";
2745 printFeedEntry(-1, $class, "Starred articles", $num_starred,
2746 "images/mark_set.png", $link);
2748 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2752 if (GLOBAL_ENABLE_LABELS
&& get_pref($link, 'ENABLE_LABELS')) {
2754 $result = db_query($link, "SELECT id,sql_exp,description FROM
2755 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2757 if (db_num_rows($result) > 0) {
2758 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2759 print "<li class=\"feedCat\">Labels</li>";
2760 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2762 print "<li><hr></li>";
2766 while ($line = db_fetch_assoc($result)) {
2768 error_reporting (0);
2770 $label_id = -$line['id'] - 11;
2771 $count = getFeedUnread($link, $label_id);
2779 error_reporting (DEFAULT_ERROR_LEVEL
);
2781 printFeedEntry($label_id,
2782 $class, db_unescape_string($line["description"]),
2783 $count, "images/label.png", $link);
2787 if (db_num_rows($result) > 0) {
2788 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2795 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2796 print "<li><hr></li>";
2799 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2800 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2801 $order_by_qpart = "category,unread DESC,title";
2803 $order_by_qpart = "category,title";
2806 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2807 $order_by_qpart = "unread DESC,title";
2809 $order_by_qpart = "title";
2813 $result = db_query($link, "SELECT ttrss_feeds.*,
2814 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2815 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2816 WHERE feed_id = ttrss_feeds.id AND unread = true
2817 AND ttrss_user_entries.ref_id = ttrss_entries.id
2818 AND owner_uid = '$owner_uid') as unread,
2820 ttrss_feed_categories.title AS category,
2821 ttrss_feed_categories.collapsed
2822 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2823 ON (ttrss_feed_categories.id = cat_id)
2825 ttrss_feeds.hidden = false AND
2826 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2827 ORDER BY $order_by_qpart");
2829 $actid = $_GET["actid"];
2839 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2841 while ($line = db_fetch_assoc($result)) {
2843 $feed = db_unescape_string($line["title"]);
2844 $feed_id = $line["id"];
2846 $subop = $_GET["subop"];
2848 $unread = $line["unread"];
2850 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2851 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2853 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2856 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2859 $rtl_tag = "dir=\"RTL\"";
2864 $tmp_result = db_query($link,
2865 "SELECT id,COUNT(unread) AS unread
2866 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2867 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2868 WHERE parent_feed = '$feed_id' AND unread = true
2869 GROUP BY ttrss_feeds.id");
2871 if (db_num_rows($tmp_result) > 0) {
2872 while ($l = db_fetch_assoc($tmp_result)) {
2873 $unread +
= $l["unread"];
2877 $cat_id = $line["cat_id"];
2879 $tmp_category = $line["category"];
2881 if (!$tmp_category) {
2882 $tmp_category = "Uncategorized";
2885 // $class = ($lnum % 2) ? "even" : "odd";
2887 if ($line["last_error"]) {
2893 if ($unread > 0) $class .= "Unread";
2895 if ($actid == $feed_id) {
2896 $class .= "Selected";
2899 $total_unread +
= $unread;
2901 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
2907 $category = $tmp_category;
2909 $collapsed = $line["collapsed"];
2911 // workaround for NULL category
2912 if ($category == "Uncategorized") {
2913 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
2918 if ($collapsed == "t" ||
$collapsed == "1") {
2919 $holder_class = "invisible";
2926 $cat_id = sprintf("%d", $cat_id);
2928 $cat_unread = getCategoryUnread($link, $cat_id);
2930 $catctr_class = ($cat_unread > 0) ?
"catCtrHasUnread" : "catCtrNoUnread";
2932 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
2933 <a id=\"FCATN-$cat_id\" href=\"#\"onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
2934 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
2935 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
2936 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
2939 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
2940 // -> keyboard navigation, etc.
2941 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
2944 printFeedEntry($feed_id, $class, $feed, $unread,
2945 "icons/$feed_id.ico", $link, $rtl_content,
2946 $last_updated, $line["last_error"]);
2951 if (db_num_rows($result) == 0) {
2952 print "<li>No feeds to display.</li>";
2959 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2960 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2961 post_int_id = ttrss_user_entries.int_id AND
2962 unread = true AND ref_id = ttrss_entries.id
2963 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
2965 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
2966 ORDER BY tag_name"); */
2968 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2969 print "<li class=\"feedCat\">Tags</li>";
2970 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2973 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2974 FROM ttrss_user_entries WHERE int_id = post_int_id
2975 AND unread = true)) AS count FROM ttrss_tags
2976 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
2980 while ($line = db_fetch_assoc($result)) {
2981 $tags[$line["tag_name"]] +
= $line["count"];
2984 foreach (array_keys($tags) as $tag) {
2986 $unread = $tags[$tag];
2994 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
2998 if (db_num_rows($result) == 0) {
2999 print "<li>No tags to display.</li>";
3002 if (get_pref($link, 'ENABLE_FEED_CATS')) {