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