api.php 25 KB

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