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