api.php 22 KB

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