]>
git.wh0rd.org - tt-rss.git/blob - functions.php
a58613ccb7d63f5fb2bada6d8a04d98d79e7540c
3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
9 require_once 'config.php';
10 require_once 'db-prefs.php';
11 require_once 'compat.php';
12 require_once 'errors.php';
13 require_once 'version.php';
15 if (RSS_BACKEND_TYPE
== "magpie") {
16 require_once 'magpierss/rss_utils.inc';
17 } else if (RSS_BACKEND_TYPE
== "simplepie") {
18 require_once 'simplepie/simplepie.inc';
21 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
23 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
27 if (DB_TYPE
== "pgsql") {
28 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
29 marked = false AND feed_id = '$feed_id' AND
30 (SELECT date_entered FROM ttrss_entries WHERE
31 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
33 $pg_version = get_pgsql_version($link);
35 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
37 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
38 ttrss_entries.id = ref_id AND
40 feed_id = '$feed_id' AND
41 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
45 $result = db_query($link, "DELETE FROM ttrss_user_entries
47 WHERE ttrss_entries.id = ref_id AND
49 feed_id = '$feed_id' AND
50 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
53 $rows = pg_affected_rows($result);
57 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
58 marked = false AND feed_id = '$feed_id' AND
59 (SELECT date_entered FROM ttrss_entries WHERE
60 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
62 $result = db_query($link, "DELETE FROM ttrss_user_entries
63 USING ttrss_user_entries, ttrss_entries
64 WHERE ttrss_entries.id = ref_id AND
66 feed_id = '$feed_id' AND
67 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
69 $rows = mysql_affected_rows($link);
74 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
78 function global_purge_old_posts($link, $do_output = false, $limit = false) {
80 $random_qpart = sql_random_function();
83 $limit_qpart = "LIMIT $limit";
88 $result = db_query($link,
89 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
90 ORDER BY $random_qpart $limit_qpart");
92 while ($line = db_fetch_assoc($result)) {
94 $feed_id = $line["id"];
95 $purge_interval = $line["purge_interval"];
96 $owner_uid = $line["owner_uid"];
98 if ($purge_interval == 0) {
100 $tmp_result = db_query($link,
101 "SELECT value FROM ttrss_user_prefs WHERE
102 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
104 if (db_num_rows($tmp_result) != 0) {
105 $purge_interval = db_fetch_result($tmp_result, 0, "value");
110 // print "Feed $feed_id: purge interval = $purge_interval\n";
113 if ($purge_interval > 0) {
114 purge_feed($link, $feed_id, $purge_interval, $do_output);
118 // purge orphaned posts in main content table
119 db_query($link, "DELETE FROM ttrss_entries WHERE
120 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
124 function purge_old_posts($link) {
126 $user_id = $_SESSION["uid"];
128 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
129 WHERE owner_uid = '$user_id'");
131 while ($line = db_fetch_assoc($result)) {
133 $feed_id = $line["id"];
134 $purge_interval = $line["purge_interval"];
136 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
138 if ($purge_interval > 0) {
139 purge_feed($link, $feed_id, $purge_interval);
143 // purge orphaned posts in main content table
144 db_query($link, "DELETE FROM ttrss_entries WHERE
145 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
148 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
150 if (WEB_DEMO_MODE
) return;
153 $user_id = $_SESSION["uid"];
154 purge_old_posts($link);
157 // db_query($link, "BEGIN");
159 if (MAX_UPDATE_TIME
> 0) {
160 if (DB_TYPE
== "mysql") {
163 $q_order = "RANDOM()";
166 $q_order = "last_updated DESC";
169 $result = db_query($link, "SELECT feed_url,id,
170 SUBSTRING(last_updated,1,19) AS last_updated,
171 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
176 while ($line = db_fetch_assoc($result)) {
177 $upd_intl = $line["update_interval"];
179 if (!$upd_intl ||
$upd_intl == 0) {
180 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
184 // Updates for this feed are disabled
188 if ($fetch ||
(!$line["last_updated"] ||
189 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
191 // print "<!-- feed: ".$line["feed_url"]." -->";
193 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
195 $upd_elapsed = time() - $upd_start;
197 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
203 // db_query($link, "COMMIT");
207 function check_feed_favicon($feed_url, $feed, $link) {
208 $feed_url = str_replace("http://", "", $feed_url);
209 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
211 $icon_url = "http://$feed_url/favicon.ico";
212 $icon_file = ICONS_DIR
. "/$feed.ico";
214 if (!file_exists($icon_file)) {
217 $r = fopen($icon_url, "r");
218 error_reporting (DEFAULT_ERROR_LEVEL
);
221 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
223 $t = fopen($tmpfname, "w");
226 $buf = fread($r, 16384);
234 if (!rename($tmpfname, $icon_file)) {
238 chmod($icon_file, 0644);
240 error_reporting (DEFAULT_ERROR_LEVEL
);
246 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
248 if (WEB_DEMO_MODE
) return;
250 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
254 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
255 FROM ttrss_feeds WHERE id = '$feed'");
257 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
258 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
260 $update_interval = db_fetch_result($result, 0, "update_interval");
262 if ($update_interval < 0) { return; }
264 $feed = db_escape_string($feed);
266 $fetch_url = $feed_url;
268 if ($auth_login && $auth_pass) {
269 $url_parts = array();
270 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
272 if ($url_parts[1] && $url_parts[2]) {
273 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
278 if (RSS_BACKEND_TYPE
== "magpie") {
280 $rss = fetch_rss($fetch_url);
281 error_reporting (DEFAULT_ERROR_LEVEL
);
282 } else if (RSS_BACKEND_TYPE
== "simplepie") {
284 if (!file_exists(SIMPLEPIE_CACHE_DIR
)) {
285 mkdir(SIMPLEPIE_CACHE_DIR
);
288 $rss = new SimplePie();
289 $rss->feed_url($fetch_url);
290 $rss->cache_location(SIMPLEPIE_CACHE_DIR
);
294 $feed = db_escape_string($feed);
298 if (RSS_BACKEND_TYPE
== "simplepie") {
299 $rss_check = $rss->data
;
304 // db_query($link, "BEGIN");
306 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
307 FROM ttrss_feeds WHERE id = '$feed'");
309 $registered_title = db_fetch_result($result, 0, "title");
310 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
311 $orig_site_url = db_fetch_result($result, 0, "site_url");
313 $owner_uid = db_fetch_result($result, 0, "owner_uid");
315 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
316 check_feed_favicon($feed_url, $feed, $link);
319 if (!$registered_title ||
$registered_title == "[Unknown]") {
321 if (RSS_BACKEND_TYPE
== "magpie") {
322 $feed_title = db_escape_string($rss->channel
["title"]);
324 $feed_title = $rss->get_feed_title();
327 db_query($link, "UPDATE ttrss_feeds SET
328 title = '$feed_title' WHERE id = '$feed'");
331 if (RSS_BACKEND_TYPE
== "magpie") {
332 $site_url = $rss->channel
["link"];
333 // weird, weird Magpie
334 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
336 $site_url = $rss->get_feed_link();
339 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
340 db_query($link, "UPDATE ttrss_feeds SET
341 site_url = '$site_url' WHERE id = '$feed'");
344 // print "I: " . $rss->channel["image"]["url"];
346 if (RSS_BACKEND_TYPE
== "magpie") {
347 $icon_url = $rss->image
["url"];
349 $icon_url = $rss->get_image_url(); # FIXME
352 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
353 $icon_url = db_escape_string($icon_url);
354 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
360 $result = db_query($link, "SELECT reg_exp,
361 ttrss_filter_types.name AS name,
362 ttrss_filter_actions.name AS action
363 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
364 owner_uid = $owner_uid AND
365 ttrss_filter_types.id = filter_type AND
366 ttrss_filter_actions.id = action_id AND
367 (feed_id IS NULL OR feed_id = '$feed')");
369 while ($line = db_fetch_assoc($result)) {
370 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
372 $filter["reg_exp"] = $line["reg_exp"];
373 $filter["action"] = $line["action"];
375 array_push($filters[$line["name"]], $filter);
378 if (RSS_BACKEND_TYPE
== "magpie") {
379 $iterator = $rss->items
;
381 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
382 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
385 $iterator = $rss->get_items();
388 if (!is_array($iterator)) {
389 /* db_query($link, "UPDATE ttrss_feeds
390 SET last_error = 'Parse error: can\'t find any articles.'
391 WHERE id = '$feed'"); */
395 foreach ($iterator as $item) {
397 if (RSS_BACKEND_TYPE
== "magpie") {
399 $entry_guid = $item["id"];
401 if (!$entry_guid) $entry_guid = $item["guid"];
402 if (!$entry_guid) $entry_guid = $item["link"];
404 if (!$entry_guid) continue;
406 $entry_timestamp = "";
408 $rss_2_date = $item['pubdate'];
409 $rss_1_date = $item['dc']['date'];
410 $atom_date = $item['issued'];
411 if (!$atom_date) $atom_date = $item['updated'];
413 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
414 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
415 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
417 if ($entry_timestamp == "") {
418 $entry_timestamp = time();
419 $no_orig_date = 'true';
421 $no_orig_date = 'false';
424 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
426 $entry_title = $item["title"];
428 // strange Magpie workaround
429 $entry_link = $item["link_"];
430 if (!$entry_link) $entry_link = $item["link"];
432 if (!$entry_title) continue;
433 if (!$entry_link) continue;
435 $entry_content = $item["content:escaped"];
437 if (!$entry_content) $entry_content = $item["content:encoded"];
438 if (!$entry_content) $entry_content = $item["content"];
439 if (!$entry_content) $entry_content = $item["atom_content"];
440 if (!$entry_content) $entry_content = $item["summary"];
441 if (!$entry_content) $entry_content = $item["description"];
443 // if (!$entry_content) continue;
446 if (is_array($entry_content)) {
447 $entry_content = $entry_content["encoded"];
448 if (!$entry_content) $entry_content = $entry_content["escaped"];
452 // print_r(htmlspecialchars($entry_content));
455 $entry_content_unescaped = $entry_content;
456 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
458 $entry_comments = $item["comments"];
460 $entry_author = db_escape_string($item['dc']['creator']);
462 $entry_guid = db_escape_string($entry_guid);
464 $result = db_query($link, "SELECT id FROM ttrss_entries
465 WHERE guid = '$entry_guid'");
467 $entry_content = db_escape_string($entry_content);
468 $entry_title = db_escape_string($entry_title);
469 $entry_link = db_escape_string($entry_link);
470 $entry_comments = db_escape_string($entry_comments);
472 $num_comments = db_escape_string($item["slash"]["comments"]);
474 if (!$num_comments) $num_comments = 0;
476 } else if (RSS_BACKEND_TYPE
== "simplepie") {
478 $entry_guid = $item->get_id();
481 $entry_guid = $item->get_permalink();
484 if (!$entry_guid) continue;
486 $entry_timestamp = $item->get_date("U");
488 if ($entry_timestamp == "") {
489 $entry_timestamp = time();
490 $no_orig_date = 'true';
492 $no_orig_date = 'false';
495 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
497 $entry_title = $item->get_title();
498 $entry_link = $item->get_permalink();
500 if (!$entry_title) continue;
501 if (!$entry_link) continue;
503 $entry_content = $item->get_description();
505 // print_r(htmlspecialchars($entry_content));
508 $entry_content_unescaped = $entry_content;
509 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
511 $entry_comments = ""; # FIXME
513 $entry_author = $item->get_author(0);
515 $entry_author = db_escape_string($entry_author->name
);
517 $entry_guid = db_escape_string($entry_guid);
519 $result = db_query($link, "SELECT id FROM ttrss_entries
520 WHERE guid = '$entry_guid'");
522 $entry_content = db_escape_string($entry_content);
523 $entry_title = db_escape_string($entry_title);
524 $entry_link = db_escape_string($entry_link);
525 $entry_comments = db_escape_string($entry_comments);
527 $num_comments = 0; # FIXME
529 if (!$num_comments) $num_comments = 0;
533 db_query($link, "BEGIN");
535 if (db_num_rows($result) == 0) {
537 // base post entry does not exist, create it
539 $result = db_query($link,
540 "INSERT INTO ttrss_entries
556 '$entry_timestamp_fmt',
565 // we keep encountering the entry in feeds, so we need to
566 // update date_entered column so that we don't get horrible
567 // dupes when the entry gets purged and reinserted again e.g.
568 // in the case of SLOW SLOW OMG SLOW updating feeds
570 $base_entry_id = db_fetch_result($result, 0, "id");
572 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
573 WHERE id = '$base_entry_id'");
576 // now it should exist, if not - bad luck then
578 $result = db_query($link, "SELECT
579 id,content_hash,no_orig_date,title,
580 substring(date_entered,1,19) as date_entered,
581 substring(updated,1,19) as updated,
585 WHERE guid = '$entry_guid'");
587 if (db_num_rows($result) == 1) {
589 // this will be used below in update handler
590 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
591 $orig_title = db_fetch_result($result, 0, "title");
592 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
593 $orig_date_entered = strtotime(db_fetch_result($result,
596 $ref_id = db_fetch_result($result, 0, "id");
598 // check for user post link to main table
600 // do we allow duplicate posts with same GUID in different feeds?
601 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
602 $dupcheck_qpart = "AND feed_id = '$feed'";
604 $dupcheck_qpart = "";
607 // error_reporting(0);
609 $filter_name = get_filter_name($entry_title, $entry_content,
610 $entry_link, $filters);
612 if ($filter_name == "filter") {
616 // error_reporting (DEFAULT_ERROR_LEVEL);
618 $result = db_query($link,
619 "SELECT ref_id FROM ttrss_user_entries WHERE
620 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
623 // okay it doesn't exist - create user entry
624 if (db_num_rows($result) == 0) {
626 if ($filter_name != 'catchup') {
628 $last_read_qpart = 'NULL';
631 $last_read_qpart = 'NOW()';
634 if ($filter_name == 'mark') {
640 $result = db_query($link,
641 "INSERT INTO ttrss_user_entries
642 (ref_id, owner_uid, feed_id, unread, last_read, marked)
643 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
644 $last_read_qpart, $marked)");
647 $post_needs_update = false;
649 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
650 ($content_hash != $orig_content_hash)) {
651 $post_needs_update = true;
654 if ($orig_title != $entry_title) {
655 $post_needs_update = true;
658 if ($orig_num_comments != $num_comments) {
659 $post_needs_update = true;
662 // this doesn't seem to be very reliable
664 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
665 // $post_needs_update = true;
668 // if post needs update, update it and mark all user entries
669 // linking to this post as updated
670 if ($post_needs_update) {
672 // print "<!-- post $orig_title needs update : $post_needs_update -->";
674 db_query($link, "UPDATE ttrss_entries
675 SET title = '$entry_title', content = '$entry_content',
676 num_comments = '$num_comments'
677 WHERE id = '$ref_id'");
679 if (get_pref($link, "MARK_UNREAD_ON_UPDATE")) {
680 db_query($link, "UPDATE ttrss_user_entries
681 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
683 db_query($link, "UPDATE ttrss_user_entries
684 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
690 db_query($link, "COMMIT");
693 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
697 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
698 $entry_content_unescaped, $entry_tags);
700 // print "<br>$entry_title : $entry_content_unescaped<br>";
701 // print_r($entry_tags);
704 $entry_tags = $entry_tags[1];
706 if (count($entry_tags) > 0) {
708 db_query($link, "BEGIN");
710 $result = db_query($link, "SELECT id,int_id
711 FROM ttrss_entries,ttrss_user_entries
712 WHERE guid = '$entry_guid'
713 AND feed_id = '$feed' AND ref_id = id
714 AND owner_uid = '$owner_uid'");
716 if (db_num_rows($result) == 1) {
718 $entry_id = db_fetch_result($result, 0, "id");
719 $entry_int_id = db_fetch_result($result, 0, "int_id");
721 foreach ($entry_tags as $tag) {
722 $tag = db_escape_string(strtolower($tag));
724 $tag = str_replace("+", " ", $tag);
725 $tag = str_replace("technorati tag: ", "", $tag);
727 $result = db_query($link, "SELECT id FROM ttrss_tags
728 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
729 owner_uid = '$owner_uid' LIMIT 1");
731 // print db_fetch_result($result, 0, "id");
733 if ($result && db_num_rows($result) == 0) {
735 // print "tagging $entry_id as $tag<br>";
737 db_query($link, "INSERT INTO ttrss_tags
738 (owner_uid,tag_name,post_int_id)
739 VALUES ('$owner_uid','$tag', '$entry_int_id')");
743 db_query($link, "COMMIT");
747 db_query($link, "UPDATE ttrss_feeds
748 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
750 // db_query($link, "COMMIT");
753 $error_msg = db_escape_string(magpie_error());
755 "UPDATE ttrss_feeds SET last_error = '$error_msg',
756 last_updated = NOW() WHERE id = '$feed'");
761 function print_select($id, $default, $values, $attributes = "") {
762 print "<select name=\"$id\" id=\"$id\" $attributes>";
763 foreach ($values as $v) {
769 print "<option$sel>$v</option>";
774 function print_select_hash($id, $default, $values, $attributes = "") {
775 print "<select name=\"$id\" id='$id' $attributes>";
776 foreach (array_keys($values) as $v) {
782 print "<option $sel value=\"$v\">".$values[$v]."</option>";
788 function get_filter_name($title, $content, $link, $filters) {
790 if ($filters["title"]) {
791 foreach ($filters["title"] as $filter) {
792 $reg_exp = $filter["reg_exp"];
793 if (preg_match("/$reg_exp/i", $title)) {
794 return $filter["action"];
799 if ($filters["content"]) {
800 foreach ($filters["content"] as $filter) {
801 $reg_exp = $filter["reg_exp"];
802 if (preg_match("/$reg_exp/i", $content)) {
803 return $filter["action"];
808 if ($filters["both"]) {
809 foreach ($filters["both"] as $filter) {
810 $reg_exp = $filter["reg_exp"];
811 if (preg_match("/$reg_exp/i", $title) ||
812 preg_match("/$reg_exp/i", $content)) {
813 return $filter["action"];
818 if ($filters["link"]) {
819 $reg_exp = $filter["reg_exp"];
820 foreach ($filters["link"] as $filter) {
821 $reg_exp = $filter["reg_exp"];
822 if (preg_match("/$reg_exp/i", $link)) {
823 return $filter["action"];
831 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
832 $rtl_content = false, $last_updated = false, $last_error = false) {
834 if (file_exists($icon_file) && filesize($icon_file) > 0) {
835 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
837 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
841 $rtl_tag = "dir=\"rtl\"";
843 $rtl_tag = "dir=\"ltr\"";
846 $error_notify_msg = "";
849 $link_title = "Error: $last_error ($last_updated)";
850 $error_notify_msg = "(Error)";
851 } else if ($last_updated) {
852 $link_title = "Updated: $last_updated";
855 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
857 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
858 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
862 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
867 $fctr_class = "class=\"invisible\"";
870 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
871 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
873 if (get_pref($link, "EXTENDED_FEEDLIST")) {
874 print "<div class=\"feedExtInfo\">
875 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
882 function getmicrotime() {
883 list($usec, $sec) = explode(" ",microtime());
884 return ((float)$usec +
(float)$sec);
887 function print_radio($id, $default, $values, $attributes = "") {
888 foreach ($values as $v) {
896 $sel .= " value=\"1\"";
898 $sel .= " value=\"0\"";
901 print "<input class=\"noborder\"
902 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
907 function initialize_user_prefs($link, $uid) {
909 $uid = db_escape_string($uid);
911 db_query($link, "BEGIN");
913 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
915 $u_result = db_query($link, "SELECT pref_name
916 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
918 $active_prefs = array();
920 while ($line = db_fetch_assoc($u_result)) {
921 array_push($active_prefs, $line["pref_name"]);
924 while ($line = db_fetch_assoc($result)) {
925 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
926 // print "adding " . $line["pref_name"] . "<br>";
928 db_query($link, "INSERT INTO ttrss_user_prefs
929 (owner_uid,pref_name,value) VALUES
930 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
935 db_query($link, "COMMIT");
939 function lookup_user_id($link, $user) {
941 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
944 if (db_num_rows($result) == 1) {
945 return db_fetch_result($result, 0, "id");
951 function http_authenticate_user($link) {
953 if (!$_SERVER["PHP_AUTH_USER"]) {
955 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
956 header('HTTP/1.0 401 Unauthorized');
960 $auth_result = authenticate_user($link,
961 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
964 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
965 header('HTTP/1.0 401 Unauthorized');
973 function authenticate_user($link, $login, $password) {
975 if (!SINGLE_USER_MODE
) {
977 $pwd_hash = 'SHA1:' . sha1($password);
979 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
980 login = '$login' AND pwd_hash = '$pwd_hash'");
982 if (db_num_rows($result) == 1) {
983 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
984 $_SESSION["name"] = db_fetch_result($result, 0, "login");
985 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
987 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
990 $user_theme = get_user_theme_path($link);
992 $_SESSION["theme"] = $user_theme;
993 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
995 initialize_user_prefs($link, $_SESSION["uid"]);
1004 $_SESSION["uid"] = 1;
1005 $_SESSION["name"] = "admin";
1007 $user_theme = get_user_theme_path($link);
1009 $_SESSION["theme"] = $user_theme;
1010 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1012 initialize_user_prefs($link, $_SESSION["uid"]);
1018 function make_password($length = 8) {
1021 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1025 while ($i < $length) {
1026 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1028 if (!strstr($password, $char)) {
1036 // this is called after user is created to initialize default feeds, labels
1039 // user preferences are checked on every login, not here
1041 function initialize_user($link, $uid) {
1043 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1044 values ('$uid','unread = true', 'Unread articles')");
1046 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1047 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1049 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1050 values ('$uid', 'Tiny Tiny RSS: New Releases',
1051 'http://tt-rss.spb.ru/releases.rss')");
1055 function logout_user() {
1057 if (isset($_COOKIE[session_name()])) {
1058 setcookie(session_name(), '', time()-42000, '/');
1062 function get_script_urlpath() {
1063 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1066 function get_login_redirect() {
1067 $server = $_SERVER["SERVER_NAME"];
1069 if (ENABLE_LOGIN_SSL
) {
1070 $protocol = "https";
1075 $url_path = get_script_urlpath();
1077 $redirect_uri = "$protocol://$server$url_path/login.php";
1079 return $redirect_uri;
1082 function validate_session($link) {
1083 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1084 if ($_SESSION["ip_address"]) {
1085 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1093 function basic_nosid_redirect_check() {
1094 if (!SINGLE_USER_MODE
) {
1095 if (!$_COOKIE[get_session_cookie_name()]) {
1096 $redirect_uri = get_login_redirect();
1097 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1098 header("Location: $redirect_uri?rt=$return_to");
1104 function login_sequence($link) {
1105 if (!SINGLE_USER_MODE
) {
1107 if (!validate_session($link)) {
1109 $redirect_uri = get_login_redirect();
1110 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1111 header("Location: $redirect_uri?rt=$return_to");
1115 if (!USE_HTTP_AUTH
) {
1116 if (!$_SESSION["uid"]) {
1117 $redirect_uri = get_login_redirect();
1118 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1119 header("Location: $redirect_uri?rt=$return_to");
1123 if (!$_SESSION["uid"]) {
1124 if (!$_SERVER["PHP_AUTH_USER"]) {
1126 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1127 header('HTTP/1.0 401 Unauthorized');
1131 $auth_result = authenticate_user($link,
1132 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1134 if (!$auth_result) {
1135 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1136 header('HTTP/1.0 401 Unauthorized');
1143 return authenticate_user($link, "admin", null);
1147 function truncate_string($str, $max_len) {
1148 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1149 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1155 function get_user_theme_path($link) {
1156 $result = db_query($link, "SELECT theme_path
1158 ttrss_themes,ttrss_users
1159 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1160 if (db_num_rows($result) != 0) {
1161 return db_fetch_result($result, 0, "theme_path");
1167 function smart_date_time($timestamp) {
1168 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1169 return date("G:i", $timestamp);
1170 } else if (date("Y", $timestamp) == date("Y")) {
1171 return date("M d, G:i", $timestamp);
1173 return date("Y/m/d G:i", $timestamp);
1177 function smart_date($timestamp) {
1178 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1180 } else if (date("Y", $timestamp) == date("Y")) {
1181 return date("D m", $timestamp);
1183 return date("Y/m/d", $timestamp);
1187 function sql_bool_to_string($s) {
1188 if ($s == "t" ||
$s == "1") {
1195 function sql_bool_to_bool($s) {
1196 if ($s == "t" ||
$s == "1") {
1204 function toggleEvenOdd($a) {
1211 function sanity_check($link) {
1214 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1215 $schema_version = db_fetch_result($result, 0, "schema_version");
1217 if ($schema_version != SCHEMA_VERSION
) {
1221 if ($error_code != 0) {
1229 function file_is_locked($filename) {
1231 $fp = fopen($filename, "r");
1232 error_reporting(DEFAULT_ERROR_LEVEL
);
1234 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1235 flock($fp, LOCK_UN
);
1245 function make_lockfile($filename) {
1246 $fp = fopen($filename, "w");
1248 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1255 function sql_random_function() {
1256 if (DB_TYPE
== "mysql") {
1263 function catchup_feed($link, $feed, $cat_view) {
1264 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1269 $cat_qpart = "cat_id = '$feed'";
1271 $cat_qpart = "cat_id IS NULL";
1274 $tmp_result = db_query($link, "SELECT id
1275 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1278 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1280 $tmp_feed = $tmp_line["id"];
1282 db_query($link, "UPDATE ttrss_user_entries
1283 SET unread = false,last_read = NOW()
1284 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1287 } else if ($feed > 0) {
1289 $tmp_result = db_query($link, "SELECT id
1290 FROM ttrss_feeds WHERE parent_feed = '$feed'
1291 ORDER BY cat_id,title");
1293 $parent_ids = array();
1295 if (db_num_rows($tmp_result) > 0) {
1296 while ($p = db_fetch_assoc($tmp_result)) {
1297 array_push($parent_ids, "feed_id = " . $p["id"]);
1300 $children_qpart = implode(" OR ", $parent_ids);
1302 db_query($link, "UPDATE ttrss_user_entries
1303 SET unread = false,last_read = NOW()
1304 WHERE (feed_id = '$feed' OR $children_qpart)
1305 AND owner_uid = " . $_SESSION["uid"]);
1308 db_query($link, "UPDATE ttrss_user_entries
1309 SET unread = false,last_read = NOW()
1310 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1313 } else if ($feed < 0 && $feed > -10) { // special, like starred
1316 db_query($link, "UPDATE ttrss_user_entries
1317 SET unread = false,last_read = NOW()
1318 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1321 } else if ($feed < -10) { // label
1323 // TODO make this more efficient
1325 $label_id = -$feed - 11;
1327 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1328 WHERE id = '$label_id'");
1331 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1333 db_query($link, "BEGIN");
1335 $tmp2_result = db_query($link,
1339 ttrss_user_entries,ttrss_entries
1343 owner_uid = " . $_SESSION["uid"]);
1345 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1346 db_query($link, "UPDATE
1349 unread = false, last_read = NOW()
1351 int_id = " . $tmp_line["int_id"]);
1354 db_query($link, "COMMIT");
1356 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1357 SET unread = false,last_read = NOW()
1360 AND owner_uid = ".$_SESSION["uid"]); */
1364 db_query($link, "BEGIN");
1366 $tag_name = db_escape_string($feed);
1368 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1369 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1371 while ($line = db_fetch_assoc($result)) {
1372 db_query($link, "UPDATE ttrss_user_entries SET
1373 unread = false, last_read = NOW()
1374 WHERE int_id = " . $line["post_int_id"]);
1376 db_query($link, "COMMIT");
1380 function update_generic_feed($link, $feed, $cat_view) {
1384 $cat_qpart = "cat_id = '$feed'";
1386 $cat_qpart = "cat_id IS NULL";
1389 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1390 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1392 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1393 $feed_url = $tmp_line["feed_url"];
1394 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1398 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1399 WHERE id = '$feed'");
1400 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1401 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1405 function getAllCounters($link) {
1406 getLabelCounters($link);
1407 getFeedCounters($link);
1408 getTagCounters($link);
1409 getGlobalCounters($link);
1410 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1411 getCategoryCounters($link);
1415 function getCategoryCounters($link) {
1416 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1417 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1418 AND unread = true)) AS unread FROM ttrss_feeds
1420 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1422 while ($line = db_fetch_assoc($result)) {
1423 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1424 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1425 $line["unread"]."\"/>";
1429 function getCategoryUnread($link, $cat) {
1432 $cat_query = "cat_id = '$cat'";
1434 $cat_query = "cat_id IS NULL";
1437 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1438 AND owner_uid = " . $_SESSION["uid"]);
1440 $cat_feeds = array();
1441 while ($line = db_fetch_assoc($result)) {
1442 array_push($cat_feeds, "feed_id = " . $line["id"]);
1445 if (count($cat_feeds) == 0) return 0;
1447 $match_part = implode(" OR ", $cat_feeds);
1449 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1450 FROM ttrss_user_entries
1451 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1455 # this needs to be rewritten
1456 while ($line = db_fetch_assoc($result)) {
1457 $unread +
= $line["unread"];
1464 function getFeedUnread($link, $feed, $is_cat = false) {
1465 $n_feed = sprintf("%d", $feed);
1468 return getCategoryUnread($link, $n_feed);
1469 } else if ($n_feed == -1) {
1470 $match_part = "marked = true";
1471 } else if ($n_feed > 0) {
1473 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1474 AND owner_uid = " . $_SESSION["uid"]);
1476 if (db_num_rows($result) > 0) {
1478 $linked_feeds = array();
1479 while ($line = db_fetch_assoc($result)) {
1480 array_push($linked_feeds, "feed_id = " . $line["id"]);
1483 $match_part = implode(" OR ", $linked_feeds);
1485 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1486 FROM ttrss_user_entries
1487 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1491 # this needs to be rewritten
1492 while ($line = db_fetch_assoc($result)) {
1493 $unread +
= $line["unread"];
1499 $match_part = "feed_id = '$n_feed'";
1501 } else if ($feed < -10) {
1502 $label_id = -$feed - 11;
1504 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1505 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1507 $match_part = db_fetch_result($result, 0, "sql_exp");
1512 $result = db_query($link, "SELECT count(int_id) AS unread
1513 FROM ttrss_user_entries
1514 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1518 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1519 FROM ttrss_tags,ttrss_user_entries
1520 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1521 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1524 $unread = db_fetch_result($result, 0, "unread");
1528 /* FIXME this needs reworking */
1530 function getGlobalUnread($link) {
1531 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1532 WHERE unread = true AND
1533 ttrss_user_entries.ref_id = ttrss_entries.id AND
1534 owner_uid = " . $_SESSION["uid"]);
1535 $c_id = db_fetch_result($result, 0, "c_id");
1539 function getGlobalCounters($link, $global_unread = -1) {
1540 if ($global_unread == -1) {
1541 $global_unread = getGlobalUnread($link);
1543 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1546 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1549 if (!$_SESSION["tctr_last_value"]) {
1550 $_SESSION["tctr_last_value"] = array();
1554 $old_counters = $_SESSION["tctr_last_value"];
1556 $tctrs_modified = false;
1558 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1559 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1560 ttrss_user_entries.ref_id = ttrss_entries.id AND
1561 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1562 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1564 select tag_name,0 as count FROM ttrss_tags
1565 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1567 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1568 FROM ttrss_user_entries WHERE int_id = post_int_id
1569 AND unread = true)) AS count FROM ttrss_tags
1570 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1574 while ($line = db_fetch_assoc($result)) {
1575 $tags[$line["tag_name"]] +
= $line["count"];
1578 foreach (array_keys($tags) as $tag) {
1579 $unread = $tags[$tag];
1581 $tag = htmlspecialchars($tag);
1583 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1584 $old_counters[$tag] = $unread;
1585 $tctrs_modified = true;
1586 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1591 if ($smart_mode && $tctrs_modified) {
1592 $_SESSION["tctr_last_value"] = $old_counters;
1597 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1600 if (!$_SESSION["lctr_last_value"]) {
1601 $_SESSION["lctr_last_value"] = array();
1607 $old_counters = $_SESSION["lctr_last_value"];
1608 $lctrs_modified = false;
1610 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1611 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1612 unread = true AND owner_uid = ".$_SESSION["uid"]);
1614 $count = db_fetch_result($result, 0, "count");
1617 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1619 $ret_arr["-1"]["counter"] = $count;
1620 $ret_arr["-1"]["description"] = "Starred";
1623 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1624 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1626 while ($line = db_fetch_assoc($result)) {
1628 $id = -$line["id"] - 11;
1630 $label_name = $line["description"];
1632 error_reporting (0);
1634 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1635 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1636 ttrss_user_entries.ref_id = ttrss_entries.id AND
1637 owner_uid = ".$_SESSION["uid"]);
1639 $count = db_fetch_result($tmp_result, 0, "count");
1641 if (!$smart_mode ||
$old_counters[$id] != $count) {
1642 $old_counters[$id] = $count;
1643 $lctrs_modified = true;
1645 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1647 $ret_arr[$id]["counter"] = $count;
1648 $ret_arr[$id]["description"] = $label_name;
1652 error_reporting (DEFAULT_ERROR_LEVEL
);
1655 if ($smart_mode && $lctrs_modified) {
1656 $_SESSION["lctr_last_value"] = $old_counters;
1662 /* function getFeedCounter($link, $id) {
1664 $result = db_query($link, "SELECT
1665 count(id) as count,last_error
1666 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1667 WHERE feed_id = '$id' AND unread = true
1668 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1669 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1671 $count = db_fetch_result($result, 0, "count");
1672 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1674 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1677 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1680 if (!$_SESSION["fctr_last_value"]) {
1681 $_SESSION["fctr_last_value"] = array();
1685 $old_counters = $_SESSION["fctr_last_value"];
1687 $result = db_query($link, "SELECT id,last_error,parent_feed,
1688 SUBSTRING(last_updated,1,19) AS last_updated,
1690 FROM ttrss_entries,ttrss_user_entries
1691 WHERE feed_id = ttrss_feeds.id AND
1692 ttrss_user_entries.ref_id = ttrss_entries.id
1693 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1694 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1695 AND parent_feed IS NULL");
1697 $fctrs_modified = false;
1699 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1701 while ($line = db_fetch_assoc($result)) {
1704 $count = $line["count"];
1705 $last_error = htmlspecialchars($line["last_error"]);
1707 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1708 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1710 $last_updated = date($short_date, strtotime($line["last_updated"]));
1713 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1715 $tmp_result = db_query($link,
1716 "SELECT id,COUNT(unread) AS unread
1717 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1718 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1719 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1721 if (db_num_rows($tmp_result) > 0) {
1722 while ($l = db_fetch_assoc($tmp_result)) {
1723 $count +
= $l["unread"];
1727 if (!$smart_mode ||
$old_counters[$id] != $count) {
1728 $old_counters[$id] = $count;
1729 $fctrs_modified = true;
1732 $error_part = "error=\"$last_error\"";
1738 $has_img_part = "hi=\"$has_img\"";
1743 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1747 if ($smart_mode && $fctrs_modified) {
1748 $_SESSION["fctr_last_value"] = $old_counters;
1752 function get_script_dt_add() {
1753 if (strpos(VERSION
, "99") === false) {
1760 function get_pgsql_version($link) {
1761 $result = db_query($link, "SELECT version() AS version");
1762 $version = split(" ", db_fetch_result($result, 0, "version"));
1766 function print_error_xml($code, $add_msg = "") {
1769 $error_msg = $ERRORS[$code];
1772 $error_msg = "$error_msg; $add_msg";
1775 print "<rpc-reply>";
1776 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1777 print "</rpc-reply>";
1780 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1782 if ($cat_id == "0" ||
!$cat_id) {
1783 $cat_qpart = "NULL";
1785 $cat_qpart = "'$cat_id'";
1788 $result = db_query($link,
1789 "SELECT id FROM ttrss_feeds
1790 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1792 if (db_num_rows($result) == 0) {
1794 $result = db_query($link,
1795 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1796 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1797 '[Unknown]', $cat_qpart)");
1799 $result = db_query($link,
1800 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1801 AND owner_uid = " . $_SESSION["uid"]);
1803 $feed_id = db_fetch_result($result, 0, "id");
1806 update_rss_feed($link, $feed_link, $feed_id, true);
1815 function print_feed_select($link, $id, $default_id = "",
1816 $attributes = "", $include_all_feeds = true) {
1818 print "<select id=\"$id\" name=\"$id\" $attributes>";
1819 if ($include_all_feeds) {
1820 print "<option value=\"0\">All feeds</option>";
1823 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1824 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1826 if (db_num_rows($result) > 0 && $include_all_feeds) {
1827 print "<option disabled>--------</option>";
1830 while ($line = db_fetch_assoc($result)) {
1831 if ($line["id"] == $default_id) {
1832 $is_selected = "selected";
1836 printf("<option $is_selected value='%d'>%s</option>",
1837 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1843 function print_feed_cat_select($link, $id, $default_id = "",
1844 $attributes = "", $include_all_cats = true) {
1846 print "<select id=\"$id\" name=\"$id\" $attributes>";
1848 if ($include_all_cats) {
1849 print "<option value=\"0\">Uncategorized</option>";
1852 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1853 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1855 if (db_num_rows($result) > 0 && $include_all_cats) {
1856 print "<option disabled>--------</option>";
1859 while ($line = db_fetch_assoc($result)) {
1860 if ($line["id"] == $default_id) {
1861 $is_selected = "selected";
1865 printf("<option $is_selected value='%d'>%s</option>",
1866 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1872 function checkbox_to_sql_bool($val) {
1873 return ($val == "on") ?
"true" : "false";
1876 function getFeedCatTitle($link, $id) {
1879 } else if ($id < -10) {
1881 } else if ($id > 0) {
1882 $result = db_query($link, "SELECT ttrss_feed_categories.title
1883 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1884 cat_id = ttrss_feed_categories.id");
1885 if (db_num_rows($result) == 1) {
1886 return db_fetch_result($result, 0, "title");
1888 return "Uncategorized";
1891 return "getFeedCatTitle($id) failed";
1896 function getFeedTitle($link, $id) {
1898 return "Starred articles";
1899 } else if ($id < -10) {
1900 $label_id = -10 - $id;
1901 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1902 if (db_num_rows($result) == 1) {
1903 return db_fetch_result($result, 0, "description");
1905 return "Unknown label ($label_id)";
1908 } else if ($id > 0) {
1909 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1910 if (db_num_rows($result) == 1) {
1911 return db_fetch_result($result, 0, "title");
1913 return "Unknown feed ($id)";
1916 return "getFeedTitle($id) failed";
1921 function get_session_cookie_name() {
1922 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
1925 function print_init_params($link) {
1926 print "<init-params>";
1927 if ($_SESSION["stored-params"]) {
1928 foreach (array_keys($_SESSION["stored-params"]) as $key) {
1930 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
1931 print "<param key=\"$key\" value=\"$value\"/>";
1936 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
1937 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
1938 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
1940 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
1941 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
1943 print "<param key=\"hide_read_feeds\" value=\"" .
1944 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
1946 print "<param key=\"feeds_sort_by_unread\" value=\"" .
1947 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
1949 print "</init-params>";
1952 function print_runtime_info($link) {
1953 print "<runtime-info>";
1954 if (ENABLE_UPDATE_DAEMON
) {
1955 print "<param key=\"daemon_is_running\" value=\"".
1956 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
1958 print "</runtime-info>";
1961 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on) {
1965 $keywords = split(" ", $search);
1966 $query_keywords = array();
1968 if ($match_on == "both") {
1970 foreach ($keywords as $k) {
1971 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
1972 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
1975 $search_query_part = implode("AND", $query_keywords) . "AND";
1977 } else if ($match_on == "title") {
1979 foreach ($keywords as $k) {
1980 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
1983 $search_query_part = implode("AND", $query_keywords) . "AND";
1985 } else if ($match_on == "content") {
1987 foreach ($keywords as $k) {
1988 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
1991 $search_query_part = implode("AND", $query_keywords) . "AND";
1994 $search_query_part = "";
1997 $view_query_part = "";
1999 if ($view_mode == "adaptive") {
2001 $view_query_part = " ";
2002 } else if ($feed != -1) {
2003 $unread = getFeedUnread($link, $feed, $cat_view);
2005 $view_query_part = " unread = true AND ";
2010 if ($view_mode == "marked") {
2011 $view_query_part = " marked = true AND ";
2014 if ($view_mode == "unread") {
2015 $view_query_part = " unread = true AND ";
2019 $limit_query_part = "LIMIT " . $limit;
2022 $vfeed_query_part = "";
2024 // override query strategy and enable feed display when searching globally
2025 if ($search && $search_mode == "all_feeds") {
2026 $query_strategy_part = "ttrss_entries.id > 0";
2027 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2028 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2029 $query_strategy_part = "ttrss_entries.id > 0";
2030 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2031 id = feed_id) as feed_title,";
2032 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2034 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2036 $tmp_result = db_query($link, "SELECT id
2037 FROM ttrss_feeds WHERE cat_id =
2038 (SELECT cat_id FROM ttrss_feeds WHERE id = '$feed') AND id != '$feed'");
2040 $cat_siblings = array();
2042 if (db_num_rows($tmp_result) > 0) {
2043 while ($p = db_fetch_assoc($tmp_result)) {
2044 array_push($cat_siblings, "feed_id = " . $p["id"]);
2047 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2048 $feed, implode(" OR ", $cat_siblings));
2051 $query_strategy_part = "ttrss_entries.id > 0";
2054 } else if ($feed >= 0) {
2059 $query_strategy_part = "cat_id = '$feed'";
2061 $query_strategy_part = "cat_id IS NULL";
2064 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2067 $tmp_result = db_query($link, "SELECT id
2068 FROM ttrss_feeds WHERE parent_feed = '$feed'
2069 ORDER BY cat_id,title");
2071 $parent_ids = array();
2073 if (db_num_rows($tmp_result) > 0) {
2074 while ($p = db_fetch_assoc($tmp_result)) {
2075 array_push($parent_ids, "feed_id = " . $p["id"]);
2078 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2079 $feed, implode(" OR ", $parent_ids));
2081 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2083 $query_strategy_part = "feed_id = '$feed'";
2086 } else if ($feed == -1) { // starred virtual feed
2087 $query_strategy_part = "marked = true";
2088 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2089 } else if ($feed <= -10) { // labels
2090 $label_id = -$feed - 11;
2092 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2093 WHERE id = '$label_id'");
2095 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2097 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2099 $query_strategy_part = "id > 0"; // dumb
2102 $order_by = "updated DESC";
2106 if ($search && $search_mode == "all_feeds") {
2107 $feed_title = "Global search results ($search)";
2108 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2109 $feed_title = "Tag search results ($search, $feed)";
2110 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2111 $feed_title = $feed;
2112 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2117 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2118 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2119 $feed_title = db_fetch_result($result, 0, "title");
2121 $feed_title = "Uncategorized";
2125 $feed_title = "Category search results ($search, $feed_title)";
2130 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2131 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2133 $feed_title = db_fetch_result($result, 0, "title");
2134 $feed_site_url = db_fetch_result($result, 0, "site_url");
2135 $last_error = db_fetch_result($result, 0, "last_error");
2138 $feed_title = "Feed search results ($search, $feed_title)";
2142 } else if ($feed == -1) {
2143 $feed_title = "Starred articles";
2144 } else if ($feed < -10) {
2145 $label_id = -$feed - 11;
2146 $result = db_query($link, "SELECT description FROM ttrss_labels
2147 WHERE id = '$label_id'");
2148 $feed_title = db_fetch_result($result, 0, "description");
2153 $feed_title = db_unescape_string($feed_title);
2155 if ($feed < -10) error_reporting (0);
2157 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2160 $feed_kind = "Feeds";
2162 $feed_kind = "Labels";
2165 $content_query_part = "content as content_preview,";
2168 ttrss_entries.id,ttrss_entries.title,
2169 SUBSTRING(updated,1,16) as updated,
2170 unread,feed_id,marked,link,last_read,
2171 SUBSTRING(last_read,1,19) as last_read_noms,
2174 SUBSTRING(updated,1,19) as updated_noms
2176 ttrss_entries,ttrss_user_entries,ttrss_feeds
2178 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2179 ttrss_user_entries.ref_id = ttrss_entries.id AND
2180 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2183 $query_strategy_part ORDER BY $order_by
2186 $result = db_query($link, $query);
2188 if ($_GET["debug"]) print $query;
2193 $feed_kind = "Tags";
2195 $result = db_query($link, "SELECT
2196 ttrss_entries.id as id,title,
2197 SUBSTRING(updated,1,16) as updated,
2199 marked,link,last_read,
2200 SUBSTRING(last_read,1,19) as last_read_noms,
2203 SUBSTRING(updated,1,19) as updated_noms
2205 ttrss_entries,ttrss_user_entries,ttrss_tags
2207 ref_id = ttrss_entries.id AND
2208 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2209 post_int_id = int_id AND tag_name = '$feed' AND
2212 $query_strategy_part ORDER BY $order_by
2213 $limit_query_part");
2216 return array($result, $feed_title, $feed_site_url, $last_error);
2220 function generate_syndicated_feed($link, $feed, $is_cat,
2221 $search, $search_mode, $match_on) {
2223 $qfh_ret = queryFeedHeadlines($link, $feed,
2224 30, false, $is_cat, $search, $search_mode, $match_on);
2226 $result = $qfh_ret[0];
2227 $feed_title = htmlspecialchars($qfh_ret[1]);
2228 $feed_site_url = $qfh_ret[2];
2229 $last_error = $qfh_ret[3];
2231 print "<rss version=\"2.0\">
2233 <title>$feed_title</title>
2234 <link>$feed_site_url</link>
2235 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2237 while ($line = db_fetch_assoc($result)) {
2239 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2241 $rfc822_date = date('r', strtotime($line["updated"]));
2243 print "<pubDate>$rfc822_date</pubDate>";
2246 htmlspecialchars($line["title"]) . "</title>";
2248 print "<description>" .
2249 htmlspecialchars($line["content_preview"]) . "</description>";
2254 print "</channel></rss>";