]>
git.wh0rd.org - tt-rss.git/blob - backend.php
2 require_once "sessions.php";
3 require_once "modules/backend-rpc.php";
5 header("Cache-Control: no-cache, must-revalidate");
6 header("Cache-Control: no-cache, must-revalidate");
8 header("Pragma: no-cache");
11 /* if ($_GET["debug"]) {
12 define('DEFAULT_ERROR_LEVEL', E_ALL);
14 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
17 error_reporting(DEFAULT_ERROR_LEVEL); */
19 define('SCHEMA_VERSION', 13);
21 require_once "sanity_check.php";
22 require_once "config.php";
24 require_once "db.php";
25 require_once "db-prefs.php";
26 require_once "functions.php";
28 $script_started = getmicrotime();
30 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
33 if (DB_TYPE
== "mysql") {
36 // PG seems to display its own errors just fine by default.
40 if (DB_TYPE
== "pgsql") {
41 pg_query("set client_encoding = 'UTF-8'");
42 pg_set_client_encoding("UNICODE");
45 $op = $_REQUEST["op"];
47 $print_exec_time = false;
49 if ((!$op ||
$op == "rpc" ||
$op == "rss" ||
$op == "digestSend" ||
50 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
51 header("Content-Type: application/xml; charset=utf-8");
53 header("Content-Type: text/html; charset=utf-8");
57 header("Content-Type: application/xml");
58 print_error_xml(7); exit;
61 if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
62 && $op != "rss" && $op != "getUnread") {
65 print_error_xml(6); die;
69 <p>Error: Not logged in.</p>
70 <script type=\"text/javascript\">
71 if (parent.window != 'undefined') {
72 parent.window.location = \"tt-rss.php\";
74 window.location = \"tt-rss.php\";
83 $purge_intervals = array(
84 0 => _("Use default"),
85 -1 => _("Never purge"),
87 14 => _("2 weeks old"),
88 31 => _("1 month old"),
89 60 => _("2 months old"),
90 90 => _("3 months old"));
92 $update_intervals = array(
93 0 => _("Use default"),
94 -1 => _("Disable updates"),
95 30 => _("Each 30 minutes"),
97 240 => _("Each 4 hours"),
98 720 => _("Each 12 hours"),
100 10080 => _("Weekly"));
103 $access_level_names = array(
105 10 => _("Administrator"));
107 require_once "modules/pref-prefs.php";
108 require_once "modules/popup-dialog.php";
109 require_once "modules/help.php";
110 require_once "modules/pref-feeds.php";
111 require_once "modules/pref-filters.php";
112 require_once "modules/pref-labels.php";
113 require_once "modules/pref-users.php";
114 require_once "modules/pref-feed-browser.php";
117 if (!sanity_check($link)) { return; }
120 handle_rpc_request($link);
123 if ($op == "feeds") {
125 $tags = $_GET["tags"];
127 $subop = $_GET["subop"];
129 if ($subop == "catchupAll") {
130 db_query($link, "UPDATE ttrss_user_entries SET
131 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
134 if ($subop == "collapse") {
135 $cat_id = db_escape_string($_GET["cid"]);
137 db_query($link, "UPDATE ttrss_feed_categories SET
138 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " .
143 outputFeedList($link, $tags);
149 $id = db_escape_string($_GET["id"]);
150 $feed_id = db_escape_string($_GET["feed"]);
152 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
153 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
155 if (db_num_rows($result) == 1) {
156 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
158 $rtl_content = false;
162 $rtl_tag = "dir=\"RTL\"";
169 $result = db_query($link, "UPDATE ttrss_user_entries
170 SET unread = false,last_read = NOW()
171 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
173 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
174 SUBSTRING(updated,1,16) as updated,
175 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
178 FROM ttrss_entries,ttrss_user_entries
179 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
185 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
186 $link_target = "target=\"_new\"";
189 $line = db_fetch_assoc($result);
191 if ($line["icon_url"]) {
192 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
194 $feed_icon = " ";
197 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
198 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
200 $entry_comments = "";
203 $num_comments = $line["num_comments"];
204 $entry_comments = "";
206 if ($num_comments > 0) {
207 if ($line["comments"]) {
208 $comments_url = $line["comments"];
210 $comments_url = $line["link"];
212 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
214 if ($line["comments"] && $line["link"] != $line["comments"]) {
215 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
219 print "<div class=\"postReply\">";
221 print "<div class=\"postHeader\">";
223 $entry_author = $line["author"];
226 $entry_author = _(" - by ") . $entry_author;
229 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
230 strtotime($line["updated"]));
232 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
235 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
236 $line["title"] . "</a>$entry_author</div>";
238 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
241 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
242 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
250 while ($tmp_line = db_fetch_assoc($tmp_result)) {
252 $tag = $tmp_line["tag_name"];
253 $tag_str = "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
255 if ($num_tags == 6) {
256 $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
257 } else if ($num_tags < 6) {
258 $tags_str .= $tag_str;
260 $f_tags_str .= $tag_str;
263 $tags_str = preg_replace("/, $/", "", $tags_str);
264 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
266 if (!$entry_comments) $entry_comments = " "; # placeholder
268 if (!$tags_str) $tags_str = '<span class="tagList">'._('no tags').'</span>';
270 print "<div style='float : right'>$tags_str
271 <a title=\"Edit tags for this article\"
272 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
273 <div clear='both'>$entry_comments</div>";
277 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
278 print "<div class=\"postContent\">";
280 if (db_num_rows($tmp_result) > 0) {
281 print "<div id=\"allEntryTags\">"._('Tags:')."$f_tags_str</div>";
284 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
285 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
288 $line["content"] = sanitize_rss($line["content"]);
290 print $line["content"] . "</div>";
297 if ($op == "viewfeed") {
299 $feed = db_escape_string($_GET["feed"]);
300 $subop = db_escape_string($_GET["subop"]);
301 $view_mode = db_escape_string($_GET["view_mode"]);
302 $limit = db_escape_string($_GET["limit"]);
303 $cat_view = db_escape_string($_GET["cat"]);
304 $next_unread_feed = db_escape_string($_GET["nuf"]);
305 $offset = db_escape_string($_GET["skip"]);
307 if (!$offset) $offset = 0;
309 if ($subop == "undefined") $subop = "";
311 if ($subop == "CatchupSelected") {
312 $ids = split(",", db_escape_string($_GET["ids"]));
313 $cmode = sprintf("%d", $_GET["cmode"]);
315 catchupArticlesById($link, $ids, $cmode);
318 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
319 update_generic_feed($link, $feed, $cat_view);
322 if ($subop == "MarkAllRead") {
323 catchup_feed($link, $feed, $cat_view);
325 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
326 if ($next_unread_feed) {
327 $feed = $next_unread_feed;
333 $result = db_query($link,
334 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
336 if (db_num_rows($result) == 0) {
337 print "<div align='center'>"._('Feed not found.')."</div>";
342 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
344 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
345 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
347 if (db_num_rows($result) == 1) {
348 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
350 $rtl_content = false;
354 $rtl_tag = "dir=\"RTL\"";
360 $rtl_content = false;
363 $script_dt_add = get_script_dt_add();
365 /* print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
366 <script type=\"text/javascript\" src=\"prototype.js\"></script>
367 <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
368 <script type=\"text/javascript\" src=\"viewfeed.js?$script_dt_add\"></script>
369 <!--[if gte IE 5.5000]>
370 <script type=\"text/javascript\" src=\"pngfix.js\"></script>
371 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
373 </head><body $rtl_tag>
374 <script type=\"text/javascript\">
375 if (document.addEventListener) {
376 document.addEventListener(\"DOMContentLoaded\", init, null);
378 window.onload = init;
381 /// START /////////////////////////////////////////////////////////////////////////////////
383 $search = db_escape_string($_GET["query"]);
384 $search_mode = db_escape_string($_GET["search_mode"]);
385 $match_on = db_escape_string($_GET["match_on"]);
391 $real_offset = $offset * $limit;
393 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
394 $search, $search_mode, $match_on, false, $real_offset);
396 $result = $qfh_ret[0];
397 $feed_title = $qfh_ret[1];
398 $feed_site_url = $qfh_ret[2];
399 $last_error = $qfh_ret[3];
401 /// STOP //////////////////////////////////////////////////////////////////////////////////
403 print "<div id=\"headlinesContainer\" $rtl_tag>";
406 print "<div align='center'>"._("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
410 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
411 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
414 print "<div id=\"headlinesInnerContainer\">";
416 if (db_num_rows($result) > 0) {
418 # print "\{$offset}";
420 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
421 print "<table class=\"headlinesList\" id=\"headlinesList\"
422 cellspacing=\"0\" width=\"100%\">";
427 error_reporting (DEFAULT_ERROR_LEVEL
);
431 while ($line = db_fetch_assoc($result)) {
433 $class = ($lnum %
2) ?
"even" : "odd";
436 $feed_id = $line["feed_id"];
438 if ($line["last_read"] == "" &&
439 ($line["unread"] != "t" && $line["unread"] != "1")) {
441 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
444 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
448 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
456 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
457 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
459 alt=\"Reset mark\" onclick='javascript:toggleMark($id)'>";
461 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
463 alt=\"Set mark\" onclick='javascript:toggleMark($id)'>";
466 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
467 # $line["title"] . "</a>";
469 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
470 $line["title"] . "</a>";
472 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
473 # $line["title"] . "</a>";
475 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
476 $updated_fmt = smart_date_time(strtotime($line["updated"]));
478 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
479 $updated_fmt = date($short_date, strtotime($line["updated"]));
482 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
483 $content_preview = truncate_string(strip_tags($line["content_preview"]),
487 $entry_author = $line["author"];
490 $entry_author = " - by $entry_author";
493 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
495 print "<tr class='$class' id='RROW-$id'>";
497 print "<td class='hlUpdatePic'>$update_pic</td>";
499 print "<td class='hlSelectRow'>
500 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
501 class=\"feedCheckBox\" id=\"RCHK-$id\">
504 print "<td class='hlMarkedPic'>$marked_pic</td>";
506 if ($line["feed_title"]) {
507 print "<td class='hlContent'>$content_link</td>";
508 print "<td class='hlFeed'>
509 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
510 $line["feed_title"]."</a> </td>";
512 print "<td class='hlContent' valign='middle'>";
514 print "<a href=\"javascript:view($id,$feed_id);\">" .
517 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
518 if ($content_preview) {
519 print "<span class=\"contentPreview\"> - $content_preview</span>";
527 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
534 $add_class = "Unread";
539 print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
541 print "<div class=\"cdmHeader\">";
543 print "<div class=\"articleUpdated\">$updated_fmt</div>";
545 print "<a class=\"title\"
546 onclick=\"javascript:toggleUnread($id, 0)\"
547 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
551 if ($line["feed_title"]) {
552 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
557 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
559 print "<div class=\"cdmFooter\">";
563 print "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
564 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
566 $tags = get_article_tags($link, $id);
570 foreach ($tags as $tag) {
572 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
575 $tags_str = preg_replace("/, $/", "", $tags_str);
577 if ($tags_str == "") $tags_str = "no tags";
579 print " $tags_str <a title=\"Edit tags for this article\"
580 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
584 # print "<div align=\"center\"><a class=\"cdmToggleLink\"
585 # href=\"javascript:toggleUnread($id)\">
586 # Toggle unread</a></div>";
595 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
599 // print_headline_subtoolbar($link,
600 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
604 print "<div class='whiteBox'>"._('No articles found.')."</div>";
612 if ($op == "pref-feeds") {
613 module_pref_feeds($link);
616 if ($op == "pref-filters") {
617 module_pref_filters($link);
620 if ($op == "pref-labels") {
621 module_pref_labels($link);
624 if ($op == "pref-prefs") {
625 module_pref_prefs($link);
628 if ($op == "pref-users") {
629 module_pref_users($link);
637 module_popup_dialog($link);
640 // update feeds of all users, may be used anonymously
641 if ($op == "globalUpdateFeeds") {
643 $result = db_query($link, "SELECT id FROM ttrss_users");
645 while ($line = db_fetch_assoc($result)) {
646 $user_id = $line["id"];
647 // print "<!-- updating feeds of uid $user_id -->";
648 update_all_feeds($link, false, $user_id);
652 <message msg=\"All feeds updated\"/>
657 if ($op == "user-details") {
659 if (WEB_DEMO_MODE ||
$_SESSION["access_level"] < 10) {
663 /* print "<html><head>
664 <title>Tiny Tiny RSS : User Details</title>
665 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
666 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
669 $uid = sprintf("%d", $_GET["id"]);
671 print "<div id=\"infoBoxTitle\">User details</div>";
673 print "<div class='infoBoxContents'>";
675 $result = db_query($link, "SELECT login,
676 SUBSTRING(last_login,1,16) AS last_login,
678 (SELECT COUNT(int_id) FROM ttrss_user_entries
679 WHERE owner_uid = id) AS stored_articles
683 if (db_num_rows($result) == 0) {
684 print "<h1>User not found</h1>";
688 # print "<h1>User Details</h1>";
690 $login = db_fetch_result($result, 0, "login");
692 # print "<h1>$login</h1>";
694 print "<table width='100%'>";
696 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
697 strtotime(db_fetch_result($result, 0, "last_login")));
698 $access_level = db_fetch_result($result, 0, "access_level");
699 $stored_articles = db_fetch_result($result, 0, "stored_articles");
701 # print "<tr><td>Username</td><td>$login</td></tr>";
702 # print "<tr><td>Access level</td><td>$access_level</td></tr>";
703 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
704 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
706 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
707 WHERE owner_uid = '$uid'");
709 $num_feeds = db_fetch_result($result, 0, "num_feeds");
711 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
713 /* $result = db_query($link, "SELECT
714 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size
715 FROM ttrss_user_entries,ttrss_entries
716 WHERE owner_uid = '$uid' AND ref_id = id");
718 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
720 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>"; */
724 print "<h1>Subscribed feeds</h1>";
726 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
727 WHERE owner_uid = '$uid' ORDER BY title");
729 print "<ul class=\"userFeedList\">";
733 while ($line = db_fetch_assoc($result)) {
735 $icon_file = ICONS_URL
."/".$line["id"].".ico";
737 if (file_exists($icon_file) && filesize($icon_file) > 0) {
738 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
740 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
743 print "<li class=\"$row_class\">$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
745 $row_class = toggleEvenOdd($row_class);
749 if (db_num_rows($result) < $num_feeds) {
750 // FIXME - add link to show ALL subscribed feeds here somewhere
752 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\"> ...</li>";
759 print "<div align='center'>
760 <input type='submit' class='button'
761 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
763 // print "</body></html>";
767 if ($op == "pref-feed-browser") {
768 module_pref_feed_browser($link);
772 $feed = db_escape_string($_GET["id"]);
773 $user = db_escape_string($_GET["user"]);
774 $pass = db_escape_string($_GET["pass"]);
775 $is_cat = $_GET["is_cat"] != false;
777 $search = db_escape_string($_GET["q"]);
778 $match_on = db_escape_string($_GET["m"]);
779 $search_mode = db_escape_string($_GET["smode"]);
781 if (!$_SESSION["uid"] && $user && $pass) {
782 authenticate_user($link, $user, $pass);
785 if ($_SESSION["uid"] ||
786 http_authenticate_user($link)) {
788 generate_syndicated_feed($link, $feed, $is_cat,
789 $search, $search_mode, $match_on);
793 if ($op == "labelFromSearch") {
794 $search = db_escape_string($_GET["search"]);
795 $search_mode = db_escape_string($_GET["smode"]);
796 $match_on = db_escape_string($_GET["match"]);
797 $is_cat = db_escape_string($_GET["is_cat"]);
798 $title = db_escape_string($_GET["title"]);
799 $feed = sprintf("%d", $_GET["feed"]);
801 $label_qparts = array();
803 $search_expr = getSearchSql($search, $match_on);
807 $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
809 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
812 if ($search_mode == "all_feeds") {
814 } else if ($search_mode == "this_cat") {
816 $tmp_result = db_query($link, "SELECT cat_id
817 FROM ttrss_feeds WHERE id = '$feed'");
819 $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
822 $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
824 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
827 $search_expr .= " AND ttrss_feeds.id = $feed ";
832 $search_expr = db_escape_string($search_expr);
837 $result = db_query($link,
838 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid)
839 VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
843 if ($op == "getUnread") {
844 $login = db_escape_string($_GET["login"]);
846 header("Content-Type: text/plain; charset=utf-8");
848 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
850 if (db_num_rows($result) == 1) {
851 $uid = db_fetch_result($result, 0, "id");
852 print getGlobalUnread($link, $uid);
854 print "-1;User not found";
857 $print_exec_time = false;
860 if ($op == "digestTest") {
861 header("Content-Type: text/plain");
862 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
863 $print_exec_time = false;
867 if ($op == "digestSend") {
868 header("Content-Type: text/plain");
869 send_headlines_digests($link);
870 $print_exec_time = false;
877 <?php
if ($print_exec_time) { ?
>
878 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->