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