api.php 25 KB

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