api.php 24 KB

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