index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. error_reporting(E_ERROR | E_PARSE);
  3. require_once "../config.php";
  4. require_once "../db.php";
  5. require_once "../db-prefs.php";
  6. require_once "../functions.php";
  7. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  8. $session_expire = SESSION_EXPIRE_TIME; //seconds
  9. $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
  10. session_name($session_name);
  11. if ($_REQUEST["sid"]) {
  12. session_id($_REQUEST["sid"]);
  13. }
  14. session_start();
  15. if (!$link) {
  16. if (DB_TYPE == "mysql") {
  17. print mysql_error();
  18. }
  19. // PG seems to display its own errors just fine by default.
  20. return;
  21. }
  22. init_connection($link);
  23. $op = db_escape_string($_REQUEST["op"]);
  24. // header("Content-Type: application/json");
  25. if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
  26. print json_encode(array("error" => 'NOT_LOGGED_IN'));
  27. return;
  28. }
  29. if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
  30. print json_encode(array("error" => 'API_DISABLED'));
  31. return;
  32. }
  33. switch ($op) {
  34. case "getVersion":
  35. $rv = array("version" => VERSION);
  36. print json_encode($rv);
  37. break;
  38. case "login":
  39. $login = db_escape_string($_REQUEST["user"]);
  40. $password = db_escape_string($_REQUEST["password"]);
  41. $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
  42. if (db_num_rows($result) != 0) {
  43. $uid = db_fetch_result($result, 0, "id");
  44. } else {
  45. $uid = 0;
  46. }
  47. if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
  48. if (authenticate_user($link, $login, $password)) {
  49. print json_encode(array("session_id" => session_id()));
  50. } else {
  51. print json_encode(array("error" => "LOGIN_ERROR"));
  52. }
  53. } else {
  54. print json_encode(array("error" => "API_DISABLED"));
  55. }
  56. break;
  57. case "logout":
  58. logout_user();
  59. print json_encode(array("status" => "OK"));
  60. break;
  61. case "isLoggedIn":
  62. print json_encode(array("status" => $_SESSION["uid"] != ''));
  63. break;
  64. case "getUnread":
  65. $feed_id = db_escape_string($_REQUEST["feed_id"]);
  66. $is_cat = db_escape_string($_REQUEST["is_cat"]);
  67. if ($feed_id) {
  68. print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
  69. } else {
  70. print json_encode(array("unread" => getGlobalUnread($link)));
  71. }
  72. break;
  73. case "getCounters":
  74. /* TODO */
  75. break;
  76. case "getFeeds":
  77. $cat_id = db_escape_string($_REQUEST["cat_id"]);
  78. $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
  79. $limit = (int) db_escape_string($_REQUEST["limit"]);
  80. $offset = (int) db_escape_string($_REQUEST["offset"]);
  81. if ($limit) {
  82. $limit_qpart = "LIMIT $limit OFFSET $offset";
  83. } else {
  84. $limit_qpart = "";
  85. }
  86. if (!$cat_id) {
  87. $result = db_query($link, "SELECT
  88. id, feed_url, cat_id, title, ".
  89. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  90. FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
  91. "ORDER BY cat_id, title " . $limit_qpart);
  92. } else {
  93. $result = db_query($link, "SELECT
  94. id, feed_url, cat_id, title, ".
  95. SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
  96. FROM ttrss_feeds WHERE
  97. cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
  98. "ORDER BY cat_id, title " . $limit_qpart);
  99. }
  100. $feeds = array();
  101. while ($line = db_fetch_assoc($result)) {
  102. $unread = getFeedUnread($link, $line["id"]);
  103. $icon_path = "../" . ICONS_DIR . "/" . $line["id"] . ".ico";
  104. $has_icon = file_exists($icon_path) && filesize($icon_path) > 0;
  105. if ($unread || !$unread_only) {
  106. $row = array(
  107. "feed_url" => $line["feed_url"],
  108. "title" => $line["title"],
  109. "id" => (int)$line["id"],
  110. "unread" => (int)$unread,
  111. "has_icon" => $has_icon,
  112. "cat_id" => (int)$line["cat_id"],
  113. "last_updated" => strtotime($line["last_updated"])
  114. );
  115. array_push($feeds, $row);
  116. }
  117. }
  118. /* Labels */
  119. if (!$cat_id || $cat_id == -2) {
  120. $counters = getLabelCounters($link, false, true);
  121. foreach (array_keys($counters) as $id) {
  122. $unread = $counters[$id]["counter"];
  123. if ($unread || !$unread_only) {
  124. $row = array(
  125. "id" => $id,
  126. "title" => $counters[$id]["description"],
  127. "unread" => $counters[$id]["counter"],
  128. "cat_id" => -2,
  129. );
  130. array_push($feeds, $row);
  131. }
  132. }
  133. }
  134. /* Virtual feeds */
  135. if (!$cat_id || $cat_id == -1) {
  136. foreach (array(-1, -2, -3, -4) as $i) {
  137. $unread = getFeedUnread($link, $i);
  138. if ($unread || !$unread_only) {
  139. $title = getFeedTitle($link, $i);
  140. $row = array(
  141. "id" => $i,
  142. "title" => $title,
  143. "unread" => $unread,
  144. "cat_id" => -1,
  145. );
  146. array_push($feeds, $row);
  147. }
  148. }
  149. }
  150. print json_encode($feeds);
  151. break;
  152. case "getCategories":
  153. $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
  154. $result = db_query($link, "SELECT
  155. id, title FROM ttrss_feed_categories
  156. WHERE owner_uid = " .
  157. $_SESSION["uid"]);
  158. $cats = array();
  159. while ($line = db_fetch_assoc($result)) {
  160. $unread = getFeedUnread($link, $line["id"], true);
  161. if ($unread || !$unread_only) {
  162. array_push($cats, array("id" => $line["id"],
  163. "title" => $line["title"],
  164. "unread" => $unread));
  165. }
  166. }
  167. print json_encode($cats);
  168. break;
  169. case "getHeadlines":
  170. $feed_id = db_escape_string($_REQUEST["feed_id"]);
  171. $limit = (int)db_escape_string($_REQUEST["limit"]);
  172. $offset = (int)db_escape_string($_REQUEST["skip"]);
  173. $filter = db_escape_string($_REQUEST["filter"]);
  174. $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
  175. $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
  176. $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
  177. /* do not rely on params below */
  178. $search = db_escape_string($_REQUEST["search"]);
  179. $search_mode = db_escape_string($_REQUEST["search_mode"]);
  180. $match_on = db_escape_string($_REQUEST["match_on"]);
  181. $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
  182. $view_mode, $is_cat, $search, $search_mode, $match_on,
  183. false, $offset);
  184. $result = $qfh_ret[0];
  185. $feed_title = $qfh_ret[1];
  186. $headlines = array();
  187. while ($line = db_fetch_assoc($result)) {
  188. $is_updated = ($line["last_read"] == "" &&
  189. ($line["unread"] != "t" && $line["unread"] != "1"));
  190. $headline_row = array(
  191. "id" => (int)$line["id"],
  192. "unread" => sql_bool_to_bool($line["unread"]),
  193. "marked" => sql_bool_to_bool($line["marked"]),
  194. "updated" => strtotime($line["updated"]),
  195. "is_updated" => $is_updated,
  196. "title" => $line["title"],
  197. "feed_id" => $line["feed_id"],
  198. );
  199. if ($show_excerpt) {
  200. $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
  201. $headline_row["excerpt"] = $excerpt;
  202. }
  203. if ($show_content) {
  204. $headline_row["content"] = $line["content_preview"];
  205. }
  206. array_push($headlines, $headline_row);
  207. }
  208. print json_encode($headlines);
  209. break;
  210. case "updateArticle":
  211. $article_id = (int) db_escape_string($_GET["article_id"]);
  212. $mode = (int) db_escape_string($_REQUEST["mode"]);
  213. $field_raw = (int)db_escape_string($_REQUEST["field"]);
  214. $field = "";
  215. $set_to = "";
  216. switch ($field_raw) {
  217. case 0:
  218. $field = "marked";
  219. break;
  220. case 1:
  221. $field = "published";
  222. break;
  223. case 2:
  224. $field = "unread";
  225. break;
  226. };
  227. switch ($mode) {
  228. case 1:
  229. $set_to = "true";
  230. break;
  231. case 0:
  232. $set_to = "false";
  233. break;
  234. case 2:
  235. $set_to = "NOT $field";
  236. break;
  237. }
  238. if ($field && $set_to) {
  239. if ($field == "unread") {
  240. $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
  241. last_read = NOW()
  242. WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
  243. } else {
  244. $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
  245. WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
  246. }
  247. }
  248. break;
  249. case "getArticle":
  250. $article_id = (int)db_escape_string($_REQUEST["article_id"]);
  251. $query = "SELECT title,link,content,feed_id,comments,int_id,
  252. marked,unread,published,
  253. ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
  254. author
  255. FROM ttrss_entries,ttrss_user_entries
  256. WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
  257. $_SESSION["uid"] ;
  258. $result = db_query($link, $query);
  259. $article = array();
  260. if (db_num_rows($result) != 0) {
  261. $line = db_fetch_assoc($result);
  262. $article = array(
  263. "title" => $line["title"],
  264. "link" => $line["link"],
  265. "labels" => get_article_labels($link, $article_id),
  266. "unread" => sql_bool_to_bool($line["unread"]),
  267. "marked" => sql_bool_to_bool($line["marked"]),
  268. "published" => sql_bool_to_bool($line["published"]),
  269. "comments" => $line["comments"],
  270. "author" => $line["author"],
  271. "updated" => strtotime($line["updated"]),
  272. "content" => $line["content"],
  273. "feed_id" => $line["feed_id"],
  274. );
  275. }
  276. print json_encode($article);
  277. break;
  278. case "getConfig":
  279. $config = array(
  280. "icons_dir" => ICONS_DIR,
  281. "icons_url" => ICONS_URL);
  282. if (ENABLE_UPDATE_DAEMON) {
  283. $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
  284. }
  285. $result = db_query($link, "SELECT COUNT(*) AS cf FROM
  286. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  287. $num_feeds = db_fetch_result($result, 0, "cf");
  288. $config["num_feeds"] = (int)$num_feeds;
  289. print json_encode($config);
  290. break;
  291. case "updateFeed":
  292. $feed_id = db_escape_string($_REQUEST["feed_id"]);
  293. $result = db_query($link,
  294. "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
  295. AND owner_uid = " . $_SESSION["uid"]);
  296. if (db_num_rows($result) > 0) {
  297. $feed_url = db_fetch_result($result, 0, "feed_url");
  298. update_rss_feed($link, $feed_url, $feed_id);
  299. }
  300. print json_encode(array("status" => "OK"));
  301. break;
  302. case "getPref":
  303. $pref_name = db_escape_string($_REQUEST["pref_name"]);
  304. print json_encode(array("value" => get_pref($link, $pref_name)));
  305. break;
  306. default:
  307. print json_encode(array("error" => 'UNKNOWN_METHOD'));
  308. break;
  309. }
  310. db_close($link);
  311. ?>