]> git.wh0rd.org - tt-rss.git/blobdiff - backend.php
update eclipse .settings
[tt-rss.git] / backend.php
index cee1b6f07cb18ca09634d87eb88d659cab301c5b..c9eed0e9affdc95e30a9b21eebd1bfc67350fee2 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);
        }
 
+       function __autoload($class) {
+               $file = "classes/".strtolower(basename($class)).".php";
+               if (file_exists($file)) {
+                       require $file;
+               }
+       }
+
+       $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";
@@ -40,8 +50,7 @@
 
        init_connection($link);
 
-       $op = $_REQUEST["op"];
-       $subop = $_REQUEST["subop"];
+       $method = strtolower($_REQUEST["method"]);
        $mode = $_REQUEST["mode"];
 
        if ((!$op || $op == "rss" || $op == "dlg") && !$_REQUEST["noxml"]) {
                authenticate_user($link, "admin", null);
        }
 
-       if (!($_SESSION["uid"] && validate_session($link)) && $op != "globalUpdateFeeds" &&
-                       $op != "rss" && $op != "getUnread" && $op != "getProfiles" &&
-                       $op != "fbexport" && $op != "logout" && $op != "pubsub") {
+       $public_calls = array("globalUpdateFeeds", "rss", "getUnread", "getProfiles", "share",
+               "fbexport", "logout", "pubsub");
 
-               if ($op == 'pref-feeds' && $_REQUEST['subop'] == 'add') {
+       if (array_search($op, $public_calls) !== false) {
+
+               handle_public_request($link, $op);
+               return;
+
+       } else if (!($_SESSION["uid"] && validate_session($link))) {
+               if ($op == 'pref-feeds' && $_REQUEST['method'] == 'add') {
                        header("Content-Type: text/html");
                        login_sequence($link);
                        render_login_form($link);
                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";
-       require_once "modules/pref-instances.php";
+
 
        $error = sanity_check($link);
 
                return;
        }
 
-       switch($op) { // Select action according to $op value.
-               case "rpc":
-                       // Handle remote procedure calls.
-                       handle_rpc_request($link);
-               break; // rpc
+       if (class_exists($op)) {
+               $handler = new $op($link, $_REQUEST);
 
+               if ($handler) {
+                       if ($handler->before()) {
+                               if (method_exists($handler, $method)) {
+                                       return $handler->$method();
+                               }
+                       }
+               }
+       }
+
+       switch($op) { // Select action according to $op value.
                case "feeds":
-                       $subop = $_REQUEST["subop"];
+                       $method = $_REQUEST["method"];
                        $root = (bool)$_REQUEST["root"];
 
-                       switch($subop) {
+                       switch($method) {
                                case "catchupAll":
                                        db_query($link, "UPDATE ttrss_user_entries SET
                                                last_read = NOW(),unread = false WHERE owner_uid = " . $_SESSION["uid"]);
                        $omode = db_escape_string($_REQUEST["omode"]);
 
                        $feed = db_escape_string($_REQUEST["feed"]);
-                       $subop = db_escape_string($_REQUEST["subop"]);
+                       $method = db_escape_string($_REQUEST["method"]);
                        $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 */
 
                                $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 && $feed > 0) {
+                       } 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']);
                        }
 
                        if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
 
-                       $ret = format_headlines_list($link, $feed, $subop,
+                       $ret = format_headlines_list($link, $feed, $method,
                                $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
                                $vgroup_last_feed, $override_order);
 
 
                        if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
 
-                       $headlines_unread = ccache_find($link, $returned_feed, $_SESSION["uid"],
-                                       $cat_view, true);
-
-                       if ($headlines_unread == -1) {
-                               $headlines_unread = getFeedUnread($link, $returned_feed, $cat_view);
-                       }
-
                        $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);
                                $reply['articles'] = $articles;
                        }
 
-                       if ($subop) {
-                               $reply['counters'] = getAllCounters($link, $omode, $feed);
-                       }
+//                     if ($method) {
+//                             $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 all feeds needing a update.
-                       update_daemon_common($link, 0, true, true);
-               break; // globalUpdateFeeds
-
-               case "pref-feed-browser":
-                       module_pref_feed_browser($link);
-               break; // pref-feed-browser
-
                case "pref-instances":
+                       require_once "modules/pref-instances.php";
                        module_pref_instances($link);
                break; // pref-instances
 
-               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";
-
-                       $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";
-                       }
-
-               break; // getUnread
-
                case "digestTest":
                        print_r(prepare_headlines_digest($link, $_SESSION["uid"]));
                break; // digestTest
                                "<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; // getprofiles
-
-               case "pubsub":
-                       $mode = db_escape_string($_REQUEST['hub_mode']);
-                       $feed_id = (int) db_escape_string($_REQUEST['id']);
-                       $feed_url = db_escape_string($_REQUEST['hub_topic']);
-
-                       if (!PUBSUBHUBBUB_ENABLED) {
-                               header('HTTP/1.0 404 Not Found');
-                               echo "404 Not found";
-                               return;
-                       }
-
-                       // 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);
-
-                                               db_query($link, "UPDATE ttrss_feeds SET
-                                                       last_update_started = '1970-01-01',
-                                                       last_updated = '1970-01-01' WHERE id = '$feed_id' AND
-                                                       owner_uid = ".$_SESSION["uid"]);
-
-                                       }
-                               } 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)));