api.php 25 KB

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