]>
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 | ||
3acc0da6 | 194 | print api_wrap_reply(API_STATUS_OK, $seq, $cats); |
378a548c | 195 | break; |
dbaa4e4a | 196 | |
378a548c AD |
197 | case "getHeadlines": |
198 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
199 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
285f7597 | 200 | $offset = (int)db_escape_string($_REQUEST["skip"]); |
378a548c AD |
201 | $filter = db_escape_string($_REQUEST["filter"]); |
202 | $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]); | |
d5e71621 AD |
203 | $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]); |
204 | $show_content = (bool)db_escape_string($_REQUEST["show_content"]); | |
ef6c9ba4 AD |
205 | /* all_articles, unread, adaptive, marked, updated */ |
206 | $view_mode = db_escape_string($_REQUEST["view_mode"]); | |
a0e580b0 | 207 | $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]); |
97e5dbb2 | 208 | $since_id = (int)db_escape_string($_REQUEST["since_id"]); |
378a548c | 209 | |
911d4c08 | 210 | $headlines = api_get_headlines($link, $feed_id, $limit, $offset, |
a0e580b0 | 211 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false, |
97e5dbb2 | 212 | $include_attachments, $since_id); |
378a548c | 213 | |
3acc0da6 | 214 | print api_wrap_reply(API_STATUS_OK, $seq, $headlines); |
378a548c | 215 | |
730c97c7 | 216 | break; |
dbaa4e4a | 217 | |
1c3fffbb | 218 | case "updateArticle": |
52ebaf93 | 219 | $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); |
1c3fffbb AD |
220 | $mode = (int) db_escape_string($_REQUEST["mode"]); |
221 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
222 | ||
223 | $field = ""; | |
224 | $set_to = ""; | |
225 | ||
226 | switch ($field_raw) { | |
227 | case 0: | |
228 | $field = "marked"; | |
229 | break; | |
230 | case 1: | |
231 | $field = "published"; | |
232 | break; | |
233 | case 2: | |
234 | $field = "unread"; | |
235 | break; | |
236 | }; | |
237 | ||
238 | switch ($mode) { | |
239 | case 1: | |
240 | $set_to = "true"; | |
241 | break; | |
242 | case 0: | |
243 | $set_to = "false"; | |
244 | break; | |
245 | case 2: | |
246 | $set_to = "NOT $field"; | |
247 | break; | |
248 | } | |
249 | ||
ef6c9ba4 AD |
250 | if ($field && $set_to && count($article_ids) > 0) { |
251 | ||
252 | $article_ids = join(", ", $article_ids); | |
253 | ||
1c3fffbb AD |
254 | if ($field == "unread") { |
255 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
256 | last_read = NOW() | |
ef6c9ba4 | 257 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); |
1c3fffbb AD |
258 | } else { |
259 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to | |
ef6c9ba4 | 260 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); |
1c3fffbb | 261 | } |
48317f5a | 262 | |
b4fdbcf4 AD |
263 | $num_updated = db_affected_rows($link, $result); |
264 | ||
265 | if ($num_updated > 0 && $field == "unread") { | |
266 | $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
267 | WHERE ref_id IN ($article_ids)"); | |
268 | ||
269 | while ($line = db_fetch_assoc($result)) { | |
270 | ccache_update($link, $line["feed_id"], $_SESSION["uid"]); | |
271 | } | |
272 | } | |
273 | ||
dbaa4e4a | 274 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK", |
b4fdbcf4 AD |
275 | "updated" => $num_updated)); |
276 | ||
277 | } else { | |
dbaa4e4a | 278 | print api_wrap_reply(API_STATUS_ERR, $seq, |
3acc0da6 | 279 | array("error" => 'INCORRECT_USAGE')); |
1c3fffbb AD |
280 | } |
281 | ||
282 | break; | |
283 | ||
730c97c7 AD |
284 | case "getArticle": |
285 | ||
823da71a | 286 | $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric)); |
730c97c7 | 287 | |
f95ac275 | 288 | $query = "SELECT id,title,link,content,feed_id,comments,int_id, |
1c3fffbb | 289 | marked,unread,published, |
730c97c7 AD |
290 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
291 | author | |
292 | FROM ttrss_entries,ttrss_user_entries | |
dbaa4e4a | 293 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . |
730c97c7 AD |
294 | $_SESSION["uid"] ; |
295 | ||
296 | $result = db_query($link, $query); | |
3acc0da6 AD |
297 | |
298 | $articles = array(); | |
299 | ||
730c97c7 | 300 | if (db_num_rows($result) != 0) { |
730c97c7 | 301 | |
f95ac275 AD |
302 | while ($line = db_fetch_assoc($result)) { |
303 | ||
304 | $attachments = get_article_enclosures($link, $line['id']); | |
305 | ||
306 | $article = array( | |
307 | "id" => $line["id"], | |
308 | "title" => $line["title"], | |
309 | "link" => $line["link"], | |
310 | "labels" => get_article_labels($link, $line['id']), | |
311 | "unread" => sql_bool_to_bool($line["unread"]), | |
312 | "marked" => sql_bool_to_bool($line["marked"]), | |
313 | "published" => sql_bool_to_bool($line["published"]), | |
314 | "comments" => $line["comments"], | |
315 | "author" => $line["author"], | |
316 | "updated" => strtotime($line["updated"]), | |
317 | "content" => $line["content"], | |
318 | "feed_id" => $line["feed_id"], | |
319 | "attachments" => $attachments | |
320 | ); | |
321 | ||
3acc0da6 AD |
322 | array_push($articles, $article); |
323 | ||
f95ac275 AD |
324 | } |
325 | } | |
730c97c7 | 326 | |
3acc0da6 AD |
327 | print api_wrap_reply(API_STATUS_OK, $seq, $articles); |
328 | ||
7c6d05cd | 329 | break; |
dbaa4e4a | 330 | |
7c6d05cd AD |
331 | case "getConfig": |
332 | $config = array( | |
333 | "icons_dir" => ICONS_DIR, | |
334 | "icons_url" => ICONS_URL); | |
335 | ||
dbaa4e4a | 336 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); |
7c6d05cd AD |
337 | |
338 | $result = db_query($link, "SELECT COUNT(*) AS cf FROM | |
339 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
340 | ||
341 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
342 | ||
343 | $config["num_feeds"] = (int)$num_feeds; | |
dbaa4e4a | 344 | |
3acc0da6 | 345 | print api_wrap_reply(API_STATUS_OK, $seq, $config); |
7c6d05cd | 346 | |
4d557a13 | 347 | break; |
7c6d05cd | 348 | |
cc374435 AD |
349 | case "updateFeed": |
350 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
351 | ||
c633e370 | 352 | update_rss_feed($link, $feed_id, true); |
cc374435 | 353 | |
3acc0da6 | 354 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK")); |
c45ffc0c | 355 | |
cc374435 AD |
356 | break; |
357 | ||
ca75c2e2 AD |
358 | case "catchupFeed": |
359 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
ee273da6 | 360 | $is_cat = db_escape_string($_REQUEST["is_cat"]); |
ca75c2e2 | 361 | |
423e6e55 | 362 | catchup_feed($link, $feed_id, $is_cat); |
ca75c2e2 | 363 | |
3acc0da6 | 364 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK")); |
ca75c2e2 AD |
365 | |
366 | break; | |
367 | ||
4d557a13 AD |
368 | case "getPref": |
369 | $pref_name = db_escape_string($_REQUEST["pref_name"]); | |
3acc0da6 | 370 | |
dbaa4e4a | 371 | print api_wrap_reply(API_STATUS_OK, $seq, |
3acc0da6 | 372 | array("value" => get_pref($link, $pref_name))); |
378a548c | 373 | break; |
dbaa4e4a | 374 | |
b792cd70 | 375 | default: |
dbaa4e4a | 376 | print api_wrap_reply(API_STATUS_ERR, $seq, |
3acc0da6 | 377 | array("error" => 'UNKNOWN_METHOD')); |
b792cd70 AD |
378 | break; |
379 | ||
378a548c AD |
380 | } |
381 | ||
382 | db_close($link); | |
dbaa4e4a | 383 | |
378a548c | 384 | ?> |