Advertisement
Guest User

Untitled

a guest
Jan 15th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.79 KB | None | 0 0
  1. <?php
  2.  
  3. // v1.4.5
  4.  
  5. class FeverAPI extends Handler {
  6.  
  7.     const API_LEVEL  = 3;
  8.  
  9.     const STATUS_OK  = 1;
  10.     const STATUS_ERR = 0;
  11.  
  12.     // debugging only functions with JSON
  13.     const DEBUG = false; // enable if you need some debug output in your tinytinyrss root
  14.     const DEBUG_USER = 0; // your user id you need to debug - look it up in your mysql database and set it to a value bigger than 0
  15.     const DEBUG_FILE = './debug_fever.txt'; // the file for debugging output
  16.  
  17.     private $xml;
  18.  
  19.     // always include api_version, status as 'auth'
  20.     // output json/xml
  21.     function wrap($status, $reply)
  22.     {
  23.         $arr = array("api_version" => self::API_LEVEL,
  24.                      "auth" => $status);
  25.  
  26.         if ($status == self::STATUS_OK)
  27.         {
  28.             $arr["last_refreshed_on_time"] = $this->lastRefreshedOnTime()."";
  29.             if (!empty($reply) && is_array($reply))
  30.                 $arr = array_merge($arr, $reply);
  31.         }
  32.  
  33.         if ($this->xml)
  34.         {
  35.             print $this->array_to_xml($arr);
  36.         }
  37.         else
  38.         {
  39.             print json_encode($arr);
  40.             if (self::DEBUG) {
  41.                 // debug output
  42.                 file_put_contents(self::DEBUG_FILE,'answer   : '.json_encode($arr)."\n",FILE_APPEND);
  43.             }
  44.         }
  45.     }
  46.  
  47.     // fever supports xml wrapped in <response> tags
  48.     private function array_to_xml($array, $container = 'response', $is_root = true)
  49.     {
  50.         if (!is_array($array)) return array_to_xml(array($array));
  51.  
  52.         $xml = '';
  53.  
  54.         if ($is_root)
  55.         {
  56.             $xml .= '<?xml version="1.0" encoding="utf-8"?>';
  57.             $xml .= "<{$container}>";
  58.         }
  59.  
  60.         foreach($array as $key => $value)
  61.         {
  62.             // make sure key is a string
  63.             $elem = $key;
  64.  
  65.             if (!is_string($key) && !empty($container))
  66.             {
  67.                 $elem = $container;
  68.             }
  69.  
  70.             $xml .= "<{$elem}>";
  71.  
  72.             if (is_array($value))
  73.             {
  74.                 if (array_keys($value) !== array_keys(array_keys($value)))
  75.                 {
  76.                     $xml .= array_to_xml($value, '', false);
  77.                 }
  78.                 else
  79.                 {
  80.                     $xml .= array_to_xml($value, r('/s$/', '', $elem), false);
  81.                 }
  82.             }
  83.             else
  84.             {
  85.                 $xml .= (htmlspecialchars($value, ENT_COMPAT, 'ISO-8859-1') != $value) ? "<![CDATA[{$value}]]>" : $value;
  86.             }
  87.  
  88.             $xml .= "</{$elem}>";
  89.         }
  90.  
  91.         if ($is_root)
  92.         {
  93.             $xml .= "</{$container}>";
  94.         }
  95.  
  96.         return preg_replace('/[\x00-\x1F\x7F]/', '', $xml);
  97.     }
  98.  
  99.     // every authenticated method includes last_refreshed_on_time
  100.     private function lastRefreshedOnTime()
  101.     {
  102.         $result = $this->dbh->query("SELECT last_updated
  103.                                      FROM ttrss_feeds
  104.                                      WHERE owner_uid = " . $_SESSION["uid"] . "
  105.                                      ORDER BY last_updated DESC");
  106.  
  107.         if ($this->dbh->num_rows($result) > 0)
  108.         {
  109.             $last_refreshed_on_time = strtotime($this->dbh->fetch_result($result, 0, "last_updated"));
  110.         }
  111.         else
  112.         {
  113.             $last_refreshed_on_time = 0;
  114.         }
  115.  
  116.         return $last_refreshed_on_time;
  117.     }
  118.  
  119.     // find the user in the db with a particular api key
  120.     private function setUser()
  121.     {
  122.         $apikey = isset($_REQUEST["api_key"])?$_REQUEST["api_key"]:'';
  123.         // here comes Mr.Reader special API for logging in
  124.         if ((strlen($apikey)==0)&&
  125.             (isset($_REQUEST["action"]))&&
  126.             ($_REQUEST["action"]=='login')&&
  127.             (isset($_REQUEST["email"]))&&
  128.             (isset($_REQUEST["password"]))) {
  129.             $email = $_REQUEST["email"];
  130.             $password = $_REQUEST["password"];
  131.             $apikey = strtoupper(md5($email.":".$password));
  132.             setcookie('fever_auth',$apikey,time()+60*60*24*30);
  133.             if (self::DEBUG) {
  134.                 // debug output
  135.                 $output = array();
  136.                 $output['email'] = $email;
  137.                 $output['apikey'] = $apikey;
  138.                 file_put_contents(self::DEBUG_FILE,'auth POST: '.json_encode($output)."\n",FILE_APPEND);
  139.             }
  140.         }
  141.         if ((strlen($apikey)==0)&&isset($_COOKIE['fever_auth'])) { // override for Mr.Reader when doing some stuff
  142.             $apikey = $_COOKIE['fever_auth'];
  143.         }
  144.         if (strlen($apikey)>0)
  145.         {
  146.             $result = $this->dbh->query("SELECT owner_uid
  147.                                          FROM ttrss_plugin_storage
  148.                                          WHERE content = '".db_escape_string('a:1:{s:8:"password";s:32:"'.strtolower($apikey).'";}') . "'");
  149.  
  150.             if ($this->dbh->num_rows($result) > 0)
  151.             {
  152.                 $_SESSION["uid"] = $this->dbh->fetch_result($result, 0, "owner_uid");
  153.             }
  154.  
  155.             if (self::DEBUG_USER>0) {
  156.                 $_SESSION["uid"] = self::DEBUG_USER; // always authenticate and set debug user
  157.             }
  158.         }
  159.     }
  160.  
  161.     // set whether xml or json
  162.     private function setXml()
  163.     {
  164.         $this->xml = false;
  165.         if (isset($_REQUEST["api"]))
  166.         {
  167.             if (strtolower($_REQUEST["api"]) == "xml")
  168.                 $this->xml = true;
  169.         }
  170.     }
  171.  
  172.     private function flattenGroups(&$groupsToGroups, &$groups, &$groupsToTitle, $index)
  173.     {
  174.         foreach ($groupsToGroups[$index] as $item)
  175.         {
  176.             $id = substr($item, strpos($item, "-") + 1);
  177.             array_push($groups, array("id" => intval($id), "title" => $groupsToTitle[$id]));
  178.             if (isset($groupsToGroups[$id]))
  179.                 $this->flattenGroups($groupsToGroups, $groups, $groupsToTitle, $id);
  180.         }
  181.     }
  182.  
  183.     function getGroups()
  184.     {
  185.         // TODO: ordering of child categories etc
  186.         $groups = array();
  187.  
  188.         $result = $this->dbh->query("SELECT id, title, parent_cat
  189.                              FROM ttrss_feed_categories
  190.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'
  191.                              ORDER BY order_id ASC");
  192.  
  193.         $groupsToGroups = array();
  194.         $groupsToTitle = array();
  195.  
  196.         while ($line = $this->dbh->fetch_assoc($result))
  197.         {
  198.             if ($line["parent_cat"] === NULL)
  199.             {
  200.                 if (!isset($groupsToGroups[-1]))
  201.                 {
  202.                     $groupsToGroups[-1] = array();
  203.                 }
  204.  
  205.                 array_push($groupsToGroups[-1], $line["order_id"] . "-" . $line["id"]);
  206.             }
  207.             else
  208.             {
  209.                 if (!isset($groupsToGroups[$line["parent_cat"]]))
  210.                 {
  211.                     $groupsToGroups[$line["parent_cat"]] = array();
  212.                 }
  213.  
  214.                 array_push($groupsToGroups[$line["parent_cat"]], $line["order_id"] . "-" . $line["id"]);
  215.             }
  216.  
  217.             $groupsToTitle[$line["id"]] = $line["title"];
  218.         }
  219.  
  220.         foreach ($groupsToGroups as $key => $value)
  221.         {
  222.             sort($value);
  223.         }
  224.  
  225.         if (isset($groupsToGroups[-1]))
  226.             $this->flattenGroups($groupsToGroups, $groups, $groupsToTitle, -1);
  227.  
  228.         return $groups;
  229.     }
  230.  
  231.     function getFeeds()
  232.     {
  233.         $feeds = array();
  234.  
  235.         $result = $this->dbh->query("SELECT id, title, feed_url, site_url, last_updated
  236.                              FROM ttrss_feeds
  237.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'
  238.                              ORDER BY order_id ASC");
  239.  
  240.         while ($line = $this->dbh->fetch_assoc($result))
  241.         {
  242.             array_push($feeds, array("id" => intval($line["id"]),
  243.                                      "favicon_id" => intval($line["id"]),
  244.                                      "title" => $line["title"],
  245.                                      "url" => $line["feed_url"],
  246.                                      "site_url" => $line["site_url"],
  247.                                      "is_spark" => 0, // unsported
  248.                                      "last_updated_on_time" => strtotime($line["last_updated"])
  249.                     ));
  250.         }
  251.         return $feeds;
  252.     }
  253.  
  254.     function getFavicons()
  255.     {
  256.         $favicons = array();
  257.  
  258.         $result = $this->dbh->query("SELECT id
  259.                              FROM ttrss_feeds
  260.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'
  261.                              ORDER BY order_id ASC");
  262.  
  263.         // data = "image/gif;base64,<base64 encoded image>
  264.         while ($line = $this->dbh->fetch_assoc($result))
  265.         {
  266.             $filename = "feed-icons/" . $line["id"] . ".ico";
  267.             if (file_exists($filename))
  268.             {
  269.                 array_push($favicons, array("id" => intval($line["id"]),
  270.                                             "data" => image_type_to_mime_type(exif_imagetype($filename)) . ";base64," . base64_encode(file_get_contents($filename))
  271.                           ));
  272.             }
  273.         }
  274.  
  275.         return $favicons;
  276.     }
  277.  
  278.     function getLinks()
  279.     {
  280.         // TODO: is there a 'hot links' alternative in ttrss?
  281.         // use ttrss_user_entries / score>0
  282.         $links = array();
  283.  
  284.         return $links;
  285.     }
  286.  
  287.     function getItems()
  288.     {
  289.         // items from specific groups, feeds
  290.         $items = array();
  291.  
  292.         $item_limit = 50;
  293.         $where = " owner_uid = '" . db_escape_string($_SESSION["uid"]) . "' AND ref_id = id ";
  294.  
  295.         if (isset($_REQUEST["feed_ids"]) || isset($_REQUEST["group_ids"])) // added 0.3
  296.         {
  297.             $feed_ids = array();
  298.             if (isset($_REQUEST["feed_ids"]))
  299.             {
  300.                 $feed_ids = explode(",", $_REQUEST["feed_ids"]);
  301.             }
  302.             if (isset($_REQUEST["group_ids"]))
  303.             {
  304.                 $group_ids = explode(",", $_REQUEST["group_ids"]);
  305.                 $num_group_ids = sizeof($group_ids);
  306.                 $groups_query = " AND cat_id IN (";
  307.                 foreach ($group_ids as $group_id)
  308.                 {
  309.                     if (is_numeric($group_id))
  310.                         $groups_query .= db_escape_string(intval($group_id)) . ",";
  311.                     else
  312.                         $num_group_ids--;
  313.                 }
  314.                 if ($num_group_ids <= 0)
  315.                     $groups_query = " AND cat_id IN ('') ";
  316.                 else
  317.                     $groups_query = trim($groups_query, ",") . ")";
  318.  
  319.                 $feeds_in_group_result = $this->dbh->query("SELECT id
  320.                                                             FROM ttrss_feeds
  321.                                                             WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "' " . $groups_query);
  322.  
  323.                 $group_feed_ids = array();
  324.                 while ($line = $this->dbh->fetch_assoc($feeds_in_group_result))
  325.                 {
  326.                     array_push($group_feed_ids, $line["id"]);
  327.                 }
  328.  
  329.                 $feed_ids = array_unique(array_merge($feed_ids, $group_feed_ids));
  330.             }
  331.  
  332.             $query = " feed_id IN (";
  333.             $num_feed_ids = sizeof($feed_ids);
  334.             foreach ($feed_ids as $feed_id)
  335.             {
  336.                 if (is_numeric($feed_id))
  337.                     $query.= db_escape_string(intval($feed_id)) . ",";
  338.                 else
  339.                     $num_feed_ids--;
  340.             }
  341.  
  342.             if ($num_feed_ids <= 0)
  343.                 $query = " feed_id IN ('') ";
  344.             else
  345.                 $query = trim($query, ",") . ")";
  346.  
  347.             if (!empty($where)) $where .= " AND ";
  348.             $where .= $query;
  349.         }
  350.  
  351.         if (isset($_REQUEST["max_id"])) // descending from most recently added
  352.         {
  353.             // use the max_id argument to request the previous $item_limit items
  354.             if (is_numeric($_REQUEST["max_id"]))
  355.             {
  356.                 $max_id = ($_REQUEST["max_id"] > 0) ? intval($_REQUEST["max_id"]) : 0;
  357.                 if ($max_id)
  358.                 {
  359.                     if (!empty($where)) $where .= " AND ";
  360.                     $where .= "id < " . db_escape_string($max_id) . " ";
  361.                 }
  362.                 else if (empty($where))
  363.                 {
  364.                     $where .= "1";
  365.                 }
  366.  
  367.                 $where .= " ORDER BY id DESC";
  368.             }
  369.         }
  370.         else if (isset($_REQUEST["with_ids"])) // selective
  371.         {
  372.             if (!empty($where)) $where .= " AND "; // group_ids & feed_ids don't make sense with this query but just in case
  373.  
  374.             $item_ids = explode(",", $_REQUEST["with_ids"]);
  375.             $query = "id IN (";
  376.             $num_ids = sizeof($item_ids);
  377.             foreach ($item_ids as $item_id)
  378.             {
  379.                 if (is_numeric($item_id))
  380.                     $query .= db_escape_string(intval($item_id)) . ",";
  381.                 else
  382.                     $num_ids--;
  383.             }
  384.  
  385.             if ($num_ids <= 0)
  386.                 $query = "id IN ('') ";
  387.             else
  388.                 $query = trim($query, ",") . ") ";
  389.  
  390.             $where .= $query;
  391.         }
  392.         else // ascending from first added
  393.         {
  394.             if (is_numeric($_REQUEST["since_id"]))
  395.             {
  396.                 // use the since_id argument to request the next $item_limit items
  397.                 $since_id   = isset($_GET["since_id"]) ? intval($_GET["since_id"]) : 0;
  398.  
  399.                 if ($since_id)
  400.                 {
  401.                     if (!empty($where)) $where .= " AND ";
  402.                     //$where .= "id > " . db_escape_string($since_id) . " ";
  403.                     $where .= "id > " . db_escape_string($since_id) . " "; // NASTY hack for Mr. Reader 2.0 on iOS and TinyTiny RSS Fever
  404.                 }
  405.                 else if (empty($where))
  406.                 {
  407.                     $where .= "1";
  408.                 }
  409.  
  410.                 $where .= " ORDER BY id ASC";
  411.             }
  412.         }
  413.  
  414.         $where .= " LIMIT " . $item_limit;
  415.  
  416.         // id, feed_id, title, author, html, url, is_saved, is_read, created_on_time
  417.         $result = $this->dbh->query("SELECT ref_id, feed_id, title, link, content, id, marked, unread, author, updated
  418.                                      FROM ttrss_entries, ttrss_user_entries
  419.                                      WHERE " . $where);
  420.  
  421.         while ($line = $this->dbh->fetch_assoc($result))
  422.         {
  423.             array_push($items, array("id" => intval($line["id"]),
  424.                                      "feed_id" => intval($line["feed_id"]),
  425.                                      "title" => $line["title"],
  426.                                      "author" => $line["author"],
  427.                                      "html" => $line["content"],
  428.                                      "url" => $line["link"],
  429.                                      "is_saved" => (sql_bool_to_bool($line["marked"]) ? 1 : 0),
  430.                                      "is_read" => ( (!sql_bool_to_bool($line["unread"])) ? 1 : 0),
  431.                                      "created_on_time" => strtotime($line["updated"])
  432.                     ));
  433.         }
  434.  
  435.         return $items;
  436.     }
  437.  
  438.     function getTotalItems()
  439.     {
  440.         // number of total items
  441.         $total_items = 0;
  442.  
  443.         $where = " owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'";
  444.         $result = $this->dbh->query("SELECT COUNT(ref_id) as total_items
  445.                                      FROM ttrss_user_entries
  446.                                      WHERE " . $where);
  447.  
  448.         if ($this->dbh->num_rows($result) > 0)
  449.         {
  450.             $total_items = $this->dbh->fetch_result($result, 0, "total_items");
  451.         }
  452.  
  453.         return $total_items;
  454.     }
  455.  
  456.     function getFeedsGroup()
  457.     {
  458.         $feeds_groups = array();
  459.  
  460.         $result = $this->dbh->query("SELECT id, cat_id
  461.                              FROM ttrss_feeds
  462.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'
  463.                              AND cat_id IS NOT NULL
  464.                              ORDER BY id ASC");
  465.  
  466.         $groupsToFeeds = array();
  467.  
  468.         while ($line = $this->dbh->fetch_assoc($result))
  469.         {
  470.             if (!array_key_exists($line["cat_id"], $groupsToFeeds))
  471.                 $groupsToFeeds[$line["cat_id"]] = array();
  472.  
  473.             array_push($groupsToFeeds[$line["cat_id"]], $line["id"]);
  474.         }
  475.  
  476.         foreach ($groupsToFeeds as $group => $feeds)
  477.         {
  478.             $feedsStr = "";
  479.             foreach ($feeds as $feed)
  480.                 $feedsStr .= $feed . ",";
  481.             $feedsStr = trim($feedsStr, ",");
  482.  
  483.             array_push($feeds_groups, array("group_id" => $group,
  484.                                             "feed_ids" => $feedsStr));
  485.         }
  486.         return $feeds_groups;
  487.     }
  488.  
  489.     function getUnreadItemIds()
  490.     {
  491.         $unreadItemIdsCSV = "";
  492.         $result = $this->dbh->query("SELECT ref_id
  493.                              FROM ttrss_user_entries
  494.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'" . "AND unread"); // ORDER BY red_id DESC
  495.  
  496.         while ($line = $this->dbh->fetch_assoc($result))
  497.         {
  498.             $unreadItemIdsCSV .= $line["ref_id"] . ",";
  499.         }
  500.         $unreadItemIdsCSV = trim($unreadItemIdsCSV, ",");
  501.  
  502.         return $unreadItemIdsCSV;
  503.     }
  504.  
  505.     function getSavedItemIds()
  506.     {
  507.         $savedItemIdsCSV = "";
  508.         $result = $this->dbh->query("SELECT ref_id
  509.                              FROM ttrss_user_entries
  510.                              WHERE owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'" . "AND marked OR published OR feed_id IS NULL OR uuid != ''");
  511.  
  512.         while ($line = $this->dbh->fetch_assoc($result))
  513.         {
  514.             $savedItemIdsCSV .= $line["ref_id"] . ",";
  515.         }
  516.         $savedItemIdsCSV = trim($savedItemIdsCSV, ",");
  517.  
  518.         return $savedItemIdsCSV;
  519.     }
  520.  
  521.     function setItem($id, $field_raw, $mode, $before = 0)
  522.     {
  523.         $field = "";
  524.         $set_to = "";
  525.  
  526.         switch ($field_raw) {
  527.             case 0:
  528.                 $field = "marked";
  529.                 $additional_fields = ",last_marked = NOW()";
  530.                 break;
  531.             case 1:
  532.                 $field = "unread";
  533.                 $additional_fields = ",last_read = NOW()";
  534.                 break;
  535.         };
  536.  
  537.         switch ($mode) {
  538.             case 1:
  539.                 $set_to = "true";
  540.                 break;
  541.             case 0:
  542.                 $set_to = "false";
  543.                 break;
  544.         }
  545.  
  546.         if ($field && $set_to)
  547.         {
  548.             $article_ids = db_escape_string($id);
  549.  
  550.             $result = $this->dbh->query("UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = '" . db_escape_string($_SESSION["uid"]) . "'");
  551.  
  552.             $num_updated = $this->dbh->affected_rows($result);
  553.  
  554.             if ($num_updated > 0 && $field == "unread") {
  555.                 $result = $this->dbh->query("SELECT DISTINCT feed_id FROM ttrss_user_entries
  556.                     WHERE ref_id IN ($article_ids)");
  557.  
  558.                 while ($line = $this->dbh->fetch_assoc($result)) {
  559.                     ccache_update($line["feed_id"], $_SESSION["uid"]);
  560.                 }
  561.             }
  562.         }
  563.     }
  564.  
  565.     function setItemAsRead($id)
  566.     {
  567.         $this->setItem($id, 1, 0);
  568.     }
  569.  
  570.     function setItemAsUnread($id)
  571.     {
  572.         $this->setItem($id, 1, 1);
  573.     }
  574.  
  575.     function setItemAsSaved($id)
  576.     {
  577.         $this->setItem($id, 0, 1);
  578.     }
  579.  
  580.     function setItemAsUnsaved($id)
  581.     {
  582.         $this->setItem($id, 0, 0);
  583.     }
  584.  
  585.     function setFeed($id, $cat, $before=0)
  586.     {
  587.         // if before is zero, set it to now so feeds all items are read from before this point in time
  588.         if ($before == 0)
  589.             $before = time();
  590.  
  591.         if (is_numeric($id))
  592.         {
  593.             // this is a category
  594.             if ($cat)
  595.             {
  596.                 // if not special feed
  597.                 if ($id > 0)
  598.                 {
  599.                     db_query("UPDATE ttrss_user_entries
  600.                         SET unread = false, last_read = NOW() WHERE ref_id IN
  601.                             (SELECT id FROM
  602.                                 (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  603.                                     AND owner_uid = '" . db_escape_string($_SESSION["uid"]) . "' AND unread = true AND feed_id IN
  604.                                         (SELECT id FROM ttrss_feeds WHERE cat_id IN (" . intval($id) . ")) AND date_entered < '" . date("Y-m-d H:i:s", $before) . "' ) as tmp)");
  605.  
  606.                 }
  607.                 // this is "all" to fever, but internally "all" is -4
  608.                 else if ($id == 0)
  609.                 {
  610.                     $id = -4;
  611.                     db_query("UPDATE ttrss_user_entries
  612.                             SET unread = false, last_read = NOW() WHERE ref_id IN
  613.                                 (SELECT id FROM
  614.                                     (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  615.                                         AND owner_uid = '" . db_escape_string($_SESSION["uid"]) . "' AND unread = true AND date_entered < '" . date("Y-m-d H:i:s", $before) . "' ) as tmp)");
  616.                 }
  617.             }
  618.             // not a category
  619.             else if ($id > 0)
  620.             {
  621.                 db_query("UPDATE ttrss_user_entries
  622.                     SET unread = false, last_read = NOW() WHERE ref_id IN
  623.                         (SELECT id FROM
  624.                             (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  625.                                 AND owner_uid = '" . db_escape_string($_SESSION["uid"]) . "' AND unread = true AND feed_id = " . intval($id) . " AND date_entered < '" . date("Y-m-d H:i:s", $before) . "' ) as tmp)");
  626.  
  627.             }
  628.             ccache_update($id,$_SESSION["uid"], $cat);
  629.         }
  630.     }
  631.  
  632.     function setFeedAsRead($id, $before)
  633.     {
  634.         $this->setFeed($id, false, $before);
  635.     }
  636.  
  637.     function setGroupAsRead($id, $before)
  638.     {
  639.         $this->setFeed($id, true, $before);
  640.     }
  641.  
  642.     // this does all the processing, since the fever api does not have a specific variable that specifies the operation
  643.     function index()
  644.     {
  645.         $response_arr = array();
  646.  
  647.         if (isset($_REQUEST["groups"]))
  648.         {
  649.             $response_arr["groups"] = $this->getGroups();
  650.             $response_arr["feeds_groups"] = $this->getFeedsGroup();
  651.         }
  652.         if (isset($_REQUEST["feeds"]))
  653.         {
  654.             $response_arr["feeds"] = $this->getFeeds();
  655.             $response_arr["feeds_groups"] = $this->getFeedsGroup();
  656.         }
  657.         // TODO: favicon support
  658.         if (isset($_REQUEST["favicons"]))
  659.         {
  660.             $response_arr["favicons"] = $this->getFavicons();
  661.         }
  662.         if (isset($_REQUEST["items"]))
  663.         {
  664.             $response_arr["total_items"] = $this->getTotalItems();
  665.             $response_arr["items"] = $this->getItems();
  666.         }
  667.         if (isset($_REQUEST["links"]))
  668.         {
  669.             $response_arr["links"] = $this->getLinks();
  670.         }
  671.         if (isset($_REQUEST["unread_item_ids"]))
  672.         {
  673.             $response_arr["unread_item_ids"] = $this->getUnreadItemIds();
  674.         }
  675.         if (isset($_REQUEST["saved_item_ids"]))
  676.         {
  677.             $response_arr["saved_item_ids"] = $this->getSavedItemIds();
  678.         }
  679.  
  680.         if (isset($_REQUEST["mark"], $_REQUEST["as"], $_REQUEST["id"]))
  681.         {
  682.             if (is_numeric($_REQUEST["id"]))
  683.             {
  684.                 $before = (isset($_REQUEST["before"])) ? $_REQUEST["before"] : null;
  685.                 $method_name = "set" . ucfirst($_REQUEST["mark"]) . "As" . ucfirst($_REQUEST["as"]);
  686.  
  687.                 if (method_exists($this, $method_name))
  688.                 {
  689.                     $id = intval($_REQUEST["id"]);
  690.                     $this->{$method_name}($id, $before);
  691.                     switch($_REQUEST["as"])
  692.                     {
  693.                         case "read":
  694.                         case "unread":
  695.                             $response_arr["unread_item_ids"] = $this->getUnreadItemIds();
  696.                         break;
  697.  
  698.                         case 'saved':
  699.                         case 'unsaved':
  700.                             $response_arr["saved_item_ids"] = $this->getSavedItemIds();
  701.                         break;
  702.                     }
  703.                 }
  704.             }
  705.         }
  706.  
  707.         if ($_SESSION["uid"])
  708.             $this->wrap(self::STATUS_OK, $response_arr);
  709.         else if (!$_SESSION["uid"])
  710.             $this->wrap(self::STATUS_ERR, NULL);
  711.  
  712.     }
  713.  
  714.     // validate the api_key, user preferences
  715.     function before($method) {
  716.         if (parent::before($method)) {
  717.             if (self::DEBUG) {
  718.                 // add request to debug log
  719.                 file_put_contents(self::DEBUG_FILE,'parameter: '.json_encode($_REQUEST)."\n",FILE_APPEND);
  720.             }
  721.  
  722.             // set the user from the db
  723.             $this->setUser();
  724.  
  725.             // are we xml or json?
  726.             $this->setXml();
  727.  
  728.             if ($this->xml)
  729.                 header("Content-Type: text/xml");
  730.             else
  731.                 header("Content-Type: application/json");
  732.  
  733.             // check we have a valid user
  734.             if (!$_SESSION["uid"]) {
  735.                 $this->wrap(self::STATUS_ERR, NULL);
  736.                 return false;
  737.             }
  738.  
  739.             // check if user has api access enabled
  740.             if ($_SESSION["uid"] && !get_pref('ENABLE_API_ACCESS')) {
  741.                 $this->wrap(self::STATUS_ERR, NULL);
  742.                 return false;
  743.             }
  744.  
  745.             return true;
  746.         }
  747.         return false;
  748.     }
  749. }
  750.  
  751. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement