api.php 22 KB

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