]>
git.wh0rd.org - tt-rss.git/blob - backend.php
34c2781d6bb352ebbb20c80e7ad4b262dc92a5b0
2 require_once "sessions.php";
3 require_once "modules/backend-rpc.php";
5 header("Cache-Control: no-cache, must-revalidate");
6 header("Pragma: no-cache");
9 /* if ($_GET["debug"]) {
10 define('DEFAULT_ERROR_LEVEL', E_ALL);
12 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
15 error_reporting(DEFAULT_ERROR_LEVEL); */
17 $op = $_REQUEST["op"];
19 define('SCHEMA_VERSION', 12);
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 $print_exec_time = false;
30 if ((!$op ||
$op == "rpc" ||
$op == "rss" ||
$op == "digestSend" ||
31 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
32 header("Content-Type: application/xml");
36 header("Content-Type: application/xml");
37 print_error_xml(7); exit;
40 if (!$_SESSION["uid"] && $op != "globalUpdateFeeds" && $op != "rss" && $op != "getUnread") {
43 print_error_xml(6); die;
47 <p>Error: Not logged in.</p>
48 <script type=\"text/javascript\">
49 if (parent.window != 'undefined') {
50 parent.window.location = \"login.php\";
52 window.location = \"login.php\";
61 $purge_intervals = array(
62 0 => _("Use default"),
63 -1 => _("Never purge"),
65 14 => _("2 weeks old"),
66 31 => _("1 month old"),
67 60 => _("2 months old"),
68 90 => _("3 months old"));
70 $update_intervals = array(
71 0 => _("Use default"),
72 -1 => _("Disable updates"),
73 30 => _("Each 30 minutes"),
75 240 => _("Each 4 hours"),
76 720 => _("Each 12 hours"),
81 $access_level_names = array(
83 10 => _("Administrator"));
85 require_once "modules/pref-prefs.php";
86 require_once "modules/popup-dialog.php";
87 require_once "modules/help.php";
88 require_once "modules/pref-feeds.php";
89 require_once "modules/pref-filters.php";
90 require_once "modules/pref-labels.php";
91 require_once "modules/pref-users.php";
92 require_once "modules/pref-feed-browser.php";
94 $script_started = getmicrotime();
96 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
99 if (DB_TYPE
== "mysql") {
102 // PG seems to display its own errors just fine by default.
106 if (DB_TYPE
== "pgsql") {
107 pg_query("set client_encoding = 'utf-8'");
110 if (!sanity_check($link)) { return; }
113 handle_rpc_request($link);
116 if ($op == "feeds") {
118 $tags = $_GET["tags"];
120 $subop = $_GET["subop"];
122 if ($subop == "catchupAll") {
123 db_query($link, "UPDATE ttrss_user_entries SET
124 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
127 if ($subop == "collapse") {
128 $cat_id = db_escape_string($_GET["cid"]);
130 db_query($link, "UPDATE ttrss_feed_categories SET
131 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " .
136 outputFeedList($link, $tags);
142 $id = db_escape_string($_GET["id"]);
143 $feed_id = db_escape_string($_GET["feed"]);
145 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
146 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
148 if (db_num_rows($result) == 1) {
149 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
151 $rtl_content = false;
155 $rtl_tag = "dir=\"RTL\"";
162 $result = db_query($link, "UPDATE ttrss_user_entries
163 SET unread = false,last_read = NOW()
164 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
166 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
167 SUBSTRING(updated,1,16) as updated,
168 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
171 FROM ttrss_entries,ttrss_user_entries
172 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
178 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
179 $link_target = "target=\"_new\"";
182 $line = db_fetch_assoc($result);
184 if ($line["icon_url"]) {
185 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
187 $feed_icon = " ";
190 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
191 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
193 $entry_comments = "";
196 $num_comments = $line["num_comments"];
197 $entry_comments = "";
199 if ($num_comments > 0) {
200 if ($line["comments"]) {
201 $comments_url = $line["comments"];
203 $comments_url = $line["link"];
205 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
207 if ($line["comments"] && $line["link"] != $line["comments"]) {
208 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
212 print "<div class=\"postReply\">";
214 print "<div class=\"postHeader\">";
216 $entry_author = $line["author"];
219 $entry_author = _(" - by ") . $entry_author;
222 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
223 strtotime($line["updated"]));
225 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
228 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
229 $line["title"] . "</a>$entry_author</div>";
231 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
234 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
235 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
243 while ($tmp_line = db_fetch_assoc($tmp_result)) {
245 $tag = $tmp_line["tag_name"];
246 $tag_str = "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
248 if ($num_tags == 6) {
249 $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
250 } else if ($num_tags < 6) {
251 $tags_str .= $tag_str;
253 $f_tags_str .= $tag_str;
256 $tags_str = preg_replace("/, $/", "", $tags_str);
257 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
259 if (!$entry_comments) $entry_comments = " "; # placeholder
261 if (!$tags_str) $tags_str = '<span class="tagList">'._('no tags').'</span>';
263 print "<div style='float : right'>$tags_str
264 <a title=\"Edit tags for this article\"
265 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
266 <div clear='both'>$entry_comments</div>";
270 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
271 print "<div class=\"postContent\">";
273 if (db_num_rows($tmp_result) > 0) {
274 print "<div id=\"allEntryTags\">"._('Tags:')."$f_tags_str</div>";
277 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
278 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
281 $line["content"] = sanitize_rss($line["content"]);
283 print $line["content"] . "</div>";
290 if ($op == "viewfeed") {
292 $feed = db_escape_string($_GET["feed"]);
293 $subop = db_escape_string($_GET["subop"]);
294 $view_mode = db_escape_string($_GET["view_mode"]);
295 $limit = db_escape_string($_GET["limit"]);
296 $cat_view = db_escape_string($_GET["cat"]);
297 $next_unread_feed = db_escape_string($_GET["nuf"]);
299 if ($subop == "undefined") $subop = "";
301 if ($subop == "CatchupSelected") {
302 $ids = split(",", db_escape_string($_GET["ids"]));
303 $cmode = sprintf("%d", $_GET["cmode"]);
305 catchupArticlesById($link, $ids, $cmode);
308 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
309 update_generic_feed($link, $feed, $cat_view);
312 if ($subop == "MarkAllRead") {
313 catchup_feed($link, $feed, $cat_view);
315 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
316 if ($next_unread_feed) {
317 $feed = $next_unread_feed;
323 $result = db_query($link,
324 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
326 if (db_num_rows($result) == 0) {
327 print "<div align='center'>"._('Feed not found.')."</div>";
332 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
334 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
335 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
337 if (db_num_rows($result) == 1) {
338 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
340 $rtl_content = false;
344 $rtl_tag = "dir=\"RTL\"";
350 $rtl_content = false;
353 $script_dt_add = get_script_dt_add();
355 /* print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
356 <script type=\"text/javascript\" src=\"prototype.js\"></script>
357 <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
358 <script type=\"text/javascript\" src=\"viewfeed.js?$script_dt_add\"></script>
359 <!--[if gte IE 5.5000]>
360 <script type=\"text/javascript\" src=\"pngfix.js\"></script>
361 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
363 </head><body $rtl_tag>
364 <script type=\"text/javascript\">
365 if (document.addEventListener) {
366 document.addEventListener(\"DOMContentLoaded\", init, null);
368 window.onload = init;
371 /// START /////////////////////////////////////////////////////////////////////////////////
373 $search = db_escape_string($_GET["query"]);
374 $search_mode = db_escape_string($_GET["search_mode"]);
375 $match_on = db_escape_string($_GET["match_on"]);
381 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on);
383 $result = $qfh_ret[0];
384 $feed_title = $qfh_ret[1];
385 $feed_site_url = $qfh_ret[2];
386 $last_error = $qfh_ret[3];
388 /// STOP //////////////////////////////////////////////////////////////////////////////////
390 print "<div id=\"headlinesContainer\" $rtl_tag>";
393 print "<div align='center'>"._("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
397 if (db_num_rows($result) > 0) {
399 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
400 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode);
402 print "<div id=\"headlinesInnerContainer\">";
404 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
405 print "<table class=\"headlinesList\" id=\"headlinesList\"
406 cellspacing=\"0\" width=\"100%\">";
411 error_reporting (DEFAULT_ERROR_LEVEL
);
415 while ($line = db_fetch_assoc($result)) {
417 $class = ($lnum %
2) ?
"even" : "odd";
420 $feed_id = $line["feed_id"];
422 if ($line["last_read"] == "" &&
423 ($line["unread"] != "t" && $line["unread"] != "1")) {
425 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
428 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
432 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
440 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
441 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
442 alt=\"Reset mark\" onclick='javascript:toggleMark($id)'>";
444 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
445 alt=\"Set mark\" onclick='javascript:toggleMark($id)'>";
448 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
449 # $line["title"] . "</a>";
451 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
452 $line["title"] . "</a>";
454 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
455 # $line["title"] . "</a>";
457 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
458 $updated_fmt = smart_date_time(strtotime($line["updated"]));
460 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
461 $updated_fmt = date($short_date, strtotime($line["updated"]));
464 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
465 $content_preview = truncate_string(strip_tags($line["content_preview"]),
469 $entry_author = $line["author"];
472 $entry_author = " - by $entry_author";
475 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
477 print "<tr class='$class' id='RROW-$id'>";
479 print "<td class='hlUpdatePic'>$update_pic</td>";
481 print "<td class='hlSelectRow'>
482 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
483 class=\"feedCheckBox\" id=\"RCHK-$id\">
486 print "<td class='hlMarkedPic'>$marked_pic</td>";
488 if ($line["feed_title"]) {
489 print "<td class='hlContent'>$content_link</td>";
490 print "<td class='hlFeed'>
491 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
492 $line["feed_title"]."</a> </td>";
494 print "<td class='hlContent' valign='middle'>";
496 print "<a href=\"javascript:view($id,$feed_id);\">" .
499 if (get_pref($link, 'SHOW_CONTENT_PREVIEW') && !$rtl_tag) {
500 if ($content_preview) {
501 print "<span class=\"contentPreview\"> - $content_preview</span>";
509 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
516 $add_class = "Unread";
521 print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
523 print "<div class=\"cdmHeader\">";
525 print "<div style=\"float : right\">$updated_fmt,
526 <a class=\"cdmToggleLink\"
527 href=\"javascript:toggleUnread($id)\">Toggle unread</a>
530 print "<a class=\"title\"
531 onclick=\"javascript:toggleUnread($id, 0)\"
532 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
536 if ($line["feed_title"]) {
537 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
542 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
544 print "<div style=\"float : right\">$marked_pic</div>
545 <div lass=\"cdmFooter\">
546 <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
547 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\"></div>";
549 # print "<div align=\"center\"><a class=\"cdmToggleLink\"
550 # href=\"javascript:toggleUnread($id)\">
551 # Toggle unread</a></div>";
560 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
566 // print_headline_subtoolbar($link,
567 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
571 print "<div class='whiteBox'>"._('No articles found.')."</div>";
577 if ($op == "pref-feeds") {
578 module_pref_feeds($link);
581 if ($op == "pref-filters") {
582 module_pref_filters($link);
585 if ($op == "pref-labels") {
586 module_pref_labels($link);
589 if ($op == "pref-prefs") {
590 module_pref_prefs($link);
593 if ($op == "pref-users") {
594 module_pref_users($link);
602 module_popup_dialog($link);
605 // update feeds of all users, may be used anonymously
606 if ($op == "globalUpdateFeeds") {
608 $result = db_query($link, "SELECT id FROM ttrss_users");
610 while ($line = db_fetch_assoc($result)) {
611 $user_id = $line["id"];
612 // print "<!-- updating feeds of uid $user_id -->";
613 update_all_feeds($link, false, $user_id);
617 <message msg=\"All feeds updated\"/>
622 if ($op == "user-details") {
624 if (WEB_DEMO_MODE ||
$_SESSION["access_level"] < 10) {
628 /* print "<html><head>
629 <title>Tiny Tiny RSS : User Details</title>
630 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
631 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
634 $uid = sprintf("%d", $_GET["id"]);
636 print "<div id=\"infoBoxTitle\">User details</div>";
638 print "<div class='infoBoxContents'>";
640 $result = db_query($link, "SELECT login,
641 SUBSTRING(last_login,1,16) AS last_login,
643 (SELECT COUNT(int_id) FROM ttrss_user_entries
644 WHERE owner_uid = id) AS stored_articles
648 if (db_num_rows($result) == 0) {
649 print "<h1>User not found</h1>";
653 # print "<h1>User Details</h1>";
655 $login = db_fetch_result($result, 0, "login");
657 # print "<h1>$login</h1>";
659 print "<table width='100%'>";
661 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
662 strtotime(db_fetch_result($result, 0, "last_login")));
663 $access_level = db_fetch_result($result, 0, "access_level");
664 $stored_articles = db_fetch_result($result, 0, "stored_articles");
666 # print "<tr><td>Username</td><td>$login</td></tr>";
667 # print "<tr><td>Access level</td><td>$access_level</td></tr>";
668 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
669 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
671 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
672 WHERE owner_uid = '$uid'");
674 $num_feeds = db_fetch_result($result, 0, "num_feeds");
676 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
678 /* $result = db_query($link, "SELECT
679 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size
680 FROM ttrss_user_entries,ttrss_entries
681 WHERE owner_uid = '$uid' AND ref_id = id");
683 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
685 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>"; */
689 print "<h1>Subscribed feeds</h1>";
691 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
692 WHERE owner_uid = '$uid' ORDER BY title");
694 print "<ul class=\"userFeedList\">";
698 while ($line = db_fetch_assoc($result)) {
700 $icon_file = ICONS_URL
."/".$line["id"].".ico";
702 if (file_exists($icon_file) && filesize($icon_file) > 0) {
703 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
705 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
708 print "<li class=\"$row_class\">$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
710 $row_class = toggleEvenOdd($row_class);
714 if (db_num_rows($result) < $num_feeds) {
715 // FIXME - add link to show ALL subscribed feeds here somewhere
717 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\"> ...</li>";
724 print "<div align='center'>
725 <input type='submit' class='button'
726 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
728 // print "</body></html>";
732 if ($op == "pref-feed-browser") {
733 module_pref_feed_browser($link);
737 $feed = db_escape_string($_GET["id"]);
738 $user = db_escape_string($_GET["user"]);
739 $pass = db_escape_string($_GET["pass"]);
740 $is_cat = $_GET["is_cat"] != false;
742 $search = db_escape_string($_GET["q"]);
743 $match_on = db_escape_string($_GET["m"]);
744 $search_mode = db_escape_string($_GET["smode"]);
746 if (!$_SESSION["uid"] && $user && $pass) {
747 authenticate_user($link, $user, $pass);
750 if ($_SESSION["uid"] ||
751 http_authenticate_user($link)) {
753 generate_syndicated_feed($link, $feed, $is_cat,
754 $search, $search_mode, $match_on);
758 if ($op == "labelFromSearch") {
759 $search = db_escape_string($_GET["search"]);
760 $search_mode = db_escape_string($_GET["smode"]);
761 $match_on = db_escape_string($_GET["match"]);
762 $is_cat = db_escape_string($_GET["is_cat"]);
763 $title = db_escape_string($_GET["title"]);
764 $feed = sprintf("%d", $_GET["feed"]);
766 $label_qparts = array();
768 $search_expr = getSearchSql($search, $match_on);
772 $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
774 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
777 if ($search_mode == "all_feeds") {
779 } else if ($search_mode == "this_cat") {
781 $tmp_result = db_query($link, "SELECT cat_id
782 FROM ttrss_feeds WHERE id = '$feed'");
784 $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
787 $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
789 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
792 $search_expr .= " AND ttrss_feeds.id = $feed ";
797 $search_expr = db_escape_string($search_expr);
802 $result = db_query($link,
803 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid)
804 VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
808 if ($op == "getUnread") {
809 $login = db_escape_string($_GET["login"]);
811 header("Content-Type: text/plain; charset=utf-8");
813 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
815 if (db_num_rows($result) == 1) {
816 $uid = db_fetch_result($result, 0, "id");
817 print getGlobalUnread($link, $uid);
819 print "-1;User not found";
822 $print_exec_time = false;
825 if ($op == "digestTest") {
826 header("Content-Type: text/plain");
827 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
828 $print_exec_time = false;
832 if ($op == "digestSend") {
833 header("Content-Type: text/plain");
834 send_headlines_digests($link);
835 $print_exec_time = false;
842 <?php
if ($print_exec_time) { ?
>
843 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->