]>
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 $op = $_REQUEST["op"];
21 define('SCHEMA_VERSION', 13);
23 require_once "sanity_check.php";
24 require_once "config.php";
26 require_once "db.php";
27 require_once "db-prefs.php";
28 require_once "functions.php";
30 $print_exec_time = false;
32 if ((!$op ||
$op == "rpc" ||
$op == "rss" ||
$op == "digestSend" ||
33 $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
34 header("Content-Type: application/xml; charset=utf-8");
36 header("Content-Type: text/html; charset=utf-8");
40 header("Content-Type: application/xml");
41 print_error_xml(7); exit;
44 if (!$_SESSION["uid"] && $op != "globalUpdateFeeds" && $op != "rss" && $op != "getUnread") {
47 print_error_xml(6); die;
51 <p>Error: Not logged in.</p>
52 <script type=\"text/javascript\">
53 if (parent.window != 'undefined') {
54 parent.window.location = \"login.php\";
56 window.location = \"login.php\";
65 $purge_intervals = array(
66 0 => _("Use default"),
67 -1 => _("Never purge"),
69 14 => _("2 weeks old"),
70 31 => _("1 month old"),
71 60 => _("2 months old"),
72 90 => _("3 months old"));
74 $update_intervals = array(
75 0 => _("Use default"),
76 -1 => _("Disable updates"),
77 30 => _("Each 30 minutes"),
79 240 => _("Each 4 hours"),
80 720 => _("Each 12 hours"),
82 10080 => _("Weekly"));
85 $access_level_names = array(
87 10 => _("Administrator"));
89 require_once "modules/pref-prefs.php";
90 require_once "modules/popup-dialog.php";
91 require_once "modules/help.php";
92 require_once "modules/pref-feeds.php";
93 require_once "modules/pref-filters.php";
94 require_once "modules/pref-labels.php";
95 require_once "modules/pref-users.php";
96 require_once "modules/pref-feed-browser.php";
98 $script_started = getmicrotime();
100 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
103 if (DB_TYPE
== "mysql") {
106 // PG seems to display its own errors just fine by default.
110 if (DB_TYPE
== "pgsql") {
111 pg_query("set client_encoding = 'utf-8'");
114 if (!sanity_check($link)) { return; }
117 handle_rpc_request($link);
120 if ($op == "feeds") {
122 $tags = $_GET["tags"];
124 $subop = $_GET["subop"];
126 if ($subop == "catchupAll") {
127 db_query($link, "UPDATE ttrss_user_entries SET
128 last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
131 if ($subop == "collapse") {
132 $cat_id = db_escape_string($_GET["cid"]);
134 db_query($link, "UPDATE ttrss_feed_categories SET
135 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " .
140 outputFeedList($link, $tags);
146 $id = db_escape_string($_GET["id"]);
147 $feed_id = db_escape_string($_GET["feed"]);
149 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
150 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
152 if (db_num_rows($result) == 1) {
153 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
155 $rtl_content = false;
159 $rtl_tag = "dir=\"RTL\"";
166 $result = db_query($link, "UPDATE ttrss_user_entries
167 SET unread = false,last_read = NOW()
168 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
170 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
171 SUBSTRING(updated,1,16) as updated,
172 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
175 FROM ttrss_entries,ttrss_user_entries
176 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
182 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
183 $link_target = "target=\"_new\"";
186 $line = db_fetch_assoc($result);
188 if ($line["icon_url"]) {
189 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
191 $feed_icon = " ";
194 /* if ($line["comments"] && $line["link"] != $line["comments"]) {
195 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
197 $entry_comments = "";
200 $num_comments = $line["num_comments"];
201 $entry_comments = "";
203 if ($num_comments > 0) {
204 if ($line["comments"]) {
205 $comments_url = $line["comments"];
207 $comments_url = $line["link"];
209 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
211 if ($line["comments"] && $line["link"] != $line["comments"]) {
212 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
216 print "<div class=\"postReply\">";
218 print "<div class=\"postHeader\">";
220 $entry_author = $line["author"];
223 $entry_author = _(" - by ") . $entry_author;
226 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
227 strtotime($line["updated"]));
229 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
232 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
233 $line["title"] . "</a>$entry_author</div>";
235 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
238 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
239 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
247 while ($tmp_line = db_fetch_assoc($tmp_result)) {
249 $tag = $tmp_line["tag_name"];
250 $tag_str = "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
252 if ($num_tags == 6) {
253 $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>";
254 } else if ($num_tags < 6) {
255 $tags_str .= $tag_str;
257 $f_tags_str .= $tag_str;
260 $tags_str = preg_replace("/, $/", "", $tags_str);
261 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
263 if (!$entry_comments) $entry_comments = " "; # placeholder
265 if (!$tags_str) $tags_str = '<span class="tagList">'._('no tags').'</span>';
267 print "<div style='float : right'>$tags_str
268 <a title=\"Edit tags for this article\"
269 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
270 <div clear='both'>$entry_comments</div>";
274 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
275 print "<div class=\"postContent\">";
277 if (db_num_rows($tmp_result) > 0) {
278 print "<div id=\"allEntryTags\">"._('Tags:')."$f_tags_str</div>";
281 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
282 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
285 $line["content"] = sanitize_rss($line["content"]);
287 print $line["content"] . "</div>";
294 if ($op == "viewfeed") {
296 $feed = db_escape_string($_GET["feed"]);
297 $subop = db_escape_string($_GET["subop"]);
298 $view_mode = db_escape_string($_GET["view_mode"]);
299 $limit = db_escape_string($_GET["limit"]);
300 $cat_view = db_escape_string($_GET["cat"]);
301 $next_unread_feed = db_escape_string($_GET["nuf"]);
302 $offset = db_escape_string($_GET["skip"]);
304 if (!$offset) $offset = 0;
306 if ($subop == "undefined") $subop = "";
308 if ($subop == "CatchupSelected") {
309 $ids = split(",", db_escape_string($_GET["ids"]));
310 $cmode = sprintf("%d", $_GET["cmode"]);
312 catchupArticlesById($link, $ids, $cmode);
315 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
316 update_generic_feed($link, $feed, $cat_view);
319 if ($subop == "MarkAllRead") {
320 catchup_feed($link, $feed, $cat_view);
322 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
323 if ($next_unread_feed) {
324 $feed = $next_unread_feed;
330 $result = db_query($link,
331 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
333 if (db_num_rows($result) == 0) {
334 print "<div align='center'>"._('Feed not found.')."</div>";
339 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
341 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
342 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
344 if (db_num_rows($result) == 1) {
345 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
347 $rtl_content = false;
351 $rtl_tag = "dir=\"RTL\"";
357 $rtl_content = false;
360 $script_dt_add = get_script_dt_add();
362 /* print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
363 <script type=\"text/javascript\" src=\"prototype.js\"></script>
364 <script type=\"text/javascript\" src=\"functions.js?$script_dt_add\"></script>
365 <script type=\"text/javascript\" src=\"viewfeed.js?$script_dt_add\"></script>
366 <!--[if gte IE 5.5000]>
367 <script type=\"text/javascript\" src=\"pngfix.js\"></script>
368 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss-ie.css\">
370 </head><body $rtl_tag>
371 <script type=\"text/javascript\">
372 if (document.addEventListener) {
373 document.addEventListener(\"DOMContentLoaded\", init, null);
375 window.onload = init;
378 /// START /////////////////////////////////////////////////////////////////////////////////
380 $search = db_escape_string($_GET["query"]);
381 $search_mode = db_escape_string($_GET["search_mode"]);
382 $match_on = db_escape_string($_GET["match_on"]);
388 $real_offset = $offset * $limit;
390 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
391 $search, $search_mode, $match_on, false, $real_offset);
393 $result = $qfh_ret[0];
394 $feed_title = $qfh_ret[1];
395 $feed_site_url = $qfh_ret[2];
396 $last_error = $qfh_ret[3];
398 /// STOP //////////////////////////////////////////////////////////////////////////////////
400 print "<div id=\"headlinesContainer\" $rtl_tag>";
403 print "<div align='center'>"._("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
407 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
408 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, $offset);
410 print "<div id=\"headlinesInnerContainer\">";
412 if (db_num_rows($result) > 0) {
414 # print "\{$offset}";
416 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
417 print "<table class=\"headlinesList\" id=\"headlinesList\"
418 cellspacing=\"0\" width=\"100%\">";
423 error_reporting (DEFAULT_ERROR_LEVEL
);
427 while ($line = db_fetch_assoc($result)) {
429 $class = ($lnum %
2) ?
"even" : "odd";
432 $feed_id = $line["feed_id"];
434 if ($line["last_read"] == "" &&
435 ($line["unread"] != "t" && $line["unread"] != "1")) {
437 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
440 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
444 if ($line["unread"] == "t" ||
$line["unread"] == "1") {
452 if ($line["marked"] == "t" ||
$line["marked"] == "1") {
453 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_set.png\"
455 alt=\"Reset mark\" onclick='javascript:toggleMark($id)'>";
457 $marked_pic = "<img id=\"FMARKPIC-$id\" src=\"images/mark_unset.png\"
459 alt=\"Set mark\" onclick='javascript:toggleMark($id)'>";
462 # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
463 # $line["title"] . "</a>";
465 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
466 $line["title"] . "</a>";
468 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
469 # $line["title"] . "</a>";
471 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
472 $updated_fmt = smart_date_time(strtotime($line["updated"]));
474 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
475 $updated_fmt = date($short_date, strtotime($line["updated"]));
478 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
479 $content_preview = truncate_string(strip_tags($line["content_preview"]),
483 $entry_author = $line["author"];
486 $entry_author = " - by $entry_author";
489 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
491 print "<tr class='$class' id='RROW-$id'>";
493 print "<td class='hlUpdatePic'>$update_pic</td>";
495 print "<td class='hlSelectRow'>
496 <input type=\"checkbox\" onclick=\"toggleSelectRow(this)\"
497 class=\"feedCheckBox\" id=\"RCHK-$id\">
500 print "<td class='hlMarkedPic'>$marked_pic</td>";
502 if ($line["feed_title"]) {
503 print "<td class='hlContent'>$content_link</td>";
504 print "<td class='hlFeed'>
505 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
506 $line["feed_title"]."</a> </td>";
508 print "<td class='hlContent' valign='middle'>";
510 print "<a href=\"javascript:view($id,$feed_id);\">" .
513 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
514 if ($content_preview) {
515 print "<span class=\"contentPreview\"> - $content_preview</span>";
523 print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>";
530 $add_class = "Unread";
535 print "<div class=\"cdmArticle$add_class\" id=\"RROW-$id\">";
537 print "<div class=\"cdmHeader\">";
539 print "<div class=\"articleUpdated\">$updated_fmt</div>";
541 print "<a class=\"title\"
542 onclick=\"javascript:toggleUnread($id, 0)\"
543 target=\"new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
547 if ($line["feed_title"]) {
548 print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
553 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
555 print "<div class=\"cdmFooter\">";
559 print "<input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
560 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
562 $tags = get_article_tags($link, $id);
566 foreach ($tags as $tag) {
568 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
571 $tags_str = preg_replace("/, $/", "", $tags_str);
573 if ($tags_str == "") $tags_str = "no tags";
575 print " $tags_str <a title=\"Edit tags for this article\"
576 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
580 # print "<div align=\"center\"><a class=\"cdmToggleLink\"
581 # href=\"javascript:toggleUnread($id)\">
582 # Toggle unread</a></div>";
591 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
595 // print_headline_subtoolbar($link,
596 // "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
600 print "<div class='whiteBox'>"._('No articles found.')."</div>";
608 if ($op == "pref-feeds") {
609 module_pref_feeds($link);
612 if ($op == "pref-filters") {
613 module_pref_filters($link);
616 if ($op == "pref-labels") {
617 module_pref_labels($link);
620 if ($op == "pref-prefs") {
621 module_pref_prefs($link);
624 if ($op == "pref-users") {
625 module_pref_users($link);
633 module_popup_dialog($link);
636 // update feeds of all users, may be used anonymously
637 if ($op == "globalUpdateFeeds") {
639 $result = db_query($link, "SELECT id FROM ttrss_users");
641 while ($line = db_fetch_assoc($result)) {
642 $user_id = $line["id"];
643 // print "<!-- updating feeds of uid $user_id -->";
644 update_all_feeds($link, false, $user_id);
648 <message msg=\"All feeds updated\"/>
653 if ($op == "user-details") {
655 if (WEB_DEMO_MODE ||
$_SESSION["access_level"] < 10) {
659 /* print "<html><head>
660 <title>Tiny Tiny RSS : User Details</title>
661 <link rel=\"stylesheet\" href=\"tt-rss.css\" type=\"text/css\">
662 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
665 $uid = sprintf("%d", $_GET["id"]);
667 print "<div id=\"infoBoxTitle\">User details</div>";
669 print "<div class='infoBoxContents'>";
671 $result = db_query($link, "SELECT login,
672 SUBSTRING(last_login,1,16) AS last_login,
674 (SELECT COUNT(int_id) FROM ttrss_user_entries
675 WHERE owner_uid = id) AS stored_articles
679 if (db_num_rows($result) == 0) {
680 print "<h1>User not found</h1>";
684 # print "<h1>User Details</h1>";
686 $login = db_fetch_result($result, 0, "login");
688 # print "<h1>$login</h1>";
690 print "<table width='100%'>";
692 $last_login = date(get_pref($link, 'LONG_DATE_FORMAT'),
693 strtotime(db_fetch_result($result, 0, "last_login")));
694 $access_level = db_fetch_result($result, 0, "access_level");
695 $stored_articles = db_fetch_result($result, 0, "stored_articles");
697 # print "<tr><td>Username</td><td>$login</td></tr>";
698 # print "<tr><td>Access level</td><td>$access_level</td></tr>";
699 print "<tr><td>Last logged in</td><td>$last_login</td></tr>";
700 print "<tr><td>Stored articles</td><td>$stored_articles</td></tr>";
702 $result = db_query($link, "SELECT COUNT(id) as num_feeds FROM ttrss_feeds
703 WHERE owner_uid = '$uid'");
705 $num_feeds = db_fetch_result($result, 0, "num_feeds");
707 print "<tr><td>Subscribed feeds count</td><td>$num_feeds</td></tr>";
709 /* $result = db_query($link, "SELECT
710 SUM(LENGTH(content)+LENGTH(title)+LENGTH(link)+LENGTH(guid)) AS db_size
711 FROM ttrss_user_entries,ttrss_entries
712 WHERE owner_uid = '$uid' AND ref_id = id");
714 $db_size = round(db_fetch_result($result, 0, "db_size") / 1024);
716 print "<tr><td>Approx. used DB size</td><td>$db_size KBytes</td></tr>"; */
720 print "<h1>Subscribed feeds</h1>";
722 $result = db_query($link, "SELECT id,title,site_url FROM ttrss_feeds
723 WHERE owner_uid = '$uid' ORDER BY title");
725 print "<ul class=\"userFeedList\">";
729 while ($line = db_fetch_assoc($result)) {
731 $icon_file = ICONS_URL
."/".$line["id"].".ico";
733 if (file_exists($icon_file) && filesize($icon_file) > 0) {
734 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"$icon_file\">";
736 $feed_icon = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\">";
739 print "<li class=\"$row_class\">$feed_icon <a href=\"".$line["site_url"]."\">".$line["title"]."</a></li>";
741 $row_class = toggleEvenOdd($row_class);
745 if (db_num_rows($result) < $num_feeds) {
746 // FIXME - add link to show ALL subscribed feeds here somewhere
748 class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\"> ...</li>";
755 print "<div align='center'>
756 <input type='submit' class='button'
757 onclick=\"closeInfoBox()\" value=\"Close this window\"></div>";
759 // print "</body></html>";
763 if ($op == "pref-feed-browser") {
764 module_pref_feed_browser($link);
768 $feed = db_escape_string($_GET["id"]);
769 $user = db_escape_string($_GET["user"]);
770 $pass = db_escape_string($_GET["pass"]);
771 $is_cat = $_GET["is_cat"] != false;
773 $search = db_escape_string($_GET["q"]);
774 $match_on = db_escape_string($_GET["m"]);
775 $search_mode = db_escape_string($_GET["smode"]);
777 if (!$_SESSION["uid"] && $user && $pass) {
778 authenticate_user($link, $user, $pass);
781 if ($_SESSION["uid"] ||
782 http_authenticate_user($link)) {
784 generate_syndicated_feed($link, $feed, $is_cat,
785 $search, $search_mode, $match_on);
789 if ($op == "labelFromSearch") {
790 $search = db_escape_string($_GET["search"]);
791 $search_mode = db_escape_string($_GET["smode"]);
792 $match_on = db_escape_string($_GET["match"]);
793 $is_cat = db_escape_string($_GET["is_cat"]);
794 $title = db_escape_string($_GET["title"]);
795 $feed = sprintf("%d", $_GET["feed"]);
797 $label_qparts = array();
799 $search_expr = getSearchSql($search, $match_on);
803 $search_expr .= " AND ttrss_feeds.cat_id = $feed ";
805 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
808 if ($search_mode == "all_feeds") {
810 } else if ($search_mode == "this_cat") {
812 $tmp_result = db_query($link, "SELECT cat_id
813 FROM ttrss_feeds WHERE id = '$feed'");
815 $cat_id = db_fetch_result($tmp_result, 0, "cat_id");
818 $search_expr .= " AND ttrss_feeds.cat_id = $cat_id ";
820 $search_expr .= " AND ttrss_feeds.cat_id IS NULL ";
823 $search_expr .= " AND ttrss_feeds.id = $feed ";
828 $search_expr = db_escape_string($search_expr);
833 $result = db_query($link,
834 "INSERT INTO ttrss_labels (sql_exp,description,owner_uid)
835 VALUES ('$search_expr', '$title', '".$_SESSION["uid"]."')");
839 if ($op == "getUnread") {
840 $login = db_escape_string($_GET["login"]);
842 header("Content-Type: text/plain; charset=utf-8");
844 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
846 if (db_num_rows($result) == 1) {
847 $uid = db_fetch_result($result, 0, "id");
848 print getGlobalUnread($link, $uid);
850 print "-1;User not found";
853 $print_exec_time = false;
856 if ($op == "digestTest") {
857 header("Content-Type: text/plain");
858 print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
859 $print_exec_time = false;
863 if ($op == "digestSend") {
864 header("Content-Type: text/plain");
865 send_headlines_digests($link);
866 $print_exec_time = false;
873 <?php
if ($print_exec_time) { ?
>
874 <!-- <?php
echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?
> -->