]> git.wh0rd.org Git - tt-rss.git/blob - api/index.php
api: implement setting article note through updateArticle
[tt-rss.git] / api / index.php
1 <?php
2         error_reporting(E_ERROR | E_PARSE);
3
4         require_once "../config.php";
5
6         require_once "../db.php";
7         require_once "../db-prefs.php";
8         require_once "../functions.php";
9
10         define('API_LEVEL', 1);
11
12         define('API_STATUS_OK', 0);
13         define('API_STATUS_ERR', 1);
14
15         chdir("..");
16
17         if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) {
18                 ob_start("ob_gzhandler");
19         }
20
21         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23         $session_expire = SESSION_EXPIRE_TIME; //seconds
24         $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
25
26         session_name($session_name);
27
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
39         if ($_REQUEST["sid"]) {
40                 session_id($_REQUEST["sid"]);
41         }
42
43         session_start();
44
45         if (!$link) {
46                 if (DB_TYPE == "mysql") {
47                         print mysql_error();
48                 }
49                 // PG seems to display its own errors just fine by default.
50                 return;
51         }
52
53         init_connection($link);
54
55         $op = db_escape_string($_REQUEST["op"]);
56         $seq = (int) $_REQUEST["seq"];
57
58         header("Content-Type: text/plain");
59
60         function api_wrap_reply($status, $seq, $reply) {
61                 print json_encode(array("seq" => $seq,
62                         "status" => $status,
63                         "content" => $reply));
64         }
65
66         if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
67                 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN'));
68                 return;
69         }
70
71         if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
72                 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED'));
73                 return;
74         }
75
76         switch ($op) {
77
78                 case "getVersion":
79                         $rv = array("version" => VERSION);
80                         print api_wrap_reply(API_STATUS_OK, $seq, $rv);
81                         break;
82
83                 case "getApiLevel":
84                         $rv = array("level" => API_LEVEL);
85                         print api_wrap_reply(API_STATUS_OK, $seq, $rv);
86                         break;
87
88                 case "login":
89                         $login = db_escape_string($_REQUEST["user"]);
90                         $password = $_REQUEST["password"];
91                         $password_base64 = base64_decode($_REQUEST["password"]);
92
93                         if (SINGLE_USER_MODE) $login = "admin";
94
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
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)) {
110                                 if (authenticate_user($link, $login, $password)) {               // try login with normal password
111                                         print api_wrap_reply(API_STATUS_OK, $seq,
112                                                 array("session_id" => session_id()));
113                                 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
114                                         print api_wrap_reply(API_STATUS_OK, $seq,
115                                                 array("session_id" => session_id()));
116                                 } else {                                                         // else we are not logged in
117                                         print api_wrap_reply(API_STATUS_ERR, $seq,
118                                                 array("error" => "LOGIN_ERROR"));
119                                 }
120                         } else {
121                                 print api_wrap_reply(API_STATUS_ERR, $seq,
122                                         array("error" => "API_DISABLED"));
123                         }
124
125                         break;
126
127                 case "logout":
128                         logout_user();
129                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
130                         break;
131
132                 case "isLoggedIn":
133                         print api_wrap_reply(API_STATUS_OK, $seq,
134                                 array("status" => $_SESSION["uid"] != ''));
135                         break;
136
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) {
142                                 print api_wrap_reply(API_STATUS_OK, $seq,
143                                         array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
144                         } else {
145                                 print api_wrap_reply(API_STATUS_OK, $seq,
146                                         array("unread" => getGlobalUnread($link)));
147                         }
148                         break;
149
150                 /* Method added for ttrss-reader for Android */
151                 case "getCounters":
152
153                         /* flct (flc is the default) FIXME: document */
154                         $output_mode = db_escape_string($_REQUEST["output_mode"]);
155
156                         print api_wrap_reply(API_STATUS_OK, $seq,
157                                 getAllCounters($link, $output_mode));
158                         break;
159
160                 case "getFeeds":
161                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
162                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
163                         $limit = (int) db_escape_string($_REQUEST["limit"]);
164                         $offset = (int) db_escape_string($_REQUEST["offset"]);
165
166                         $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
167
168                         print api_wrap_reply(API_STATUS_OK, $seq, $feeds);
169
170                         break;
171
172                 case "getCategories":
173                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
174
175                         // TODO do not return empty categories, return Uncategorized and standard virtual cats
176
177                         $result = db_query($link, "SELECT
178                                         id, title FROM ttrss_feed_categories
179                                 WHERE owner_uid = " .
180                                 $_SESSION["uid"]);
181
182                         $cats = array();
183
184                         while ($line = db_fetch_assoc($result)) {
185                                 $unread = getFeedUnread($link, $line["id"], true);
186
187                                 if ($unread || !$unread_only) {
188                                         array_push($cats, array("id" => $line["id"],
189                                                 "title" => $line["title"],
190                                                 "unread" => $unread));
191                                 }
192                         }
193
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
204                         print api_wrap_reply(API_STATUS_OK, $seq, $cats);
205                         break;
206
207                 case "getHeadlines":
208                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
209                         $limit = (int)db_escape_string($_REQUEST["limit"]);
210                         $offset = (int)db_escape_string($_REQUEST["skip"]);
211                         $filter = db_escape_string($_REQUEST["filter"]);
212                         $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
213                         $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
214                         $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
215                         /* all_articles, unread, adaptive, marked, updated */
216                         $view_mode = db_escape_string($_REQUEST["view_mode"]);
217                         $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
218                         $since_id = (int)db_escape_string($_REQUEST["since_id"]);
219
220                         $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
221                                 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
222                                 $include_attachments, $since_id);
223
224                         print api_wrap_reply(API_STATUS_OK, $seq, $headlines);
225
226                         break;
227
228                 case "updateArticle":
229                         $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
230                         $mode = (int) db_escape_string($_REQUEST["mode"]);
231                         $data = db_escape_string($_REQUEST["data"]);
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;
247                                 case 3:
248                                         $field = "note";
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
263                         if ($field == "note") $set_to = "'$data'";
264
265                         if ($field && $set_to && count($article_ids) > 0) {
266
267                                 $article_ids = join(", ", $article_ids);
268
269                                 if ($field == "unread") {
270                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
271                                                 last_read = NOW()
272                                                 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
273                                 } else {
274                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
275                                                 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
276                                 }
277
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
289                                 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK",
290                                         "updated" => $num_updated));
291
292                         } else {
293                                 print api_wrap_reply(API_STATUS_ERR, $seq,
294                                         array("error" => 'INCORRECT_USAGE'));
295                         }
296
297                         break;
298
299                 case "getArticle":
300
301                         $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric));
302
303                         $query = "SELECT id,title,link,content,feed_id,comments,int_id,
304                                 marked,unread,published,
305                                 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
306                                 author
307                                 FROM ttrss_entries,ttrss_user_entries
308                                 WHERE   id IN ($article_id) AND ref_id = id AND owner_uid = " .
309                                         $_SESSION["uid"] ;
310
311                         $result = db_query($link, $query);
312
313                         $articles = array();
314
315                         if (db_num_rows($result) != 0) {
316
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
337                                         array_push($articles, $article);
338
339                                 }
340                         }
341
342                         print api_wrap_reply(API_STATUS_OK, $seq, $articles);
343
344                         break;
345
346                 case "getConfig":
347                         $config = array(
348                                 "icons_dir" => ICONS_DIR,
349                                 "icons_url" => ICONS_URL);
350
351                         $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
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;
359
360                         print api_wrap_reply(API_STATUS_OK, $seq, $config);
361
362                         break;
363
364                 case "updateFeed":
365                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
366
367                         update_rss_feed($link, $feed_id, true);
368
369                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
370
371                         break;
372
373                 case "catchupFeed":
374                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
375                         $is_cat = db_escape_string($_REQUEST["is_cat"]);
376
377                         catchup_feed($link, $feed_id, $is_cat);
378
379                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
380
381                         break;
382
383                 case "getPref":
384                         $pref_name = db_escape_string($_REQUEST["pref_name"]);
385
386                         print api_wrap_reply(API_STATUS_OK, $seq,
387                                 array("value" => get_pref($link, $pref_name)));
388                         break;
389
390                 default:
391                         print api_wrap_reply(API_STATUS_ERR, $seq,
392                                 array("error" => 'UNKNOWN_METHOD'));
393                         break;
394
395         }
396
397         db_close($link);
398
399 ?>