api.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. class API extends Handler {
  3. const API_LEVEL = 6;
  4. const STATUS_OK = 0;
  5. const STATUS_ERR = 1;
  6. private $seq;
  7. function before($method) {
  8. if (parent::before($method)) {
  9. header("Content-Type: text/json");
  10. if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
  11. $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
  12. return false;
  13. }
  14. if ($_SESSION["uid"] && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) {
  15. $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
  16. return false;
  17. }
  18. $this->seq = (int) $_REQUEST['seq'];
  19. return true;
  20. }
  21. return false;
  22. }
  23. function wrap($status, $reply) {
  24. print json_encode(array("seq" => $this->seq,
  25. "status" => $status,
  26. "content" => $reply));
  27. }
  28. function getVersion() {
  29. $rv = array("version" => VERSION);
  30. $this->wrap(self::STATUS_OK, $rv);
  31. }
  32. function getApiLevel() {
  33. $rv = array("level" => self::API_LEVEL);
  34. $this->wrap(self::STATUS_OK, $rv);
  35. }
  36. function login() {
  37. @session_destroy();
  38. @session_start();
  39. $login = $this->dbh->escape_string($_REQUEST["user"]);
  40. $password = $_REQUEST["password"];
  41. $password_base64 = base64_decode($_REQUEST["password"]);
  42. if (SINGLE_USER_MODE) $login = "admin";
  43. $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE login = '$login'");
  44. if ($this->dbh->num_rows($result) != 0) {
  45. $uid = $this->dbh->fetch_result($result, 0, "id");
  46. } else {
  47. $uid = 0;
  48. }
  49. if (!$uid) {
  50. $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
  51. return;
  52. }
  53. if (get_pref("ENABLE_API_ACCESS", $uid)) {
  54. if (authenticate_user($login, $password)) { // try login with normal password
  55. $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
  56. "api_level" => self::API_LEVEL));
  57. } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password
  58. $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
  59. "api_level" => self::API_LEVEL));
  60. } else { // else we are not logged in
  61. $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
  62. }
  63. } else {
  64. $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
  65. }
  66. }
  67. function logout() {
  68. logout_user();
  69. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  70. }
  71. function isLoggedIn() {
  72. $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
  73. }
  74. function getUnread() {
  75. $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
  76. $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]);
  77. if ($feed_id) {
  78. $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat)));
  79. } else {
  80. $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread()));
  81. }
  82. }
  83. /* Method added for ttrss-reader for Android */
  84. function getCounters() {
  85. $this->wrap(self::STATUS_OK, getAllCounters());
  86. }
  87. function getFeeds() {
  88. $cat_id = $this->dbh->escape_string($_REQUEST["cat_id"]);
  89. $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
  90. $limit = (int) $this->dbh->escape_string($_REQUEST["limit"]);
  91. $offset = (int) $this->dbh->escape_string($_REQUEST["offset"]);
  92. $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
  93. $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested);
  94. $this->wrap(self::STATUS_OK, $feeds);
  95. }
  96. function getCategories() {
  97. $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
  98. $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]);
  99. $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
  100. // TODO do not return empty categories, return Uncategorized and standard virtual cats
  101. if ($enable_nested)
  102. $nested_qpart = "parent_cat IS NULL";
  103. else
  104. $nested_qpart = "true";
  105. $result = $this->dbh->query("SELECT
  106. id, title, order_id, (SELECT COUNT(id) FROM
  107. ttrss_feeds WHERE
  108. ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds,
  109. (SELECT COUNT(id) FROM
  110. ttrss_feed_categories AS c2 WHERE
  111. c2.parent_cat = ttrss_feed_categories.id) AS num_cats
  112. FROM ttrss_feed_categories
  113. WHERE $nested_qpart AND owner_uid = " .
  114. $_SESSION["uid"]);
  115. $cats = array();
  116. while ($line = $this->dbh->fetch_assoc($result)) {
  117. if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) {
  118. $unread = getFeedUnread($line["id"], true);
  119. if ($enable_nested)
  120. $unread += getCategoryChildrenUnread($line["id"]);
  121. if ($unread || !$unread_only) {
  122. array_push($cats, array("id" => $line["id"],
  123. "title" => $line["title"],
  124. "unread" => $unread,
  125. "order_id" => (int) $line["order_id"],
  126. ));
  127. }
  128. }
  129. }
  130. foreach (array(-2,-1,0) as $cat_id) {
  131. if ($include_empty || !$this->isCategoryEmpty($cat_id)) {
  132. $unread = getFeedUnread($cat_id, true);
  133. if ($unread || !$unread_only) {
  134. array_push($cats, array("id" => $cat_id,
  135. "title" => getCategoryTitle($cat_id),
  136. "unread" => $unread));
  137. }
  138. }
  139. }
  140. $this->wrap(self::STATUS_OK, $cats);
  141. }
  142. function getHeadlines() {
  143. $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
  144. if ($feed_id != "") {
  145. $limit = (int)$this->dbh->escape_string($_REQUEST["limit"]);
  146. if (!$limit || $limit >= 200) $limit = 200;
  147. $offset = (int)$this->dbh->escape_string($_REQUEST["skip"]);
  148. $filter = $this->dbh->escape_string($_REQUEST["filter"]);
  149. $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
  150. $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]);
  151. $show_content = sql_bool_to_bool($_REQUEST["show_content"]);
  152. /* all_articles, unread, adaptive, marked, updated */
  153. $view_mode = $this->dbh->escape_string($_REQUEST["view_mode"]);
  154. $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]);
  155. $since_id = (int)$this->dbh->escape_string($_REQUEST["since_id"]);
  156. $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
  157. $sanitize_content = true;
  158. $override_order = false;
  159. switch ($_REQUEST["order_by"]) {
  160. case "date_reverse":
  161. $override_order = "date_entered, updated";
  162. break;
  163. case "feed_dates":
  164. $override_order = "updated DESC";
  165. break;
  166. }
  167. /* do not rely on params below */
  168. $search = $this->dbh->escape_string($_REQUEST["search"]);
  169. $search_mode = $this->dbh->escape_string($_REQUEST["search_mode"]);
  170. $headlines = $this->api_get_headlines($feed_id, $limit, $offset,
  171. $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
  172. $include_attachments, $since_id, $search, $search_mode,
  173. $include_nested, $sanitize_content);
  174. $this->wrap(self::STATUS_OK, $headlines);
  175. } else {
  176. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  177. }
  178. }
  179. function updateArticle() {
  180. $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  181. $mode = (int) $this->dbh->escape_string($_REQUEST["mode"]);
  182. $data = $this->dbh->escape_string($_REQUEST["data"]);
  183. $field_raw = (int)$this->dbh->escape_string($_REQUEST["field"]);
  184. $field = "";
  185. $set_to = "";
  186. switch ($field_raw) {
  187. case 0:
  188. $field = "marked";
  189. $additional_fields = ",last_marked = NOW()";
  190. break;
  191. case 1:
  192. $field = "published";
  193. $additional_fields = ",last_published = NOW()";
  194. break;
  195. case 2:
  196. $field = "unread";
  197. $additional_fields = ",last_read = NOW()";
  198. break;
  199. case 3:
  200. $field = "note";
  201. };
  202. switch ($mode) {
  203. case 1:
  204. $set_to = "true";
  205. break;
  206. case 0:
  207. $set_to = "false";
  208. break;
  209. case 2:
  210. $set_to = "NOT $field";
  211. break;
  212. }
  213. if ($field == "note") $set_to = "'$data'";
  214. if ($field && $set_to && count($article_ids) > 0) {
  215. $article_ids = join(", ", $article_ids);
  216. $result = $this->dbh->query("UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
  217. $num_updated = $this->dbh->affected_rows($result);
  218. if ($num_updated > 0 && $field == "unread") {
  219. $result = $this->dbh->query("SELECT DISTINCT feed_id FROM ttrss_user_entries
  220. WHERE ref_id IN ($article_ids)");
  221. while ($line = $this->dbh->fetch_assoc($result)) {
  222. ccache_update($line["feed_id"], $_SESSION["uid"]);
  223. }
  224. }
  225. if ($num_updated > 0 && $field == "published") {
  226. if (PUBSUBHUBBUB_HUB) {
  227. $rss_link = get_self_url_prefix() .
  228. "/public.php?op=rss&id=-2&key=" .
  229. get_feed_access_key(-2, false);
  230. $p = new Publisher(PUBSUBHUBBUB_HUB);
  231. $pubsub_result = $p->publish_update($rss_link);
  232. }
  233. }
  234. $this->wrap(self::STATUS_OK, array("status" => "OK",
  235. "updated" => $num_updated));
  236. } else {
  237. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  238. }
  239. }
  240. function getArticle() {
  241. $article_id = join(",", array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_id"])), is_numeric));
  242. $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
  243. marked,unread,published,score,
  244. ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
  245. author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
  246. FROM ttrss_entries,ttrss_user_entries
  247. WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
  248. $_SESSION["uid"] ;
  249. $result = $this->dbh->query($query);
  250. $articles = array();
  251. if ($this->dbh->num_rows($result) != 0) {
  252. while ($line = $this->dbh->fetch_assoc($result)) {
  253. $attachments = get_article_enclosures($line['id']);
  254. $article = array(
  255. "id" => $line["id"],
  256. "title" => $line["title"],
  257. "link" => $line["link"],
  258. "labels" => get_article_labels($line['id']),
  259. "unread" => sql_bool_to_bool($line["unread"]),
  260. "marked" => sql_bool_to_bool($line["marked"]),
  261. "published" => sql_bool_to_bool($line["published"]),
  262. "comments" => $line["comments"],
  263. "author" => $line["author"],
  264. "updated" => (int) strtotime($line["updated"]),
  265. "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"],
  266. "feed_id" => $line["feed_id"],
  267. "attachments" => $attachments,
  268. "score" => (int)$line["score"],
  269. "feed_title" => $line["feed_title"]
  270. );
  271. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
  272. $article = $p->hook_render_article_api(array("article" => $article));
  273. }
  274. array_push($articles, $article);
  275. }
  276. }
  277. $this->wrap(self::STATUS_OK, $articles);
  278. }
  279. function getConfig() {
  280. $config = array(
  281. "icons_dir" => ICONS_DIR,
  282. "icons_url" => ICONS_URL);
  283. $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
  284. $result = $this->dbh->query("SELECT COUNT(*) AS cf FROM
  285. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  286. $num_feeds = $this->dbh->fetch_result($result, 0, "cf");
  287. $config["num_feeds"] = (int)$num_feeds;
  288. $this->wrap(self::STATUS_OK, $config);
  289. }
  290. function updateFeed() {
  291. require_once "include/rssfuncs.php";
  292. $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
  293. update_rss_feed($feed_id, true);
  294. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  295. }
  296. function catchupFeed() {
  297. $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
  298. $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]);
  299. catchup_feed($feed_id, $is_cat);
  300. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  301. }
  302. function getPref() {
  303. $pref_name = $this->dbh->escape_string($_REQUEST["pref_name"]);
  304. $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
  305. }
  306. function getLabels() {
  307. //$article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  308. $article_id = (int)$_REQUEST['article_id'];
  309. $rv = array();
  310. $result = $this->dbh->query("SELECT id, caption, fg_color, bg_color
  311. FROM ttrss_labels2
  312. WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
  313. if ($article_id)
  314. $article_labels = get_article_labels($article_id);
  315. else
  316. $article_labels = array();
  317. while ($line = $this->dbh->fetch_assoc($result)) {
  318. $checked = false;
  319. foreach ($article_labels as $al) {
  320. if ($al[0] == $line['id']) {
  321. $checked = true;
  322. break;
  323. }
  324. }
  325. array_push($rv, array(
  326. "id" => (int)$line['id'],
  327. "caption" => $line['caption'],
  328. "fg_color" => $line['fg_color'],
  329. "bg_color" => $line['bg_color'],
  330. "checked" => $checked));
  331. }
  332. $this->wrap(self::STATUS_OK, $rv);
  333. }
  334. function setArticleLabel() {
  335. $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  336. $label_id = (int) $this->dbh->escape_string($_REQUEST['label_id']);
  337. $assign = (bool) $this->dbh->escape_string($_REQUEST['assign']) == "true";
  338. $label = $this->dbh->escape_string(label_find_caption(
  339. $label_id, $_SESSION["uid"]));
  340. $num_updated = 0;
  341. if ($label) {
  342. foreach ($article_ids as $id) {
  343. if ($assign)
  344. label_add_article($id, $label, $_SESSION["uid"]);
  345. else
  346. label_remove_article($id, $label, $_SESSION["uid"]);
  347. ++$num_updated;
  348. }
  349. }
  350. $this->wrap(self::STATUS_OK, array("status" => "OK",
  351. "updated" => $num_updated));
  352. }
  353. function index($method) {
  354. $plugin = PluginHost::getInstance()->get_api_method(strtolower($method));
  355. if ($plugin && method_exists($plugin, $method)) {
  356. $reply = $plugin->$method();
  357. $this->wrap($reply[0], $reply[1]);
  358. } else {
  359. $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
  360. }
  361. }
  362. function shareToPublished() {
  363. $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"]));
  364. $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
  365. $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"]));
  366. if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) {
  367. $this->wrap(self::STATUS_OK, array("status" => 'OK'));
  368. } else {
  369. $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
  370. }
  371. }
  372. static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
  373. $feeds = array();
  374. /* Labels */
  375. if ($cat_id == -4 || $cat_id == -2) {
  376. $counters = getLabelCounters(true);
  377. foreach (array_values($counters) as $cv) {
  378. $unread = $cv["counter"];
  379. if ($unread || !$unread_only) {
  380. $row = array(
  381. "id" => $cv["id"],
  382. "title" => $cv["description"],
  383. "unread" => $cv["counter"],
  384. "cat_id" => -2,
  385. );
  386. array_push($feeds, $row);
  387. }
  388. }
  389. }
  390. /* Virtual feeds */
  391. if ($cat_id == -4 || $cat_id == -1) {
  392. foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
  393. $unread = getFeedUnread($i);
  394. if ($unread || !$unread_only) {
  395. $title = getFeedTitle($i);
  396. $row = array(
  397. "id" => $i,
  398. "title" => $title,
  399. "unread" => $unread,
  400. "cat_id" => -1,
  401. );
  402. array_push($feeds, $row);
  403. }
  404. }
  405. }
  406. /* Child cats */
  407. if ($include_nested && $cat_id) {
  408. $result = db_query("SELECT
  409. id, title FROM ttrss_feed_categories
  410. WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
  411. " ORDER BY id, title");
  412. while ($line = db_fetch_assoc($result)) {
  413. $unread = getFeedUnread($line["id"], true) +
  414. getCategoryChildrenUnread($line["id"]);
  415. if ($unread || !$unread_only) {
  416. $row = array(
  417. "id" => $line["id"],
  418. "title" => $line["title"],
  419. "unread" => $unread,
  420. "is_cat" => true,
  421. );
  422. array_push($feeds, $row);
  423. }
  424. }
  425. }
  426. /* Real feeds */
  427. if ($limit) {
  428. $limit_qpart = "LIMIT $limit OFFSET $offset";
  429. } else {
  430. $limit_qpart = "";
  431. }
  432. if ($cat_id == -4 || $cat_id == -3) {
  433. $result = db_query("SELECT
  434. id, feed_url, cat_id, title, order_id, ".
  435. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  436. FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
  437. " ORDER BY cat_id, title " . $limit_qpart);
  438. } else {
  439. if ($cat_id)
  440. $cat_qpart = "cat_id = '$cat_id'";
  441. else
  442. $cat_qpart = "cat_id IS NULL";
  443. $result = db_query("SELECT
  444. id, feed_url, cat_id, title, order_id, ".
  445. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  446. FROM ttrss_feeds WHERE
  447. $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
  448. " ORDER BY cat_id, title " . $limit_qpart);
  449. }
  450. while ($line = db_fetch_assoc($result)) {
  451. $unread = getFeedUnread($line["id"]);
  452. $has_icon = feed_has_icon($line['id']);
  453. if ($unread || !$unread_only) {
  454. $row = array(
  455. "feed_url" => $line["feed_url"],
  456. "title" => $line["title"],
  457. "id" => (int)$line["id"],
  458. "unread" => (int)$unread,
  459. "has_icon" => $has_icon,
  460. "cat_id" => (int)$line["cat_id"],
  461. "last_updated" => (int) strtotime($line["last_updated"]),
  462. "order_id" => (int) $line["order_id"],
  463. );
  464. array_push($feeds, $row);
  465. }
  466. }
  467. return $feeds;
  468. }
  469. static function api_get_headlines($feed_id, $limit, $offset,
  470. $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
  471. $include_attachments, $since_id,
  472. $search = "", $search_mode = "",
  473. $include_nested = false, $sanitize_content = true) {
  474. $qfh_ret = queryFeedHeadlines($feed_id, $limit,
  475. $view_mode, $is_cat, $search, $search_mode,
  476. $order, $offset, 0, false, $since_id, $include_nested);
  477. $result = $qfh_ret[0];
  478. $feed_title = $qfh_ret[1];
  479. $headlines = array();
  480. while ($line = db_fetch_assoc($result)) {
  481. $is_updated = ($line["last_read"] == "" &&
  482. ($line["unread"] != "t" && $line["unread"] != "1"));
  483. $tags = explode(",", $line["tag_cache"]);
  484. $labels = json_decode($line["label_cache"], true);
  485. //if (!$tags) $tags = get_article_tags($line["id"]);
  486. //if (!$labels) $labels = get_article_labels($line["id"]);
  487. $headline_row = array(
  488. "id" => (int)$line["id"],
  489. "unread" => sql_bool_to_bool($line["unread"]),
  490. "marked" => sql_bool_to_bool($line["marked"]),
  491. "published" => sql_bool_to_bool($line["published"]),
  492. "updated" => (int) strtotime($line["updated"]),
  493. "is_updated" => $is_updated,
  494. "title" => $line["title"],
  495. "link" => $line["link"],
  496. "feed_id" => $line["feed_id"],
  497. "tags" => $tags,
  498. );
  499. if ($include_attachments)
  500. $headline_row['attachments'] = get_article_enclosures(
  501. $line['id']);
  502. if ($show_excerpt) {
  503. $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
  504. $headline_row["excerpt"] = $excerpt;
  505. }
  506. if ($show_content) {
  507. if ($line["cached_content"] != "") {
  508. $line["content_preview"] =& $line["cached_content"];
  509. }
  510. if ($sanitize_content) {
  511. $headline_row["content"] = sanitize(
  512. $line["content_preview"],
  513. sql_bool_to_bool($line['hide_images']),
  514. false, $line["site_url"]);
  515. } else {
  516. $headline_row["content"] = $line["content_preview"];
  517. }
  518. }
  519. // unify label output to ease parsing
  520. if ($labels["no-labels"] == 1) $labels = array();
  521. $headline_row["labels"] = $labels;
  522. $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] :
  523. $feed_title;
  524. $headline_row["comments_count"] = (int)$line["num_comments"];
  525. $headline_row["comments_link"] = $line["comments"];
  526. $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
  527. $headline_row["author"] = $line["author"];
  528. $headline_row["score"] = (int)$line["score"];
  529. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
  530. $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
  531. }
  532. array_push($headlines, $headline_row);
  533. }
  534. return $headlines;
  535. }
  536. function unsubscribeFeed() {
  537. $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
  538. $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
  539. id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
  540. if ($this->dbh->num_rows($result) != 0) {
  541. Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]);
  542. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  543. } else {
  544. $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
  545. }
  546. }
  547. function subscribeToFeed() {
  548. $feed_url = $this->dbh->escape_string($_REQUEST["feed_url"]);
  549. $category_id = (int) $this->dbh->escape_string($_REQUEST["category_id"]);
  550. $login = $this->dbh->escape_string($_REQUEST["login"]);
  551. $password = $this->dbh->escape_string($_REQUEST["password"]);
  552. if ($feed_url) {
  553. $rc = subscribe_to_feed($feed_url, $category_id, $login, $password);
  554. $this->wrap(self::STATUS_OK, array("status" => $rc));
  555. } else {
  556. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  557. }
  558. }
  559. function getFeedTree() {
  560. $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
  561. $pf = new Pref_Feeds($_REQUEST);
  562. $_REQUEST['mode'] = 2;
  563. $_REQUEST['force_show_empty'] = $include_empty;
  564. if ($pf){
  565. $data = $pf->makefeedtree();
  566. $this->wrap(self::STATUS_OK, array("categories" => $data));
  567. } else {
  568. $this->wrap(self::STATUS_ERR, array("error" =>
  569. 'UNABLE_TO_INSTANTIATE_OBJECT'));
  570. }
  571. }
  572. // only works for labels or uncategorized for the time being
  573. private function isCategoryEmpty($id) {
  574. if ($id == -2) {
  575. $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_labels2
  576. WHERE owner_uid = " . $_SESSION["uid"]);
  577. return $this->dbh->fetch_result($result, 0, "count") == 0;
  578. } else if ($id == 0) {
  579. $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_feeds
  580. WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]);
  581. return $this->dbh->fetch_result($result, 0, "count") == 0;
  582. }
  583. return false;
  584. }
  585. }
  586. ?>