]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: add getArticle
[tt-rss.git] / api / index.php
1 <?php
2
3 /* This is experimental JSON-based API. It has to be manually enabled:
4 *
5 * Add define('_JSON_API_ENABLED', true) to config.php
6 */
7
8 error_reporting(E_ERROR | E_PARSE);
9
10 require_once "../config.php";
11
12 require_once "../db.php";
13 require_once "../db-prefs.php";
14 require_once "../functions.php";
15
16 if (!defined('_JSON_API_ENABLED')) {
17 print json_encode(array("error" => "API_DISABLED"));
18 return;
19 }
20
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 $session_expire = SESSION_EXPIRE_TIME; //seconds
24 $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
25
26 session_start();
27
28 if (!$link) {
29 if (DB_TYPE == "mysql") {
30 print mysql_error();
31 }
32 // PG seems to display its own errors just fine by default.
33 return;
34 }
35
36 init_connection($link);
37
38 $op = db_escape_string($_REQUEST["op"]);
39
40 // header("Content-Type: application/json");
41
42 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
43 print json_encode(array("error" => 'NOT_LOGGED_IN'));
44 return;
45 }
46
47 /* TODO: add pref key to disable/enable API
48 if ($_SESSION["uid"] && !get_pref($link, 'API_ENABLED')) {
49 print json_encode(array("error" => 'API_DISABLED'));
50 return;
51 } */
52
53 switch ($op) {
54 case "getVersion":
55 $rv = array("version" => VERSION);
56 print json_encode($rv);
57 break;
58 case "login":
59 $login = db_escape_string($_REQUEST["user"]);
60 $password = db_escape_string($_REQUEST["password"]);
61
62 if (authenticate_user($link, $login, $password)) {
63 print json_encode(array("uid" => $_SESSION["uid"]));
64 } else {
65 print json_encode(array("uid" => 0));
66 }
67
68 break;
69 case "logout":
70 logout_user();
71 print json_encode(array("uid" => 0));
72 break;
73 case "isLoggedIn":
74 print json_encode(array("status" => $_SESSION["uid"] != ''));
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
80 if (!$cat_id) {
81 $result = db_query($link, "SELECT
82 id, feed_url, cat_id, title, ".
83 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
84 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
85 } else {
86 $result = db_query($link, "SELECT
87 id, feed_url, cat_id, title, ".
88 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
89 FROM ttrss_feeds WHERE
90 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
91 }
92
93 $feeds = array();
94
95 while ($line = db_fetch_assoc($result)) {
96
97 $unread = getFeedUnread($link, $line["id"]);
98
99 if ($unread || !$unread_only) {
100
101 $line_struct = array(
102 "feed_url" => $line["feed_url"],
103 "title" => $line["title"],
104 "id" => (int)$line["id"],
105 "unread" => (int)$unread,
106 "cat_id" => (int)$line["cat_id"],
107 "last_updated" => strtotime($line["last_updated"])
108 );
109
110 array_push($feeds, $line_struct);
111 }
112 }
113
114 print json_encode($feeds);
115
116 break;
117 case "getCategories":
118 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
119
120 $result = db_query($link, "SELECT
121 id, title FROM ttrss_feed_categories
122 WHERE owner_uid = " .
123 $_SESSION["uid"]);
124
125 $cats = array();
126
127 while ($line = db_fetch_assoc($result)) {
128 $unread = getFeedUnread($link, $line["id"], true);
129
130 if ($unread || !$unread_only) {
131 array_push($cats, array($line["id"] =>
132 array("title" => $line["title"], "unread" => $unread)));
133 }
134 }
135
136 print json_encode($cats);
137 break;
138 case "getHeadlines":
139 $feed_id = db_escape_string($_REQUEST["feed_id"]);
140 $limit = (int)db_escape_string($_REQUEST["limit"]);
141 $filter = db_escape_string($_REQUEST["filter"]);
142 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
143 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
144
145 /* do not rely on params below */
146
147 $search = db_escape_string($_REQUEST["search"]);
148 $search_mode = db_escape_string($_REQUEST["search_mode"]);
149 $match_on = db_escape_string($_REQUEST["match_on"]);
150
151 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
152 $view_mode, $is_cat, $search, $search_mode, $match_on);
153
154 $result = $qfh_ret[0];
155 $feed_title = $qfh_ret[1];
156
157 $headlines = array();
158
159 while ($line = db_fetch_assoc($result)) {
160 $is_updated = ($line["last_read"] == "" &&
161 ($line["unread"] != "t" && $line["unread"] != "1"));
162
163 $headline_row = array(
164 "id" => (int)$line["id"],
165 "unread" => sql_bool_to_bool($line["unread"]),
166 "marked" => sql_bool_to_bool($line["marked"]),
167 "updated" => strtotime($line["updated"]),
168 "is_updated" => $is_updated,
169 "title" => $line["title"],
170 "feed_id" => $line["feed_id"],
171 );
172
173 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
174
175 array_push($headlines, $headline_row);
176 }
177
178 print json_encode($headlines);
179
180 break;
181 case "getArticle":
182
183 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
184
185 $query = "SELECT title,link,content,feed_id,comments,int_id,
186 marked,unread,
187 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
188 author
189 FROM ttrss_entries,ttrss_user_entries
190 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
191 $_SESSION["uid"] ;
192
193 $result = db_query($link, $query);
194
195 $article = array();
196
197 if (db_num_rows($result) != 0) {
198 $line = db_fetch_assoc($result);
199
200 $article = array(
201 "title" => $line["title"],
202 "link" => $line["link"],
203 "unread" => sql_bool_to_bool($line["unread"]),
204 "marked" => sql_bool_to_bool($line["marked"]),
205 "comments" => $line["comments"],
206 "author" => $line["author"],
207 "updated" => strtotime($line["updated"]),
208 "content" => $line["content"],
209 "feed_id" => $line["feed_id"],
210 );
211 }
212
213 print json_encode($article);
214
215 break;
216 }
217
218 db_close($link);
219
220 ?>