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