]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: add getHeadlines since_id
[tt-rss.git] / api / index.php
1 <?php
2 error_reporting(E_ERROR | E_PARSE);
3
4 require_once "../config.php";
5
6 require_once "../db.php";
7 require_once "../db-prefs.php";
8 require_once "../functions.php";
9
10 define('API_STATUS_OK', 0);
11 define('API_STATUS_ERR', 1);
12
13 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) {
14 ob_start("ob_gzhandler");
15 }
16
17 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
18
19 $session_expire = SESSION_EXPIRE_TIME; //seconds
20 $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
21
22 session_name($session_name);
23
24 $input = file_get_contents("php://input");
25
26 // Override $_REQUEST with JSON-encoded data if available
27 if ($input) {
28 $input = json_decode($input, true);
29
30 if ($input) {
31 $_REQUEST = $input;
32 }
33 }
34
35 if ($_REQUEST["sid"]) {
36 session_id($_REQUEST["sid"]);
37 }
38
39 session_start();
40
41 if (!$link) {
42 if (DB_TYPE == "mysql") {
43 print mysql_error();
44 }
45 // PG seems to display its own errors just fine by default.
46 return;
47 }
48
49 init_connection($link);
50
51 $op = db_escape_string($_REQUEST["op"]);
52 $seq = (int) $_REQUEST["seq"];
53
54 header("Content-Type: text/plain");
55
56 function api_wrap_reply($status, $seq, $reply) {
57 print json_encode(array("seq" => $seq,
58 "status" => $status,
59 "content" => $reply));
60 }
61
62 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
63 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN'));
64 return;
65 }
66
67 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
68 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED'));
69 return;
70 }
71
72 switch ($op) {
73
74 case "getVersion":
75 $rv = array("version" => VERSION);
76 print api_wrap_reply(API_STATUS_OK, $seq, $rv);
77 break;
78
79 case "login":
80 $login = db_escape_string($_REQUEST["user"]);
81 $password = db_escape_string($_REQUEST["password"]);
82 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
83
84 if (SINGLE_USER_MODE) $login = "admin";
85
86 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
87
88 if (db_num_rows($result) != 0) {
89 $uid = db_fetch_result($result, 0, "id");
90 } else {
91 $uid = 0;
92 }
93
94 if (!$uid) {
95 print api_wrap_reply(API_STATUS_ERR, $seq,
96 array("error" => "LOGIN_ERROR"));
97 return;
98 }
99
100 if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
101 if (authenticate_user($link, $login, $password)) { // try login with normal password
102 print api_wrap_reply(API_STATUS_OK, $seq,
103 array("session_id" => session_id()));
104 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
105 print api_wrap_reply(API_STATUS_OK, $seq,
106 array("session_id" => session_id()));
107 } else { // else we are not logged in
108 print api_wrap_reply(API_STATUS_ERR, $seq,
109 array("error" => "LOGIN_ERROR"));
110 }
111 } else {
112 print api_wrap_reply(API_STATUS_ERR, $seq,
113 array("error" => "API_DISABLED"));
114 }
115
116 break;
117
118 case "logout":
119 logout_user();
120 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
121 break;
122
123 case "isLoggedIn":
124 print api_wrap_reply(API_STATUS_OK, $seq,
125 array("status" => $_SESSION["uid"] != ''));
126 break;
127
128 case "getUnread":
129 $feed_id = db_escape_string($_REQUEST["feed_id"]);
130 $is_cat = db_escape_string($_REQUEST["is_cat"]);
131
132 if ($feed_id) {
133 print api_wrap_reply(API_STATUS_OK, $seq,
134 array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
135 } else {
136 print api_wrap_reply(API_STATUS_OK, $seq,
137 array("unread" => getGlobalUnread($link)));
138 }
139 break;
140
141 /* Method added for ttrss-reader for Android */
142 case "getCounters":
143
144 /* flct (flc is the default) FIXME: document */
145 $output_mode = db_escape_string($_REQUEST["output_mode"]);
146
147 print api_wrap_reply(API_STATUS_OK, $seq,
148 getAllCounters($link, $output_mode));
149 break;
150
151 case "getFeeds":
152 $cat_id = db_escape_string($_REQUEST["cat_id"]);
153 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
154 $limit = (int) db_escape_string($_REQUEST["limit"]);
155 $offset = (int) db_escape_string($_REQUEST["offset"]);
156
157 chdir(".."); // so feed_has_icon() would work properly for relative ICONS_DIR
158
159 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
160
161 print api_wrap_reply(API_STATUS_OK, $seq, $feeds);
162
163 break;
164
165 case "getCategories":
166 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
167
168 $result = db_query($link, "SELECT
169 id, title FROM ttrss_feed_categories
170 WHERE owner_uid = " .
171 $_SESSION["uid"]);
172
173 $cats = array();
174
175 while ($line = db_fetch_assoc($result)) {
176 $unread = getFeedUnread($link, $line["id"], true);
177
178 if ($unread || !$unread_only) {
179 array_push($cats, array("id" => $line["id"],
180 "title" => $line["title"],
181 "unread" => $unread));
182 }
183 }
184
185 print api_wrap_reply(API_STATUS_OK, $seq, $cats);
186 break;
187
188 case "getHeadlines":
189 $feed_id = db_escape_string($_REQUEST["feed_id"]);
190 $limit = (int)db_escape_string($_REQUEST["limit"]);
191 $offset = (int)db_escape_string($_REQUEST["skip"]);
192 $filter = db_escape_string($_REQUEST["filter"]);
193 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
194 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
195 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
196 /* all_articles, unread, adaptive, marked, updated */
197 $view_mode = db_escape_string($_REQUEST["view_mode"]);
198 $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
199 $since_id = (int)db_escape_string($_REQUEST["since_id"]);
200
201 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
202 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
203 $include_attachments, $since_id);
204
205 print api_wrap_reply(API_STATUS_OK, $seq, $headlines);
206
207 break;
208
209 case "updateArticle":
210 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
211 $mode = (int) db_escape_string($_REQUEST["mode"]);
212 $field_raw = (int)db_escape_string($_REQUEST["field"]);
213
214 $field = "";
215 $set_to = "";
216
217 switch ($field_raw) {
218 case 0:
219 $field = "marked";
220 break;
221 case 1:
222 $field = "published";
223 break;
224 case 2:
225 $field = "unread";
226 break;
227 };
228
229 switch ($mode) {
230 case 1:
231 $set_to = "true";
232 break;
233 case 0:
234 $set_to = "false";
235 break;
236 case 2:
237 $set_to = "NOT $field";
238 break;
239 }
240
241 if ($field && $set_to && count($article_ids) > 0) {
242
243 $article_ids = join(", ", $article_ids);
244
245 if ($field == "unread") {
246 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
247 last_read = NOW()
248 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
249 } else {
250 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
251 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
252 }
253
254 $num_updated = db_affected_rows($link, $result);
255
256 if ($num_updated > 0 && $field == "unread") {
257 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
258 WHERE ref_id IN ($article_ids)");
259
260 while ($line = db_fetch_assoc($result)) {
261 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
262 }
263 }
264
265 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK",
266 "updated" => $num_updated));
267
268 } else {
269 print api_wrap_reply(API_STATUS_ERR, $seq,
270 array("error" => 'INCORRECT_USAGE'));
271 }
272
273 break;
274
275 case "getArticle":
276
277 $article_id = db_escape_string($_REQUEST["article_id"]);
278
279 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
280 marked,unread,published,
281 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
282 author
283 FROM ttrss_entries,ttrss_user_entries
284 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
285 $_SESSION["uid"] ;
286
287 $result = db_query($link, $query);
288
289 $articles = array();
290
291 if (db_num_rows($result) != 0) {
292
293 while ($line = db_fetch_assoc($result)) {
294
295 $attachments = get_article_enclosures($link, $line['id']);
296
297 $article = array(
298 "id" => $line["id"],
299 "title" => $line["title"],
300 "link" => $line["link"],
301 "labels" => get_article_labels($link, $line['id']),
302 "unread" => sql_bool_to_bool($line["unread"]),
303 "marked" => sql_bool_to_bool($line["marked"]),
304 "published" => sql_bool_to_bool($line["published"]),
305 "comments" => $line["comments"],
306 "author" => $line["author"],
307 "updated" => strtotime($line["updated"]),
308 "content" => $line["content"],
309 "feed_id" => $line["feed_id"],
310 "attachments" => $attachments
311 );
312
313 array_push($articles, $article);
314
315 }
316 }
317
318 print api_wrap_reply(API_STATUS_OK, $seq, $articles);
319
320 break;
321
322 case "getConfig":
323 $config = array(
324 "icons_dir" => ICONS_DIR,
325 "icons_url" => ICONS_URL);
326
327 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
328
329 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
330 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
331
332 $num_feeds = db_fetch_result($result, 0, "cf");
333
334 $config["num_feeds"] = (int)$num_feeds;
335
336 print api_wrap_reply(API_STATUS_OK, $seq, $config);
337
338 break;
339
340 case "updateFeed":
341 $feed_id = db_escape_string($_REQUEST["feed_id"]);
342
343 update_rss_feed($link, $feed_id, true);
344
345 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
346
347 break;
348
349 case "catchupFeed":
350 $feed_id = db_escape_string($_REQUEST["feed_id"]);
351 $is_cat = db_escape_string($_REQUEST["is_cat"]);
352
353 catchup_feed($link, $feed_id, $is_cat);
354
355 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
356
357 break;
358
359 case "getPref":
360 $pref_name = db_escape_string($_REQUEST["pref_name"]);
361
362 print api_wrap_reply(API_STATUS_OK, $seq,
363 array("value" => get_pref($link, $pref_name)));
364 break;
365
366 default:
367 print api_wrap_reply(API_STATUS_ERR, $seq,
368 array("error" => 'UNKNOWN_METHOD'));
369 break;
370
371 }
372
373 db_close($link);
374
375 ?>