]>
git.wh0rd.org - tt-rss.git/blob - functions.php
a46061bda64bc10ebaf2235c0d73c9ee6ba0d882
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;
535 $entry_content = sanitize_rss($entry_content);
536 $entry_title = sanitize_rss($entry_title);
537 $entry_link = sanitize_rss($entry_link);
538 $entry_comments = sanitize_rss($entry_comments);
540 db_query($link, "BEGIN");
542 if (db_num_rows($result) == 0) {
544 // base post entry does not exist, create it
546 $result = db_query($link,
547 "INSERT INTO ttrss_entries
563 '$entry_timestamp_fmt',
572 // we keep encountering the entry in feeds, so we need to
573 // update date_entered column so that we don't get horrible
574 // dupes when the entry gets purged and reinserted again e.g.
575 // in the case of SLOW SLOW OMG SLOW updating feeds
577 $base_entry_id = db_fetch_result($result, 0, "id");
579 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
580 WHERE id = '$base_entry_id'");
583 // now it should exist, if not - bad luck then
585 $result = db_query($link, "SELECT
586 id,content_hash,no_orig_date,title,
587 substring(date_entered,1,19) as date_entered,
588 substring(updated,1,19) as updated,
592 WHERE guid = '$entry_guid'");
594 if (db_num_rows($result) == 1) {
596 // this will be used below in update handler
597 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
598 $orig_title = db_fetch_result($result, 0, "title");
599 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
600 $orig_date_entered = strtotime(db_fetch_result($result,
603 $ref_id = db_fetch_result($result, 0, "id");
605 // check for user post link to main table
607 // do we allow duplicate posts with same GUID in different feeds?
608 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
609 $dupcheck_qpart = "AND feed_id = '$feed'";
611 $dupcheck_qpart = "";
614 // error_reporting(0);
616 $filter_name = get_filter_name($entry_title, $entry_content,
617 $entry_link, $filters);
619 if ($filter_name == "filter") {
623 // error_reporting (DEFAULT_ERROR_LEVEL);
625 $result = db_query($link,
626 "SELECT ref_id FROM ttrss_user_entries WHERE
627 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
630 // okay it doesn't exist - create user entry
631 if (db_num_rows($result) == 0) {
633 if ($filter_name != 'catchup') {
635 $last_read_qpart = 'NULL';
638 $last_read_qpart = 'NOW()';
641 if ($filter_name == 'mark') {
647 $result = db_query($link,
648 "INSERT INTO ttrss_user_entries
649 (ref_id, owner_uid, feed_id, unread, last_read, marked)
650 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
651 $last_read_qpart, $marked)");
654 $post_needs_update = false;
656 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
657 ($content_hash != $orig_content_hash)) {
658 $post_needs_update = true;
661 if ($orig_title != $entry_title) {
662 $post_needs_update = true;
665 if ($orig_num_comments != $num_comments) {
666 $post_needs_update = true;
669 // this doesn't seem to be very reliable
671 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
672 // $post_needs_update = true;
675 // if post needs update, update it and mark all user entries
676 // linking to this post as updated
677 if ($post_needs_update) {
679 // print "<!-- post $orig_title needs update : $post_needs_update -->";
681 db_query($link, "UPDATE ttrss_entries
682 SET title = '$entry_title', content = '$entry_content',
683 num_comments = '$num_comments'
684 WHERE id = '$ref_id'");
686 if (get_pref($link, "MARK_UNREAD_ON_UPDATE")) {
687 db_query($link, "UPDATE ttrss_user_entries
688 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
690 db_query($link, "UPDATE ttrss_user_entries
691 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
697 db_query($link, "COMMIT");
700 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
704 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
705 $entry_content_unescaped, $entry_tags);
707 // print "<br>$entry_title : $entry_content_unescaped<br>";
708 // print_r($entry_tags);
711 $entry_tags = $entry_tags[1];
713 if (count($entry_tags) > 0) {
715 db_query($link, "BEGIN");
717 $result = db_query($link, "SELECT id,int_id
718 FROM ttrss_entries,ttrss_user_entries
719 WHERE guid = '$entry_guid'
720 AND feed_id = '$feed' AND ref_id = id
721 AND owner_uid = '$owner_uid'");
723 if (db_num_rows($result) == 1) {
725 $entry_id = db_fetch_result($result, 0, "id");
726 $entry_int_id = db_fetch_result($result, 0, "int_id");
728 foreach ($entry_tags as $tag) {
729 $tag = db_escape_string(strtolower($tag));
731 $tag = str_replace("+", " ", $tag);
732 $tag = str_replace("technorati tag: ", "", $tag);
734 $result = db_query($link, "SELECT id FROM ttrss_tags
735 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
736 owner_uid = '$owner_uid' LIMIT 1");
738 // print db_fetch_result($result, 0, "id");
740 if ($result && db_num_rows($result) == 0) {
742 // print "tagging $entry_id as $tag<br>";
744 db_query($link, "INSERT INTO ttrss_tags
745 (owner_uid,tag_name,post_int_id)
746 VALUES ('$owner_uid','$tag', '$entry_int_id')");
750 db_query($link, "COMMIT");
754 db_query($link, "UPDATE ttrss_feeds
755 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
757 // db_query($link, "COMMIT");
760 $error_msg = db_escape_string(magpie_error());
762 "UPDATE ttrss_feeds SET last_error = '$error_msg',
763 last_updated = NOW() WHERE id = '$feed'");
768 function print_select($id, $default, $values, $attributes = "") {
769 print "<select name=\"$id\" id=\"$id\" $attributes>";
770 foreach ($values as $v) {
776 print "<option$sel>$v</option>";
781 function print_select_hash($id, $default, $values, $attributes = "") {
782 print "<select name=\"$id\" id='$id' $attributes>";
783 foreach (array_keys($values) as $v) {
789 print "<option $sel value=\"$v\">".$values[$v]."</option>";
795 function get_filter_name($title, $content, $link, $filters) {
797 if ($filters["title"]) {
798 foreach ($filters["title"] as $filter) {
799 $reg_exp = $filter["reg_exp"];
800 if (preg_match("/$reg_exp/i", $title)) {
801 return $filter["action"];
806 if ($filters["content"]) {
807 foreach ($filters["content"] as $filter) {
808 $reg_exp = $filter["reg_exp"];
809 if (preg_match("/$reg_exp/i", $content)) {
810 return $filter["action"];
815 if ($filters["both"]) {
816 foreach ($filters["both"] as $filter) {
817 $reg_exp = $filter["reg_exp"];
818 if (preg_match("/$reg_exp/i", $title) ||
819 preg_match("/$reg_exp/i", $content)) {
820 return $filter["action"];
825 if ($filters["link"]) {
826 $reg_exp = $filter["reg_exp"];
827 foreach ($filters["link"] as $filter) {
828 $reg_exp = $filter["reg_exp"];
829 if (preg_match("/$reg_exp/i", $link)) {
830 return $filter["action"];
838 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
839 $rtl_content = false, $last_updated = false, $last_error = false) {
841 if (file_exists($icon_file) && filesize($icon_file) > 0) {
842 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
844 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
848 $rtl_tag = "dir=\"rtl\"";
850 $rtl_tag = "dir=\"ltr\"";
853 $error_notify_msg = "";
856 $link_title = "Error: $last_error ($last_updated)";
857 $error_notify_msg = "(Error)";
858 } else if ($last_updated) {
859 $link_title = "Updated: $last_updated";
862 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
864 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
865 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
869 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
874 $fctr_class = "class=\"invisible\"";
877 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
878 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
880 if (get_pref($link, "EXTENDED_FEEDLIST")) {
881 print "<div class=\"feedExtInfo\">
882 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
889 function getmicrotime() {
890 list($usec, $sec) = explode(" ",microtime());
891 return ((float)$usec +
(float)$sec);
894 function print_radio($id, $default, $values, $attributes = "") {
895 foreach ($values as $v) {
903 $sel .= " value=\"1\"";
905 $sel .= " value=\"0\"";
908 print "<input class=\"noborder\"
909 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
914 function initialize_user_prefs($link, $uid) {
916 $uid = db_escape_string($uid);
918 db_query($link, "BEGIN");
920 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
922 $u_result = db_query($link, "SELECT pref_name
923 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
925 $active_prefs = array();
927 while ($line = db_fetch_assoc($u_result)) {
928 array_push($active_prefs, $line["pref_name"]);
931 while ($line = db_fetch_assoc($result)) {
932 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
933 // print "adding " . $line["pref_name"] . "<br>";
935 db_query($link, "INSERT INTO ttrss_user_prefs
936 (owner_uid,pref_name,value) VALUES
937 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
942 db_query($link, "COMMIT");
946 function lookup_user_id($link, $user) {
948 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
951 if (db_num_rows($result) == 1) {
952 return db_fetch_result($result, 0, "id");
958 function http_authenticate_user($link) {
960 if (!$_SERVER["PHP_AUTH_USER"]) {
962 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
963 header('HTTP/1.0 401 Unauthorized');
967 $auth_result = authenticate_user($link,
968 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
971 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
972 header('HTTP/1.0 401 Unauthorized');
980 function authenticate_user($link, $login, $password) {
982 if (!SINGLE_USER_MODE
) {
984 $pwd_hash = 'SHA1:' . sha1($password);
986 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
987 login = '$login' AND pwd_hash = '$pwd_hash'");
989 if (db_num_rows($result) == 1) {
990 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
991 $_SESSION["name"] = db_fetch_result($result, 0, "login");
992 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
994 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
997 $user_theme = get_user_theme_path($link);
999 $_SESSION["theme"] = $user_theme;
1000 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1002 initialize_user_prefs($link, $_SESSION["uid"]);
1011 $_SESSION["uid"] = 1;
1012 $_SESSION["name"] = "admin";
1014 $user_theme = get_user_theme_path($link);
1016 $_SESSION["theme"] = $user_theme;
1017 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1019 initialize_user_prefs($link, $_SESSION["uid"]);
1025 function make_password($length = 8) {
1028 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1032 while ($i < $length) {
1033 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1035 if (!strstr($password, $char)) {
1043 // this is called after user is created to initialize default feeds, labels
1046 // user preferences are checked on every login, not here
1048 function initialize_user($link, $uid) {
1050 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1051 values ('$uid','unread = true', 'Unread articles')");
1053 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1054 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1056 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1057 values ('$uid', 'Tiny Tiny RSS: New Releases',
1058 'http://tt-rss.spb.ru/releases.rss')");
1062 function logout_user() {
1064 if (isset($_COOKIE[session_name()])) {
1065 setcookie(session_name(), '', time()-42000, '/');
1069 function get_script_urlpath() {
1070 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1073 function get_login_redirect() {
1074 $server = $_SERVER["SERVER_NAME"];
1076 if (ENABLE_LOGIN_SSL
) {
1077 $protocol = "https";
1082 $url_path = get_script_urlpath();
1084 $redirect_uri = "$protocol://$server$url_path/login.php";
1086 return $redirect_uri;
1089 function validate_session($link) {
1090 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
1091 if ($_SESSION["ip_address"]) {
1092 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1100 function basic_nosid_redirect_check() {
1101 if (!SINGLE_USER_MODE
) {
1102 if (!$_COOKIE[get_session_cookie_name()]) {
1103 $redirect_uri = get_login_redirect();
1104 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1105 header("Location: $redirect_uri?rt=$return_to");
1111 function login_sequence($link) {
1112 if (!SINGLE_USER_MODE
) {
1114 if (!validate_session($link)) {
1116 $redirect_uri = get_login_redirect();
1117 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1118 header("Location: $redirect_uri?rt=$return_to");
1122 if (!USE_HTTP_AUTH
) {
1123 if (!$_SESSION["uid"]) {
1124 $redirect_uri = get_login_redirect();
1125 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1126 header("Location: $redirect_uri?rt=$return_to");
1130 if (!$_SESSION["uid"]) {
1131 if (!$_SERVER["PHP_AUTH_USER"]) {
1133 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1134 header('HTTP/1.0 401 Unauthorized');
1138 $auth_result = authenticate_user($link,
1139 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1141 if (!$auth_result) {
1142 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1143 header('HTTP/1.0 401 Unauthorized');
1150 return authenticate_user($link, "admin", null);
1154 function truncate_string($str, $max_len) {
1155 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1156 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1162 function get_user_theme_path($link) {
1163 $result = db_query($link, "SELECT theme_path
1165 ttrss_themes,ttrss_users
1166 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1167 if (db_num_rows($result) != 0) {
1168 return db_fetch_result($result, 0, "theme_path");
1174 function smart_date_time($timestamp) {
1175 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1176 return date("G:i", $timestamp);
1177 } else if (date("Y", $timestamp) == date("Y")) {
1178 return date("M d, G:i", $timestamp);
1180 return date("Y/m/d G:i", $timestamp);
1184 function smart_date($timestamp) {
1185 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1187 } else if (date("Y", $timestamp) == date("Y")) {
1188 return date("D m", $timestamp);
1190 return date("Y/m/d", $timestamp);
1194 function sql_bool_to_string($s) {
1195 if ($s == "t" ||
$s == "1") {
1202 function sql_bool_to_bool($s) {
1203 if ($s == "t" ||
$s == "1") {
1211 function toggleEvenOdd($a) {
1218 function sanity_check($link) {
1223 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1224 $schema_version = db_fetch_result($result, 0, "schema_version");
1226 if ($schema_version != SCHEMA_VERSION
) {
1230 if (DB_TYPE
== "mysql") {
1231 $result = db_query($link, "SELECT true", false);
1232 if (db_num_rows($result) != 1) {
1237 error_reporting (DEFAULT_ERROR_LEVEL
);
1239 if ($error_code != 0) {
1240 print_error_xml($error_code);
1247 function file_is_locked($filename) {
1249 $fp = fopen($filename, "r");
1250 error_reporting(DEFAULT_ERROR_LEVEL
);
1252 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1253 flock($fp, LOCK_UN
);
1263 function make_lockfile($filename) {
1264 $fp = fopen($filename, "w");
1266 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1273 function sql_random_function() {
1274 if (DB_TYPE
== "mysql") {
1281 function catchup_feed($link, $feed, $cat_view) {
1283 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1288 $cat_qpart = "cat_id = '$feed'";
1290 $cat_qpart = "cat_id IS NULL";
1293 $tmp_result = db_query($link, "SELECT id
1294 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1297 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1299 $tmp_feed = $tmp_line["id"];
1301 db_query($link, "UPDATE ttrss_user_entries
1302 SET unread = false,last_read = NOW()
1303 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1306 } else if ($feed > 0) {
1308 $tmp_result = db_query($link, "SELECT id
1309 FROM ttrss_feeds WHERE parent_feed = '$feed'
1310 ORDER BY cat_id,title");
1312 $parent_ids = array();
1314 if (db_num_rows($tmp_result) > 0) {
1315 while ($p = db_fetch_assoc($tmp_result)) {
1316 array_push($parent_ids, "feed_id = " . $p["id"]);
1319 $children_qpart = implode(" OR ", $parent_ids);
1321 db_query($link, "UPDATE ttrss_user_entries
1322 SET unread = false,last_read = NOW()
1323 WHERE (feed_id = '$feed' OR $children_qpart)
1324 AND owner_uid = " . $_SESSION["uid"]);
1327 db_query($link, "UPDATE ttrss_user_entries
1328 SET unread = false,last_read = NOW()
1329 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1332 } else if ($feed < 0 && $feed > -10) { // special, like starred
1335 db_query($link, "UPDATE ttrss_user_entries
1336 SET unread = false,last_read = NOW()
1337 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1340 } else if ($feed < -10) { // label
1342 // TODO make this more efficient
1344 $label_id = -$feed - 11;
1346 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1347 WHERE id = '$label_id'");
1350 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1352 db_query($link, "BEGIN");
1354 $tmp2_result = db_query($link,
1358 ttrss_user_entries,ttrss_entries,ttrss_feeds
1360 ref_id = ttrss_entries.id AND
1361 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1363 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1365 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1366 db_query($link, "UPDATE
1369 unread = false, last_read = NOW()
1371 int_id = " . $tmp_line["int_id"]);
1374 db_query($link, "COMMIT");
1376 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1377 SET unread = false,last_read = NOW()
1380 AND owner_uid = ".$_SESSION["uid"]); */
1384 db_query($link, "BEGIN");
1386 $tag_name = db_escape_string($feed);
1388 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1389 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1391 while ($line = db_fetch_assoc($result)) {
1392 db_query($link, "UPDATE ttrss_user_entries SET
1393 unread = false, last_read = NOW()
1394 WHERE int_id = " . $line["post_int_id"]);
1396 db_query($link, "COMMIT");
1400 function update_generic_feed($link, $feed, $cat_view) {
1404 $cat_qpart = "cat_id = '$feed'";
1406 $cat_qpart = "cat_id IS NULL";
1409 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1410 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1412 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1413 $feed_url = $tmp_line["feed_url"];
1414 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1418 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1419 WHERE id = '$feed'");
1420 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1421 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1425 function getAllCounters($link) {
1426 getLabelCounters($link);
1427 getFeedCounters($link);
1428 getTagCounters($link);
1429 getGlobalCounters($link);
1430 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1431 getCategoryCounters($link);
1435 function getCategoryCounters($link) {
1436 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1437 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1438 AND unread = true)) AS unread FROM ttrss_feeds
1440 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1442 while ($line = db_fetch_assoc($result)) {
1443 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1444 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1445 $line["unread"]."\"/>";
1449 function getCategoryUnread($link, $cat) {
1452 $cat_query = "cat_id = '$cat'";
1454 $cat_query = "cat_id IS NULL";
1457 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1458 AND owner_uid = " . $_SESSION["uid"]);
1460 $cat_feeds = array();
1461 while ($line = db_fetch_assoc($result)) {
1462 array_push($cat_feeds, "feed_id = " . $line["id"]);
1465 if (count($cat_feeds) == 0) return 0;
1467 $match_part = implode(" OR ", $cat_feeds);
1469 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1470 FROM ttrss_user_entries
1471 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1475 # this needs to be rewritten
1476 while ($line = db_fetch_assoc($result)) {
1477 $unread +
= $line["unread"];
1484 function getFeedUnread($link, $feed, $is_cat = false) {
1485 $n_feed = sprintf("%d", $feed);
1488 return getCategoryUnread($link, $n_feed);
1489 } else if ($n_feed == -1) {
1490 $match_part = "marked = true";
1491 } else if ($n_feed > 0) {
1493 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1494 AND owner_uid = " . $_SESSION["uid"]);
1496 if (db_num_rows($result) > 0) {
1498 $linked_feeds = array();
1499 while ($line = db_fetch_assoc($result)) {
1500 array_push($linked_feeds, "feed_id = " . $line["id"]);
1503 $match_part = implode(" OR ", $linked_feeds);
1505 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1506 FROM ttrss_user_entries
1507 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1511 # this needs to be rewritten
1512 while ($line = db_fetch_assoc($result)) {
1513 $unread +
= $line["unread"];
1519 $match_part = "feed_id = '$n_feed'";
1521 } else if ($feed < -10) {
1522 $label_id = -$feed - 11;
1524 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1525 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1527 $match_part = db_fetch_result($result, 0, "sql_exp");
1532 $result = db_query($link, "SELECT count(int_id) AS unread
1533 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1534 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1535 ttrss_user_entries.ref_id = ttrss_entries.id AND
1536 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1540 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1541 FROM ttrss_tags,ttrss_user_entries
1542 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1543 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1546 $unread = db_fetch_result($result, 0, "unread");
1550 /* FIXME this needs reworking */
1552 function getGlobalUnread($link) {
1553 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1554 WHERE unread = true AND
1555 ttrss_user_entries.ref_id = ttrss_entries.id AND
1556 owner_uid = " . $_SESSION["uid"]);
1557 $c_id = db_fetch_result($result, 0, "c_id");
1561 function getGlobalCounters($link, $global_unread = -1) {
1562 if ($global_unread == -1) {
1563 $global_unread = getGlobalUnread($link);
1565 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1568 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1571 if (!$_SESSION["tctr_last_value"]) {
1572 $_SESSION["tctr_last_value"] = array();
1576 $old_counters = $_SESSION["tctr_last_value"];
1578 $tctrs_modified = false;
1580 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1581 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1582 ttrss_user_entries.ref_id = ttrss_entries.id AND
1583 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1584 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1586 select tag_name,0 as count FROM ttrss_tags
1587 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1589 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1590 FROM ttrss_user_entries WHERE int_id = post_int_id
1591 AND unread = true)) AS count FROM ttrss_tags
1592 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1596 while ($line = db_fetch_assoc($result)) {
1597 $tags[$line["tag_name"]] +
= $line["count"];
1600 foreach (array_keys($tags) as $tag) {
1601 $unread = $tags[$tag];
1603 $tag = htmlspecialchars($tag);
1605 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1606 $old_counters[$tag] = $unread;
1607 $tctrs_modified = true;
1608 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1613 if ($smart_mode && $tctrs_modified) {
1614 $_SESSION["tctr_last_value"] = $old_counters;
1619 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
, $ret_mode = false) {
1622 if (!$_SESSION["lctr_last_value"]) {
1623 $_SESSION["lctr_last_value"] = array();
1629 $old_counters = $_SESSION["lctr_last_value"];
1630 $lctrs_modified = false;
1632 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1633 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1634 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1635 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1637 $count = db_fetch_result($result, 0, "count");
1640 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1642 $ret_arr["-1"]["counter"] = $count;
1643 $ret_arr["-1"]["description"] = "Starred";
1646 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1647 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1649 while ($line = db_fetch_assoc($result)) {
1651 $id = -$line["id"] - 11;
1653 $label_name = $line["description"];
1655 error_reporting (0);
1657 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1658 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1659 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1660 ttrss_user_entries.ref_id = ttrss_entries.id AND
1661 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1663 $count = db_fetch_result($tmp_result, 0, "count");
1665 if (!$smart_mode ||
$old_counters[$id] != $count) {
1666 $old_counters[$id] = $count;
1667 $lctrs_modified = true;
1669 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1671 $ret_arr[$id]["counter"] = $count;
1672 $ret_arr[$id]["description"] = $label_name;
1676 error_reporting (DEFAULT_ERROR_LEVEL
);
1679 if ($smart_mode && $lctrs_modified) {
1680 $_SESSION["lctr_last_value"] = $old_counters;
1686 /* function getFeedCounter($link, $id) {
1688 $result = db_query($link, "SELECT
1689 count(id) as count,last_error
1690 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1691 WHERE feed_id = '$id' AND unread = true
1692 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1693 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1695 $count = db_fetch_result($result, 0, "count");
1696 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1698 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1701 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1704 if (!$_SESSION["fctr_last_value"]) {
1705 $_SESSION["fctr_last_value"] = array();
1709 $old_counters = $_SESSION["fctr_last_value"];
1711 $result = db_query($link, "SELECT id,last_error,parent_feed,
1712 SUBSTRING(last_updated,1,19) AS last_updated,
1714 FROM ttrss_entries,ttrss_user_entries
1715 WHERE feed_id = ttrss_feeds.id AND
1716 ttrss_user_entries.ref_id = ttrss_entries.id
1717 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1718 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1719 AND parent_feed IS NULL");
1721 $fctrs_modified = false;
1723 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1725 while ($line = db_fetch_assoc($result)) {
1728 $count = $line["count"];
1729 $last_error = htmlspecialchars($line["last_error"]);
1731 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1732 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1734 $last_updated = date($short_date, strtotime($line["last_updated"]));
1737 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1739 $tmp_result = db_query($link,
1740 "SELECT id,COUNT(unread) AS unread
1741 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1742 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1743 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1745 if (db_num_rows($tmp_result) > 0) {
1746 while ($l = db_fetch_assoc($tmp_result)) {
1747 $count +
= $l["unread"];
1751 if (!$smart_mode ||
$old_counters[$id] != $count) {
1752 $old_counters[$id] = $count;
1753 $fctrs_modified = true;
1756 $error_part = "error=\"$last_error\"";
1762 $has_img_part = "hi=\"$has_img\"";
1767 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1771 if ($smart_mode && $fctrs_modified) {
1772 $_SESSION["fctr_last_value"] = $old_counters;
1776 function get_script_dt_add() {
1777 if (strpos(VERSION
, "99") === false) {
1784 function get_pgsql_version($link) {
1785 $result = db_query($link, "SELECT version() AS version");
1786 $version = split(" ", db_fetch_result($result, 0, "version"));
1790 function print_error_xml($code, $add_msg = "") {
1793 $error_msg = $ERRORS[$code];
1796 $error_msg = "$error_msg; $add_msg";
1799 print "<rpc-reply>";
1800 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1801 print "</rpc-reply>";
1804 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1806 if ($cat_id == "0" ||
!$cat_id) {
1807 $cat_qpart = "NULL";
1809 $cat_qpart = "'$cat_id'";
1812 $result = db_query($link,
1813 "SELECT id FROM ttrss_feeds
1814 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1816 if (db_num_rows($result) == 0) {
1818 $result = db_query($link,
1819 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1820 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1821 '[Unknown]', $cat_qpart)");
1823 $result = db_query($link,
1824 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1825 AND owner_uid = " . $_SESSION["uid"]);
1827 $feed_id = db_fetch_result($result, 0, "id");
1830 update_rss_feed($link, $feed_link, $feed_id, true);
1839 function print_feed_select($link, $id, $default_id = "",
1840 $attributes = "", $include_all_feeds = true) {
1842 print "<select id=\"$id\" name=\"$id\" $attributes>";
1843 if ($include_all_feeds) {
1844 print "<option value=\"0\">All feeds</option>";
1847 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1848 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1850 if (db_num_rows($result) > 0 && $include_all_feeds) {
1851 print "<option disabled>--------</option>";
1854 while ($line = db_fetch_assoc($result)) {
1855 if ($line["id"] == $default_id) {
1856 $is_selected = "selected";
1860 printf("<option $is_selected value='%d'>%s</option>",
1861 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1867 function print_feed_cat_select($link, $id, $default_id = "",
1868 $attributes = "", $include_all_cats = true) {
1870 print "<select id=\"$id\" name=\"$id\" $attributes>";
1872 if ($include_all_cats) {
1873 print "<option value=\"0\">Uncategorized</option>";
1876 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1877 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1879 if (db_num_rows($result) > 0 && $include_all_cats) {
1880 print "<option disabled>--------</option>";
1883 while ($line = db_fetch_assoc($result)) {
1884 if ($line["id"] == $default_id) {
1885 $is_selected = "selected";
1889 printf("<option $is_selected value='%d'>%s</option>",
1890 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1896 function checkbox_to_sql_bool($val) {
1897 return ($val == "on") ?
"true" : "false";
1900 function getFeedCatTitle($link, $id) {
1903 } else if ($id < -10) {
1905 } else if ($id > 0) {
1906 $result = db_query($link, "SELECT ttrss_feed_categories.title
1907 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1908 cat_id = ttrss_feed_categories.id");
1909 if (db_num_rows($result) == 1) {
1910 return db_fetch_result($result, 0, "title");
1912 return "Uncategorized";
1915 return "getFeedCatTitle($id) failed";
1920 function getFeedTitle($link, $id) {
1922 return "Starred articles";
1923 } else if ($id < -10) {
1924 $label_id = -10 - $id;
1925 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1926 if (db_num_rows($result) == 1) {
1927 return db_fetch_result($result, 0, "description");
1929 return "Unknown label ($label_id)";
1932 } else if ($id > 0) {
1933 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1934 if (db_num_rows($result) == 1) {
1935 return db_fetch_result($result, 0, "title");
1937 return "Unknown feed ($id)";
1940 return "getFeedTitle($id) failed";
1945 function get_session_cookie_name() {
1946 return ((!defined('TTRSS_SESSION_NAME')) ?
"ttrss_sid" : TTRSS_SESSION_NAME
);
1949 function print_init_params($link) {
1950 print "<init-params>";
1951 if ($_SESSION["stored-params"]) {
1952 foreach (array_keys($_SESSION["stored-params"]) as $key) {
1954 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
1955 print "<param key=\"$key\" value=\"$value\"/>";
1960 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON
. "\"/>";
1961 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH
. "\"/>";
1962 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY
. "\"/>";
1964 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
1965 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
1967 print "<param key=\"hide_read_feeds\" value=\"" .
1968 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
1970 print "<param key=\"feeds_sort_by_unread\" value=\"" .
1971 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
1973 print "</init-params>";
1976 function print_runtime_info($link) {
1977 print "<runtime-info>";
1978 if (ENABLE_UPDATE_DAEMON
) {
1979 print "<param key=\"daemon_is_running\" value=\"".
1980 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
1982 print "</runtime-info>";
1985 function getSearchSql($search, $match_on) {
1987 $search_query_part = "";
1989 $keywords = split(" ", $search);
1990 $query_keywords = array();
1992 if ($match_on == "both") {
1994 foreach ($keywords as $k) {
1995 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
1996 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
1999 $search_query_part = implode("AND", $query_keywords) . " AND ";
2001 } else if ($match_on == "title") {
2003 foreach ($keywords as $k) {
2004 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2007 $search_query_part = implode("AND", $query_keywords) . " AND ";
2009 } else if ($match_on == "content") {
2011 foreach ($keywords as $k) {
2012 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2016 $search_query_part = implode("AND", $query_keywords);
2018 return $search_query_part;
2021 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2024 $search_query_part = getSearchSql($search, $match_on);
2025 $search_query_part .= " AND ";
2028 $search_query_part = "";
2031 $view_query_part = "";
2033 if ($view_mode == "adaptive") {
2035 $view_query_part = " ";
2036 } else if ($feed != -1) {
2037 $unread = getFeedUnread($link, $feed, $cat_view);
2039 $view_query_part = " unread = true AND ";
2044 if ($view_mode == "marked") {
2045 $view_query_part = " marked = true AND ";
2048 if ($view_mode == "unread") {
2049 $view_query_part = " unread = true AND ";
2053 $limit_query_part = "LIMIT " . $limit;
2056 $vfeed_query_part = "";
2058 // override query strategy and enable feed display when searching globally
2059 if ($search && $search_mode == "all_feeds") {
2060 $query_strategy_part = "ttrss_entries.id > 0";
2061 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2062 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2063 $query_strategy_part = "ttrss_entries.id > 0";
2064 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2065 id = feed_id) as feed_title,";
2066 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2068 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2070 $tmp_result = false;
2073 $tmp_result = db_query($link, "SELECT id
2074 FROM ttrss_feeds WHERE cat_id = '$feed'");
2076 $tmp_result = db_query($link, "SELECT id
2077 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2078 WHERE id = '$feed') AND id != '$feed'");
2081 $cat_siblings = array();
2083 if (db_num_rows($tmp_result) > 0) {
2084 while ($p = db_fetch_assoc($tmp_result)) {
2085 array_push($cat_siblings, "feed_id = " . $p["id"]);
2088 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2089 $feed, implode(" OR ", $cat_siblings));
2092 $query_strategy_part = "ttrss_entries.id > 0";
2095 } else if ($feed >= 0) {
2100 $query_strategy_part = "cat_id = '$feed'";
2102 $query_strategy_part = "cat_id IS NULL";
2105 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2108 $tmp_result = db_query($link, "SELECT id
2109 FROM ttrss_feeds WHERE parent_feed = '$feed'
2110 ORDER BY cat_id,title");
2112 $parent_ids = array();
2114 if (db_num_rows($tmp_result) > 0) {
2115 while ($p = db_fetch_assoc($tmp_result)) {
2116 array_push($parent_ids, "feed_id = " . $p["id"]);
2119 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2120 $feed, implode(" OR ", $parent_ids));
2122 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2124 $query_strategy_part = "feed_id = '$feed'";
2127 } else if ($feed == -1) { // starred virtual feed
2128 $query_strategy_part = "marked = true";
2129 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2130 } else if ($feed <= -10) { // labels
2131 $label_id = -$feed - 11;
2133 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2134 WHERE id = '$label_id'");
2136 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2138 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2140 $query_strategy_part = "id > 0"; // dumb
2143 if (get_pref($link, 'REVERSE_HEADLINES')) {
2144 $order_by = "updated";
2146 $order_by = "updated DESC";
2149 if ($override_order) {
2150 $order_by = $override_order;
2155 if ($search && $search_mode == "all_feeds") {
2156 $feed_title = "Global search results ($search)";
2157 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2158 $feed_title = "Tag search results ($search, $feed)";
2159 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2160 $feed_title = $feed;
2161 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2166 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2167 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2168 $feed_title = db_fetch_result($result, 0, "title");
2170 $feed_title = "Uncategorized";
2174 $feed_title = "Category search results ($search, $feed_title)";
2179 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2180 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2182 $feed_title = db_fetch_result($result, 0, "title");
2183 $feed_site_url = db_fetch_result($result, 0, "site_url");
2184 $last_error = db_fetch_result($result, 0, "last_error");
2187 $feed_title = "Feed search results ($search, $feed_title)";
2191 } else if ($feed == -1) {
2192 $feed_title = "Starred articles";
2193 } else if ($feed < -10) {
2194 $label_id = -$feed - 11;
2195 $result = db_query($link, "SELECT description FROM ttrss_labels
2196 WHERE id = '$label_id'");
2197 $feed_title = db_fetch_result($result, 0, "description");
2200 $feed_title = "Label search results ($search, $feed_title)";
2206 $feed_title = db_unescape_string($feed_title);
2208 if ($feed < -10) error_reporting (0);
2210 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2213 $feed_kind = "Feeds";
2215 $feed_kind = "Labels";
2218 $content_query_part = "content as content_preview,";
2221 ttrss_entries.id,ttrss_entries.title,
2222 SUBSTRING(updated,1,16) as updated,
2223 unread,feed_id,marked,link,last_read,
2224 SUBSTRING(last_read,1,19) as last_read_noms,
2227 SUBSTRING(updated,1,19) as updated_noms
2229 ttrss_entries,ttrss_user_entries,ttrss_feeds
2231 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2232 ttrss_user_entries.ref_id = ttrss_entries.id AND
2233 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2236 $query_strategy_part ORDER BY $order_by
2239 $result = db_query($link, $query);
2241 if ($_GET["debug"]) print $query;
2246 $feed_kind = "Tags";
2248 $result = db_query($link, "SELECT
2249 ttrss_entries.id as id,title,
2250 SUBSTRING(updated,1,16) as updated,
2252 marked,link,last_read,
2253 SUBSTRING(last_read,1,19) as last_read_noms,
2256 SUBSTRING(updated,1,19) as updated_noms
2258 ttrss_entries,ttrss_user_entries,ttrss_tags
2260 ref_id = ttrss_entries.id AND
2261 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2262 post_int_id = int_id AND tag_name = '$feed' AND
2265 $query_strategy_part ORDER BY $order_by
2266 $limit_query_part");
2269 return array($result, $feed_title, $feed_site_url, $last_error);
2273 function generate_syndicated_feed($link, $feed, $is_cat,
2274 $search, $search_mode, $match_on) {
2276 $qfh_ret = queryFeedHeadlines($link, $feed,
2277 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2279 $result = $qfh_ret[0];
2280 $feed_title = htmlspecialchars($qfh_ret[1]);
2281 $feed_site_url = $qfh_ret[2];
2282 $last_error = $qfh_ret[3];
2284 print "<rss version=\"2.0\">
2286 <title>$feed_title</title>
2287 <link>$feed_site_url</link>
2288 <generator>Tiny Tiny RSS v".VERSION
."</generator>";
2290 while ($line = db_fetch_assoc($result)) {
2292 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2294 $rfc822_date = date('r', strtotime($line["updated"]));
2296 print "<pubDate>$rfc822_date</pubDate>";
2299 htmlspecialchars($line["title"]) . "</title>";
2301 print "<description>" .
2302 htmlspecialchars($line["content_preview"]) . "</description>";
2307 print "</channel></rss>";
2311 function getCategoryTitle($link, $cat_id) {
2313 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2316 if (db_num_rows($result) == 1) {
2317 return db_fetch_result($result, 0, "title");
2319 return "Uncategorized";
2323 function sanitize_rss($str) {
2326 $res = preg_replace('/<script.*?>/i',
2327 "<p class=\"scriptWarn\">", $str);
2329 $res = preg_replace('/<\/script>/i',