]> git.wh0rd.org - tt-rss.git/blobdiff - backend.php
functions.php: Fixed some mild errors.
[tt-rss.git] / backend.php
index 6fad38d2e92c761d5bc7f3966acbc742c05fca31..29903843495636e52e962ae63fa91facbf6fc0a5 100644 (file)
@@ -24,9 +24,7 @@
 
        no_cache_incantation();
 
-       if (ENABLE_TRANSLATIONS == true) {
-               startup_gettext();
-       }
+       startup_gettext();
 
        $script_started = getmicrotime();
 
        $subop = $_REQUEST["subop"];
        $mode = $_REQUEST["mode"];
 
-       $print_exec_time = false;
-
-       if ((!$op || $op == "rpc" || $op == "rss" ||
-                       $op == "digestSend" || $op == "dlg" ||
-                       $op == "globalUpdateFeeds") && !$_REQUEST["noxml"]) {
-                               header("Content-Type: application/xml; charset=utf-8");
-
-               } else {
-               if (!$_REQUEST["noxml"]) {
-                       header("Content-Type: text/html; charset=utf-8");
-               } else {
+       if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
+                       header("Content-Type: application/xml; charset=utf-8");
+       } else {
                        header("Content-Type: text/plain; charset=utf-8");
-               }
        }
 
        if (ENABLE_GZIP_OUTPUT) {
                authenticate_user($link, "admin", null);
        }
 
-       /* if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds"
-               && $op != "rss" && $op != "getUnread" && $op != "publish" && $op != "getProfiles") {
+       if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
+                       $op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
+                       $op != "fbexport" && $op != "logout" && $op != "pubsub") {
 
-               if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
-                       print_error_xml(6); exit;
+               if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
+                       header("Content-Type: text/html");
+                       login_sequence($link);
+                       render_login_form($link);
                } else {
-                       header("Location: tt-rss.php?return=" .
-                               urlencode($_SERVER['REQUEST_URI']));
+                       header("Content-Type: text/plain");
+                       print json_encode(array("error" => array("code" => 6)));
                }
-               exit;
-       } */
-
-       if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
-                               $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
-
-               header("Content-Type: text/plain");
-               print json_encode(array("error" => array("code" => 6)));
                return;
        }
 
        require_once "modules/pref-filters.php";
        require_once "modules/pref-labels.php";
        require_once "modules/pref-users.php";
+       require_once "modules/pref-instances.php";
+
+       $error = sanity_check($link);
 
-       if (!sanity_check($link)) { return; }
+       if ($error['code'] != 0 && $op != "logout") {
+               print json_encode(array("error" => $error));
+               return;
+       }
 
        switch($op) { // Select action according to $op value.
                case "rpc":
                        $id = db_escape_string($_REQUEST['id']);
 
                        $result = db_query($link, "SELECT link FROM ttrss_entries, ttrss_user_entries
-                               WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'");
+                               WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."'
+                               LIMIT 1");
 
                        if (db_num_rows($result) == 1) {
                                $article_url = db_fetch_result($result, 0, 'link');
                case "view":
 
                        $id = db_escape_string($_REQUEST["id"]);
-                       $cids = split(",", db_escape_string($_REQUEST["cids"]));
+                       $cids = explode(",", db_escape_string($_REQUEST["cids"]));
                        $mode = db_escape_string($_REQUEST["mode"]);
                        $omode = db_escape_string($_REQUEST["omode"]);
 
                        if ($mode == "") {
                                array_push($articles, format_article($link, $id, false));
                        } else if ($mode == "zoom") {
-                               array_push($articles, format_article($link, $id, false, true, true));
+                               array_push($articles, format_article($link, $id, true, true));
+                       } else if ($mode == "raw") {
+                               if ($_REQUEST['html']) {
+                                       header("Content-Type: text/html");
+                                       print '<link rel="stylesheet" type="text/css" href="tt-rss.css"/>';
+                               }
+
+                               $article = format_article($link, $id, false);
+                               print $article['content'];
+                               return;
                        } else {
                                catchupArticleById($link, $id, 0);
                        }
                        $result = false;
 
                        if ($feed < -10) {
-                               $label_feed = -10-$feed;
+                               $label_feed = -11-$feed;
                                $result = db_query($link, "SELECT id FROM ttrss_labels2 WHERE
                                        id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
                        } else if (!$cat_view && $feed > 0) {
                                $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
                                        id = '$feed' AND owner_uid = " . $_SESSION['uid']);
-                       } else if ($cat_view) {
+                       } else if ($cat_view && $feed > 0) {
                                $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
                                        id = '$feed' AND owner_uid = " . $_SESSION['uid']);
                        }
                                $articles = array();
 
                                foreach ($topmost_article_ids as $id) {
-                                       array_push($articles, format_article($link, $id, $feed, false));
+                                       array_push($articles, format_article($link, $id, false));
                                }
 
                                $reply['articles'] = $articles;
                break; // pref-pub-items
 
                case "globalUpdateFeeds":
-                       // update feeds of all users, may be used anonymously
-
-                       print "<!--";
                        // Update all feeds needing a update.
                        update_daemon_common($link, 0, true, true);
-                       print " -->";
-
-                       print "<rpc-reply>
-                               <message msg=\"All feeds updated\"/>
-                       </rpc-reply>";
                break; // globalUpdateFeeds
 
                case "pref-feed-browser":
                        module_pref_feed_browser($link);
                break; // pref-feed-browser
 
+               case "pref-instances":
+                       module_pref_instances($link);
+               break; // pref-instances
+
                case "rss":
                        $feed = db_escape_string($_REQUEST["id"]);
                        $key = db_escape_string($_REQUEST["key"]);
                        $login = db_escape_string($_REQUEST["login"]);
                        $fresh = $_REQUEST["fresh"] == "1";
 
-                       header("Content-Type: text/plain; charset=utf-8");
-
                        $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
 
                        if (db_num_rows($result) == 1) {
                                print "-1;User not found";
                        }
 
-                       $print_exec_time = false;
                break; // getUnread
 
                case "digestTest":
-                       header("Content-Type: text/plain");
                        print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
-                       $print_exec_time = false;
                break; // digestTest
 
                case "digestSend":
-                       header("Content-Type: text/plain");
                        send_headlines_digests($link);
-                       $print_exec_time = false;
                break; // digestSend
 
                case "loading":
+                       header("Content-type: text/html");
                        print __("Loading, please wait...") . " " .
                                "<img src='images/indicator_tiny.gif'>";
+               break; // loading
 
                case "getProfiles":
                        $login = db_escape_string($_REQUEST["login"]);
 
                                $_SESSION = array();
                        }
-               break;
+               break; // getprofiles
 
-       } // Select action according to $op value.
+               case "pubsub":
+                       $mode = db_escape_string($_REQUEST['hub_mode']);
+                       $feed_id = db_escape_string($_REQUEST['id']);
+                       $feed_url = db_escape_string($_REQUEST['hub_topic']);
+
+                       // TODO: implement hub_verifytoken checking
+
+                       $result = db_query($link, "SELECT feed_url FROM ttrss_feeds
+                               WHERE id = '$feed_id'");
+
+                       if (db_num_rows($result) != 0) {
+
+                               $check_feed_url = db_fetch_result($result, 0, "feed_url");
+
+                               if ($check_feed_url && ($check_feed_url == $feed_url || !$feed_url)) {
+                                       if ($mode == "subscribe") {
+
+                                               db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 2
+                                                       WHERE id = '$feed_id'");
 
+                                               print $_REQUEST['hub_challenge'];
+                                               return;
+
+                                       } else if ($mode == "unsubscribe") {
+
+                                               db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 0
+                                                       WHERE id = '$feed_id'");
+
+                                               print $_REQUEST['hub_challenge'];
+                                               return;
+
+                                       } else if (!$mode) {
+
+                                               // Received update ping, schedule feed update.
+
+                                               update_rss_feed($link, $feed_id, true, true);
+
+                                       }
+                               } else {
+                                       header('HTTP/1.0 404 Not Found');
+                                       echo "404 Not found";
+                               }
+                       } else {
+                               header('HTTP/1.0 404 Not Found');
+                               echo "404 Not found";
+                       }
+
+               break; // pubsub
+
+               case "logout":
+                       logout_user();
+                       header("Location: tt-rss.php");
+               break; // logout
+
+               case "fbexport":
+
+                       $access_key = db_escape_string($_POST["key"]);
+
+                       // TODO: rate limit checking using last_connected
+                       $result = db_query($link, "SELECT id FROM ttrss_linked_instances
+                               WHERE access_key = '$access_key'");
+
+                       if (db_num_rows($result) == 1) {
+
+                               $instance_id = db_fetch_result($result, 0, "id");
+
+                               $result = db_query($link, "SELECT feed_url, site_url, title, subscribers
+                                       FROM ttrss_feedbrowser_cache ORDER BY subscribers DESC LIMIT 100");
+
+                               $feeds = array();
+
+                               while ($line = db_fetch_assoc($result)) {
+                                       array_push($feeds, $line);
+                               }
+
+                               db_query($link, "UPDATE ttrss_linked_instances SET
+                                       last_status_in = 1 WHERE id = '$instance_id'");
+
+                               print json_encode(array("feeds" => $feeds));
+                       } else {
+                               print json_encode(array("error" => array("code" => 6)));
+                       }
+               break; // fbexport
+
+               default:
+                       header("Content-Type: text/plain");
+                       print json_encode(array("error" => array("code" => 7)));
+               break; // fallback
+       } // Select action according to $op value.
 
        // We close the connection to database.
        db_close($link);
 ?>
-
-<?php if ($print_exec_time) { ?>
-<!-- <?php echo sprintf("Backend execution time: %.4f seconds", getmicrotime() - $script_started) ?> -->
-<?php } ?>