- if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
- print json_encode(array("error" => 'API_DISABLED'));
- return;
- }
-
- switch ($op) {
- case "getVersion":
- $rv = array("version" => VERSION);
- print json_encode($rv);
- break;
- case "login":
- $login = db_escape_string($_REQUEST["user"]);
- $password = db_escape_string($_REQUEST["password"]);
-
- $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
-
- if (db_num_rows($result) != 0) {
- $uid = db_fetch_result($result, 0, "id");
- } else {
- $uid = 0;
- }
-
- if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
- if (authenticate_user($link, $login, $password)) {
- print json_encode(array("uid" => $_SESSION["uid"]));
- } else {
- print json_encode(array("error" => "LOGIN_ERROR"));
- }
- } else {
- print json_encode(array("error" => "API_DISABLED"));
- }
-
- break;
- case "logout":
- logout_user();
- print json_encode(array("uid" => 0));
- break;
- case "isLoggedIn":
- print json_encode(array("status" => $_SESSION["uid"] != ''));
- break;
- case "getUnread":
- $feed_id = db_escape_string($_REQUEST["feed_id"]);
- $is_cat = db_escape_string($_REQUEST["is_cat"]);
-
- if ($feed_id) {
- print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
- } else {
- print json_encode(array("unread" => getGlobalUnread($link)));
- }
- break;
- case "getFeeds":
- $cat_id = db_escape_string($_REQUEST["cat_id"]);
- $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
-
- if (!$cat_id) {
- $result = db_query($link, "SELECT
- id, feed_url, cat_id, title, ".
- SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
- FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
- } else {
- $result = db_query($link, "SELECT
- id, feed_url, cat_id, title, ".
- SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
- FROM ttrss_feeds WHERE
- cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
- }
-
- $feeds = array();
-
- while ($line = db_fetch_assoc($result)) {
-
- $unread = getFeedUnread($link, $line["id"]);
-
- if ($unread || !$unread_only) {
-
- $row = array(
- "feed_url" => $line["feed_url"],
- "title" => $line["title"],
- "id" => (int)$line["id"],
- "unread" => (int)$unread,
- "cat_id" => (int)$line["cat_id"],
- "last_updated" => strtotime($line["last_updated"])
- );
-
- array_push($feeds, $row);
- }
- }
-
- /* Labels */
-
- if (!$cat_id || $cat_id == -2) {
- $counters = getLabelCounters($link, false, true);
-
- foreach (array_keys($counters) as $id) {
-
- $unread = $counters[$id]["counter"];
-
- if ($unread || !$unread_only) {
-
- $row = array(
- "id" => $id,
- "title" => $counters[$id]["description"],
- "unread" => $counters[$id]["counter"],
- "cat_id" => -2,
- );
-
- array_push($feeds, $row);
- }
- }
- }
-
- /* Virtual feeds */
-
- if (!$cat_id || $cat_id == -1) {
- foreach (array(-1, -2, -3, -4) as $i) {
- $unread = getFeedUnread($link, $i);
-
- if ($unread || !$unread_only) {
- $title = getFeedTitle($link, $i);
-
- $row = array(
- "id" => $i,
- "title" => $title,
- "unread" => $unread,
- "cat_id" => -1,
- );
- array_push($feeds, $row);
- }
-
- }
- }
-
- print json_encode($feeds);