api.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <?php
  2. class API extends Handler {
  3. const API_LEVEL = 7;
  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 = !isset($_REQUEST["sanitize"]) ||
  158. sql_bool_to_bool($_REQUEST["sanitize"]);
  159. $override_order = false;
  160. switch ($_REQUEST["order_by"]) {
  161. case "date_reverse":
  162. $override_order = "score DESC, date_entered, updated";
  163. break;
  164. case "feed_dates":
  165. $override_order = "updated DESC";
  166. break;
  167. }
  168. /* do not rely on params below */
  169. $search = $this->dbh->escape_string($_REQUEST["search"]);
  170. $search_mode = $this->dbh->escape_string($_REQUEST["search_mode"]);
  171. $headlines = $this->api_get_headlines($feed_id, $limit, $offset,
  172. $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
  173. $include_attachments, $since_id, $search, $search_mode,
  174. $include_nested, $sanitize_content);
  175. $this->wrap(self::STATUS_OK, $headlines);
  176. } else {
  177. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  178. }
  179. }
  180. function updateArticle() {
  181. $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  182. $mode = (int) $this->dbh->escape_string($_REQUEST["mode"]);
  183. $data = $this->dbh->escape_string($_REQUEST["data"]);
  184. $field_raw = (int)$this->dbh->escape_string($_REQUEST["field"]);
  185. $field = "";
  186. $set_to = "";
  187. switch ($field_raw) {
  188. case 0:
  189. $field = "marked";
  190. $additional_fields = ",last_marked = NOW()";
  191. break;
  192. case 1:
  193. $field = "published";
  194. $additional_fields = ",last_published = NOW()";
  195. break;
  196. case 2:
  197. $field = "unread";
  198. $additional_fields = ",last_read = NOW()";
  199. break;
  200. case 3:
  201. $field = "note";
  202. };
  203. switch ($mode) {
  204. case 1:
  205. $set_to = "true";
  206. break;
  207. case 0:
  208. $set_to = "false";
  209. break;
  210. case 2:
  211. $set_to = "NOT $field";
  212. break;
  213. }
  214. if ($field == "note") $set_to = "'$data'";
  215. if ($field && $set_to && count($article_ids) > 0) {
  216. $article_ids = join(", ", $article_ids);
  217. $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"]);
  218. $num_updated = $this->dbh->affected_rows($result);
  219. if ($num_updated > 0 && $field == "unread") {
  220. $result = $this->dbh->query("SELECT DISTINCT feed_id FROM ttrss_user_entries
  221. WHERE ref_id IN ($article_ids)");
  222. while ($line = $this->dbh->fetch_assoc($result)) {
  223. ccache_update($line["feed_id"], $_SESSION["uid"]);
  224. }
  225. }
  226. if ($num_updated > 0 && $field == "published") {
  227. if (PUBSUBHUBBUB_HUB) {
  228. $rss_link = get_self_url_prefix() .
  229. "/public.php?op=rss&id=-2&key=" .
  230. get_feed_access_key(-2, false);
  231. $p = new Publisher(PUBSUBHUBBUB_HUB);
  232. $pubsub_result = $p->publish_update($rss_link);
  233. }
  234. }
  235. $this->wrap(self::STATUS_OK, array("status" => "OK",
  236. "updated" => $num_updated));
  237. } else {
  238. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  239. }
  240. }
  241. function getArticle() {
  242. $article_id = join(",", array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_id"])), is_numeric));
  243. if ($article_id) {
  244. $query = "SELECT id,title,link,content,feed_id,comments,int_id,
  245. marked,unread,published,score,
  246. ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
  247. author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title
  248. FROM ttrss_entries,ttrss_user_entries
  249. WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
  250. $_SESSION["uid"] ;
  251. $result = $this->dbh->query($query);
  252. $articles = array();
  253. if ($this->dbh->num_rows($result) != 0) {
  254. while ($line = $this->dbh->fetch_assoc($result)) {
  255. $attachments = get_article_enclosures($line['id']);
  256. $article = array(
  257. "id" => $line["id"],
  258. "title" => $line["title"],
  259. "link" => $line["link"],
  260. "labels" => get_article_labels($line['id']),
  261. "unread" => sql_bool_to_bool($line["unread"]),
  262. "marked" => sql_bool_to_bool($line["marked"]),
  263. "published" => sql_bool_to_bool($line["published"]),
  264. "comments" => $line["comments"],
  265. "author" => $line["author"],
  266. "updated" => (int) strtotime($line["updated"]),
  267. "content" => $line["content"],
  268. "feed_id" => $line["feed_id"],
  269. "attachments" => $attachments,
  270. "score" => (int)$line["score"],
  271. "feed_title" => $line["feed_title"]
  272. );
  273. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
  274. $article = $p->hook_render_article_api(array("article" => $article));
  275. }
  276. array_push($articles, $article);
  277. }
  278. }
  279. $this->wrap(self::STATUS_OK, $articles);
  280. } else {
  281. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  282. }
  283. }
  284. function getConfig() {
  285. $config = array(
  286. "icons_dir" => ICONS_DIR,
  287. "icons_url" => ICONS_URL);
  288. $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
  289. $result = $this->dbh->query("SELECT COUNT(*) AS cf FROM
  290. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  291. $num_feeds = $this->dbh->fetch_result($result, 0, "cf");
  292. $config["num_feeds"] = (int)$num_feeds;
  293. $this->wrap(self::STATUS_OK, $config);
  294. }
  295. function updateFeed() {
  296. require_once "include/rssfuncs.php";
  297. $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
  298. update_rss_feed($feed_id, true);
  299. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  300. }
  301. function catchupFeed() {
  302. $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
  303. $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]);
  304. catchup_feed($feed_id, $is_cat);
  305. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  306. }
  307. function getPref() {
  308. $pref_name = $this->dbh->escape_string($_REQUEST["pref_name"]);
  309. $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
  310. }
  311. function getLabels() {
  312. //$article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  313. $article_id = (int)$_REQUEST['article_id'];
  314. $rv = array();
  315. $result = $this->dbh->query("SELECT id, caption, fg_color, bg_color
  316. FROM ttrss_labels2
  317. WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
  318. if ($article_id)
  319. $article_labels = get_article_labels($article_id);
  320. else
  321. $article_labels = array();
  322. while ($line = $this->dbh->fetch_assoc($result)) {
  323. $checked = false;
  324. foreach ($article_labels as $al) {
  325. if ($al[0] == $line['id']) {
  326. $checked = true;
  327. break;
  328. }
  329. }
  330. array_push($rv, array(
  331. "id" => (int)label_to_feed_id($line['id']),
  332. "caption" => $line['caption'],
  333. "fg_color" => $line['fg_color'],
  334. "bg_color" => $line['bg_color'],
  335. "checked" => $checked));
  336. }
  337. $this->wrap(self::STATUS_OK, $rv);
  338. }
  339. function setArticleLabel() {
  340. $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
  341. $label_id = (int) $this->dbh->escape_string($_REQUEST['label_id']);
  342. $assign = (bool) $this->dbh->escape_string($_REQUEST['assign']) == "true";
  343. $label = $this->dbh->escape_string(label_find_caption(
  344. $label_id, $_SESSION["uid"]));
  345. $num_updated = 0;
  346. if ($label) {
  347. foreach ($article_ids as $id) {
  348. if ($assign)
  349. label_add_article($id, $label, $_SESSION["uid"]);
  350. else
  351. label_remove_article($id, $label, $_SESSION["uid"]);
  352. ++$num_updated;
  353. }
  354. }
  355. $this->wrap(self::STATUS_OK, array("status" => "OK",
  356. "updated" => $num_updated));
  357. }
  358. function index($method) {
  359. $plugin = PluginHost::getInstance()->get_api_method(strtolower($method));
  360. if ($plugin && method_exists($plugin, $method)) {
  361. $reply = $plugin->$method();
  362. $this->wrap($reply[0], $reply[1]);
  363. } else {
  364. $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
  365. }
  366. }
  367. function shareToPublished() {
  368. $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"]));
  369. $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
  370. $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"]));
  371. if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) {
  372. $this->wrap(self::STATUS_OK, array("status" => 'OK'));
  373. } else {
  374. $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
  375. }
  376. }
  377. static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
  378. $feeds = array();
  379. /* Labels */
  380. if ($cat_id == -4 || $cat_id == -2) {
  381. $counters = getLabelCounters(true);
  382. foreach (array_values($counters) as $cv) {
  383. $unread = $cv["counter"];
  384. if ($unread || !$unread_only) {
  385. $row = array(
  386. "id" => $cv["id"],
  387. "title" => $cv["description"],
  388. "unread" => $cv["counter"],
  389. "cat_id" => -2,
  390. );
  391. array_push($feeds, $row);
  392. }
  393. }
  394. }
  395. /* Virtual feeds */
  396. if ($cat_id == -4 || $cat_id == -1) {
  397. foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
  398. $unread = getFeedUnread($i);
  399. if ($unread || !$unread_only) {
  400. $title = getFeedTitle($i);
  401. $row = array(
  402. "id" => $i,
  403. "title" => $title,
  404. "unread" => $unread,
  405. "cat_id" => -1,
  406. );
  407. array_push($feeds, $row);
  408. }
  409. }
  410. }
  411. /* Child cats */
  412. if ($include_nested && $cat_id) {
  413. $result = db_query("SELECT
  414. id, title FROM ttrss_feed_categories
  415. WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
  416. " ORDER BY id, title");
  417. while ($line = db_fetch_assoc($result)) {
  418. $unread = getFeedUnread($line["id"], true) +
  419. getCategoryChildrenUnread($line["id"]);
  420. if ($unread || !$unread_only) {
  421. $row = array(
  422. "id" => $line["id"],
  423. "title" => $line["title"],
  424. "unread" => $unread,
  425. "is_cat" => true,
  426. );
  427. array_push($feeds, $row);
  428. }
  429. }
  430. }
  431. /* Real feeds */
  432. if ($limit) {
  433. $limit_qpart = "LIMIT $limit OFFSET $offset";
  434. } else {
  435. $limit_qpart = "";
  436. }
  437. if ($cat_id == -4 || $cat_id == -3) {
  438. $result = db_query("SELECT
  439. id, feed_url, cat_id, title, order_id, ".
  440. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  441. FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
  442. " ORDER BY cat_id, title " . $limit_qpart);
  443. } else {
  444. if ($cat_id)
  445. $cat_qpart = "cat_id = '$cat_id'";
  446. else
  447. $cat_qpart = "cat_id IS NULL";
  448. $result = db_query("SELECT
  449. id, feed_url, cat_id, title, order_id, ".
  450. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  451. FROM ttrss_feeds WHERE
  452. $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
  453. " ORDER BY cat_id, title " . $limit_qpart);
  454. }
  455. while ($line = db_fetch_assoc($result)) {
  456. $unread = getFeedUnread($line["id"]);
  457. $has_icon = feed_has_icon($line['id']);
  458. if ($unread || !$unread_only) {
  459. $row = array(
  460. "feed_url" => $line["feed_url"],
  461. "title" => $line["title"],
  462. "id" => (int)$line["id"],
  463. "unread" => (int)$unread,
  464. "has_icon" => $has_icon,
  465. "cat_id" => (int)$line["cat_id"],
  466. "last_updated" => (int) strtotime($line["last_updated"]),
  467. "order_id" => (int) $line["order_id"],
  468. );
  469. array_push($feeds, $row);
  470. }
  471. }
  472. return $feeds;
  473. }
  474. static function api_get_headlines($feed_id, $limit, $offset,
  475. $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
  476. $include_attachments, $since_id,
  477. $search = "", $search_mode = "",
  478. $include_nested = false, $sanitize_content = true) {
  479. $qfh_ret = queryFeedHeadlines($feed_id, $limit,
  480. $view_mode, $is_cat, $search, $search_mode,
  481. $order, $offset, 0, false, $since_id, $include_nested);
  482. $result = $qfh_ret[0];
  483. $feed_title = $qfh_ret[1];
  484. $headlines = array();
  485. while ($line = db_fetch_assoc($result)) {
  486. $line["content_preview"] = truncate_string(strip_tags($line["content_preview"]), 100);
  487. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
  488. $line = $p->hook_query_headlines($line, 100, true);
  489. }
  490. $is_updated = ($line["last_read"] == "" &&
  491. ($line["unread"] != "t" && $line["unread"] != "1"));
  492. $tags = explode(",", $line["tag_cache"]);
  493. $labels = json_decode($line["label_cache"], true);
  494. //if (!$tags) $tags = get_article_tags($line["id"]);
  495. //if (!$labels) $labels = get_article_labels($line["id"]);
  496. $headline_row = array(
  497. "id" => (int)$line["id"],
  498. "unread" => sql_bool_to_bool($line["unread"]),
  499. "marked" => sql_bool_to_bool($line["marked"]),
  500. "published" => sql_bool_to_bool($line["published"]),
  501. "updated" => (int) strtotime($line["updated"]),
  502. "is_updated" => $is_updated,
  503. "title" => $line["title"],
  504. "link" => $line["link"],
  505. "feed_id" => $line["feed_id"],
  506. "tags" => $tags,
  507. );
  508. if ($include_attachments)
  509. $headline_row['attachments'] = get_article_enclosures(
  510. $line['id']);
  511. if ($show_excerpt)
  512. $headline_row["excerpt"] = $line["content_preview"];
  513. if ($show_content) {
  514. if ($sanitize_content) {
  515. $headline_row["content"] = sanitize(
  516. $line["content"],
  517. sql_bool_to_bool($line['hide_images']),
  518. false, $line["site_url"], false, $line["id"]);
  519. } else {
  520. $headline_row["content"] = $line["content"];
  521. }
  522. }
  523. // unify label output to ease parsing
  524. if ($labels["no-labels"] == 1) $labels = array();
  525. $headline_row["labels"] = $labels;
  526. $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] :
  527. $feed_title;
  528. $headline_row["comments_count"] = (int)$line["num_comments"];
  529. $headline_row["comments_link"] = $line["comments"];
  530. $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
  531. $headline_row["author"] = $line["author"];
  532. $headline_row["score"] = (int)$line["score"];
  533. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
  534. $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
  535. }
  536. array_push($headlines, $headline_row);
  537. }
  538. return $headlines;
  539. }
  540. function unsubscribeFeed() {
  541. $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
  542. $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
  543. id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
  544. if ($this->dbh->num_rows($result) != 0) {
  545. Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]);
  546. $this->wrap(self::STATUS_OK, array("status" => "OK"));
  547. } else {
  548. $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
  549. }
  550. }
  551. function subscribeToFeed() {
  552. $feed_url = $this->dbh->escape_string($_REQUEST["feed_url"]);
  553. $category_id = (int) $this->dbh->escape_string($_REQUEST["category_id"]);
  554. $login = $this->dbh->escape_string($_REQUEST["login"]);
  555. $password = $this->dbh->escape_string($_REQUEST["password"]);
  556. if ($feed_url) {
  557. $rc = subscribe_to_feed($feed_url, $category_id, $login, $password);
  558. $this->wrap(self::STATUS_OK, array("status" => $rc));
  559. } else {
  560. $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
  561. }
  562. }
  563. function getFeedTree() {
  564. $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
  565. $pf = new Pref_Feeds($_REQUEST);
  566. $_REQUEST['mode'] = 2;
  567. $_REQUEST['force_show_empty'] = $include_empty;
  568. if ($pf){
  569. $data = $pf->makefeedtree();
  570. $this->wrap(self::STATUS_OK, array("categories" => $data));
  571. } else {
  572. $this->wrap(self::STATUS_ERR, array("error" =>
  573. 'UNABLE_TO_INSTANTIATE_OBJECT'));
  574. }
  575. }
  576. // only works for labels or uncategorized for the time being
  577. private function isCategoryEmpty($id) {
  578. if ($id == -2) {
  579. $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_labels2
  580. WHERE owner_uid = " . $_SESSION["uid"]);
  581. return $this->dbh->fetch_result($result, 0, "count") == 0;
  582. } else if ($id == 0) {
  583. $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_feeds
  584. WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]);
  585. return $this->dbh->fetch_result($result, 0, "count") == 0;
  586. }
  587. return false;
  588. }
  589. }
  590. ?>