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