api.php 25 KB

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