]> git.wh0rd.org - tt-rss.git/blobdiff - backend.php
overall directory tree cleanup
[tt-rss.git] / backend.php
index 6fad38d2e92c761d5bc7f3966acbc742c05fca31..247cab38b9df45451e9e8e6a35c4eff31800617c 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+       set_include_path(get_include_path() . PATH_SEPARATOR . "include");
+
        /* remove ill effects of magic quotes */
 
        if (get_magic_quotes_gpc()) {
                $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
 
+       $op = $_REQUEST["op"];
+
        require_once "functions.php";
-       require_once "sessions.php";
-       require_once "modules/backend-rpc.php";
+       if ($op != "share") require_once "sessions.php";
        require_once "sanity_check.php";
        require_once "config.php";
        require_once "db.php";
@@ -24,9 +27,7 @@
 
        no_cache_incantation();
 
-       if (ENABLE_TRANSLATIONS == true) {
-               startup_gettext();
-       }
+       startup_gettext();
 
        $script_started = getmicrotime();
 
 
        init_connection($link);
 
-       $op = $_REQUEST["op"];
        $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") {
+       $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
+               "fbexport", "logout", "pubsub");
 
-               if ($op == "rpc" || $op == "viewfeed" || $op == "view") {
-                       print_error_xml(6); exit;
-               } else {
-                       header("Location: tt-rss.php?return=" .
-                               urlencode($_SERVER['REQUEST_URI']));
-               }
-               exit;
-       } */
+       if (array_search($op, $public_calls) !== false) {
 
-       if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
-                               $op != "rss" && $op != "getUnread" && $op != "getProfiles") {
+               handle_public_request($link, $op);
+               return;
 
-               header("Content-Type: text/plain");
-               print json_encode(array("error" => array("code" => 6)));
+       } else if (!($_SESSION["uid"] && validate_session($link))) {
+               if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
+                       header("Content-Type: text/html");
+                       login_sequence($link);
+                       render_login_form($link);
+               } else {
+                       header("Content-Type: text/plain");
+                       print json_encode(array("error" => array("code" => 6)));
+               }
                return;
        }
 
                5 => __("Power User"),
                10 => __("Administrator"));
 
-       require_once "modules/pref-prefs.php";
-       require_once "modules/popup-dialog.php";
-       require_once "modules/help.php";
-       require_once "modules/pref-feeds.php";
-       require_once "modules/pref-filters.php";
-       require_once "modules/pref-labels.php";
-       require_once "modules/pref-users.php";
 
-       if (!sanity_check($link)) { return; }
+
+       $error = sanity_check($link);
+
+       if ($error['code'] != 0 && $op != "logout") {
+               print json_encode(array("error" => $error));
+               return;
+       }
 
        switch($op) { // Select action according to $op value.
                case "rpc":
-                       // Handle remote procedure calls.
+                       require_once "modules/backend-rpc.php";
                        handle_rpc_request($link);
                break; // 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));
-                       } else {
-                               catchupArticleById($link, $id, 0);
+                               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;
                        }
 
+                       catchupArticleById($link, $id, 0);
+
                        if (!$_SESSION["bw_limit"]) {
                                foreach ($cids as $cid) {
                                        if ($cid) {
                        $subop = db_escape_string($_REQUEST["subop"]);
                        $view_mode = db_escape_string($_REQUEST["view_mode"]);
                        $limit = (int) get_pref($link, "DEFAULT_ARTICLE_LIMIT");
-                       @$cat_view = db_escape_string($_REQUEST["cat"]);
+                       @$cat_view = db_escape_string($_REQUEST["cat"]) == "true";
                        @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
                        @$offset = db_escape_string($_REQUEST["skip"]);
                        @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
                        $order_by = db_escape_string($_REQUEST["order_by"]);
 
+                       if (is_numeric($feed)) $feed = (int) $feed;
+
                        /* Feed -5 is a special case: it is used to display auxiliary information
                         * when there's nothing to load - e.g. no stuff in fresh feed */
 
                        $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) {
+                       } else if (!$cat_view && is_numeric($feed) && $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 && is_numeric($feed) && $feed > 0) {
                                $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
                                        id = '$feed' AND owner_uid = " . $_SESSION['uid']);
                        }
                        $disable_cache = $ret[3];
                        $vgroup_last_feed = $ret[4];
 
-                       $reply['headlines']['content'] = $ret[5];
-                       $reply['headlines']['toolbar'] = $ret[6];
+//                     if ($_REQUEST["debug"]) print_r($ret);
 
-                       if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
+                       $reply['headlines']['content'] =& $ret[5]['content'];
+                       $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
 
-                       $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
-                                       $cat_view, true);
-
-                       if ($headlines_unread == -1) {
-                               $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
-                       }
+                       if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
 
                        $reply['headlines-info'] = array("count" => (int) $headlines_count,
                                "vgroup_last_feed" => $vgroup_last_feed,
-                               "unread" => (int) $headlines_unread,
                                "disable_cache" => (bool) $disable_cache);
 
                        if ($_REQUEST["debug"]) $timing_info = print_checkpoint("20", $timing_info);
                                $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;
                        }
 
-                       if ($subop) {
-                               $reply['counters'] = getAllCounters($link, $omode, $feed);
-                       }
+//                     if ($subop) {
+//                             $reply['counters'] = getAllCounters($link, $omode, $feed);
+//                     }
 
                        if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
 
                        $reply['runtime-info'] = make_runtime_info($link);
 
                        print json_encode($reply);
-
                break; // viewfeed
 
                case "pref-feeds":
+                       require_once "modules/pref-feeds.php";
                        module_pref_feeds($link);
                break; // pref-feeds
 
                case "pref-filters":
+                       require_once "modules/pref-filters.php";
                        module_pref_filters($link);
                break; // pref-filters
 
                case "pref-labels":
+                       require_once "modules/pref-labels.php";
                        module_pref_labels($link);
                break; // pref-labels
 
                case "pref-prefs":
+                       require_once "modules/pref-prefs.php";
                        module_pref_prefs($link);
                break; // pref-prefs
 
                case "pref-users":
+                       require_once "modules/pref-users.php";
                        module_pref_users($link);
                break; // prefs-users
 
                case "help":
+                       require_once "modules/help.php";
                        module_help($link);
                break; // help
 
                case "dlg":
+                       require_once "modules/popup-dialog.php";
                        module_popup_dialog($link);
                break; // dlg
 
-               case "pref-pub-items":
-                       module_pref_pub_items($link);
-               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 "rss":
-                       $feed = db_escape_string($_REQUEST["id"]);
-                       $key = db_escape_string($_REQUEST["key"]);
-                       $is_cat = $_REQUEST["is_cat"] != false;
-                       $limit = (int)db_escape_string($_REQUEST["limit"]);
-
-                       $search = db_escape_string($_REQUEST["q"]);
-                       $match_on = db_escape_string($_REQUEST["m"]);
-                       $search_mode = db_escape_string($_REQUEST["smode"]);
-                       $view_mode = db_escape_string($_REQUEST["view-mode"]);
-
-                       if (SINGLE_USER_MODE) {
-                               authenticate_user($link, "admin", null);
-                       }
-
-                       $owner_id = false;
-
-                       if ($key) {
-                               $result = db_query($link, "SELECT owner_uid FROM
-                                       ttrss_access_keys WHERE access_key = '$key' AND feed_id = '$feed'");
-
-                               if (db_num_rows($result) == 1)
-                                       $owner_id = db_fetch_result($result, 0, "owner_uid");
-                       }
-
-                       if ($owner_id) {
-                               $_SESSION['uid'] = $owner_id;
-
-                               generate_syndicated_feed($link, 0, $feed, $is_cat, $limit,
-                                       $search, $search_mode, $match_on, $view_mode);
-                       } else {
-                               header('HTTP/1.1 403 Forbidden');
-                       }
-               break; // rss
-
-               case "getUnread":
-                       $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) {
-                               $uid = db_fetch_result($result, 0, "id");
-
-                               print getGlobalUnread($link, $uid);
-
-                               if ($fresh) {
-                                       print ";";
-                                       print getFeedArticles($link, -3, false, true, $uid);
-                               }
-
-                       } else {
-                               print "-1;User not found";
-                       }
-
-                       $print_exec_time = false;
-               break; // getUnread
+               case "pref-instances":
+                       require_once "modules/pref-instances.php";
+                       module_pref_instances($link);
+               break; // pref-instances
 
                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"]);
-                       $password = db_escape_string($_REQUEST["password"]);
-
-                       if (authenticate_user($link, $login, $password)) {
-                               $result = db_query($link, "SELECT * FROM ttrss_settings_profiles
-                                       WHERE owner_uid = " . $_SESSION["uid"] . " ORDER BY title");
-
-                               print "<select style='width: 100%' name='profile'>";
-
-                               print "<option value='0'>" . __("Default profile") . "</option>";
-
-                               while ($line = db_fetch_assoc($result)) {
-                                       $id = $line["id"];
-                                       $title = $line["title"];
-
-                                       print "<option value='$id'>$title</option>";
-                               }
-
-                               print "</select>";
-
-                               $_SESSION = array();
-                       }
-               break;
-
+               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 } ?>