]> git.wh0rd.org Git - tt-rss.git/blob - api/index.php
fix API includes
[tt-rss.git] / api / index.php
1 <?php
2         error_reporting(E_ERROR | E_PARSE);
3
4         require_once "../config.php";
5
6         set_include_path(get_include_path() . PATH_SEPARATOR .
7                 dirname(__FILE__) . PATH_SEPARATOR .
8                 dirname(dirname(__FILE__)) . PATH_SEPARATOR .
9                 dirname(dirname(__FILE__)) . "/include" );
10
11         require_once "db.php";
12         require_once "db-prefs.php";
13         require_once "functions.php";
14
15         define('API_LEVEL', 1);
16
17         define('API_STATUS_OK', 0);
18         define('API_STATUS_ERR', 1);
19
20         chdir("..");
21
22         if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) {
23                 ob_start("ob_gzhandler");
24         }
25
26         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
27
28         $session_expire = SESSION_EXPIRE_TIME; //seconds
29         $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
30
31         session_name($session_name);
32
33         $input = file_get_contents("php://input");
34
35         // Override $_REQUEST with JSON-encoded data if available
36         if ($input) {
37                 $input = json_decode($input, true);
38
39                 if ($input) {
40                         $_REQUEST = $input;
41                 }
42         }
43
44         if ($_REQUEST["sid"]) {
45                 session_id($_REQUEST["sid"]);
46         }
47
48         session_start();
49
50         if (!$link) {
51                 if (DB_TYPE == "mysql") {
52                         print mysql_error();
53                 }
54                 // PG seems to display its own errors just fine by default.
55                 return;
56         }
57
58         init_connection($link);
59
60         $op = db_escape_string($_REQUEST["op"]);
61         $seq = (int) $_REQUEST["seq"];
62
63         header("Content-Type: text/plain");
64
65         function api_wrap_reply($status, $seq, $reply) {
66                 print json_encode(array("seq" => $seq,
67                         "status" => $status,
68                         "content" => $reply));
69         }
70
71         if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
72                 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN'));
73                 return;
74         }
75
76         if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
77                 print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED'));
78                 return;
79         }
80
81         switch ($op) {
82
83                 case "getVersion":
84                         $rv = array("version" => VERSION);
85                         print api_wrap_reply(API_STATUS_OK, $seq, $rv);
86                         break;
87
88                 case "getApiLevel":
89                         $rv = array("level" => API_LEVEL);
90                         print api_wrap_reply(API_STATUS_OK, $seq, $rv);
91                         break;
92
93                 case "login":
94                         $login = db_escape_string($_REQUEST["user"]);
95                         $password = $_REQUEST["password"];
96                         $password_base64 = base64_decode($_REQUEST["password"]);
97
98                         if (SINGLE_USER_MODE) $login = "admin";
99
100                         $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
101
102                         if (db_num_rows($result) != 0) {
103                                 $uid = db_fetch_result($result, 0, "id");
104                         } else {
105                                 $uid = 0;
106                         }
107
108                         if (!$uid) {
109                                 print api_wrap_reply(API_STATUS_ERR, $seq,
110                                         array("error" => "LOGIN_ERROR"));
111                                 return;
112                         }
113
114                         if (get_pref($link, "ENABLE_API_ACCESS", $uid)) {
115                                 if (authenticate_user($link, $login, $password)) {               // try login with normal password
116                                         print api_wrap_reply(API_STATUS_OK, $seq,
117                                                 array("session_id" => session_id()));
118                                 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
119                                         print api_wrap_reply(API_STATUS_OK, $seq,
120                                                 array("session_id" => session_id()));
121                                 } else {                                                         // else we are not logged in
122                                         print api_wrap_reply(API_STATUS_ERR, $seq,
123                                                 array("error" => "LOGIN_ERROR"));
124                                 }
125                         } else {
126                                 print api_wrap_reply(API_STATUS_ERR, $seq,
127                                         array("error" => "API_DISABLED"));
128                         }
129
130                         break;
131
132                 case "logout":
133                         logout_user();
134                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
135                         break;
136
137                 case "isLoggedIn":
138                         print api_wrap_reply(API_STATUS_OK, $seq,
139                                 array("status" => $_SESSION["uid"] != ''));
140                         break;
141
142                 case "getUnread":
143                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
144                         $is_cat = db_escape_string($_REQUEST["is_cat"]);
145
146                         if ($feed_id) {
147                                 print api_wrap_reply(API_STATUS_OK, $seq,
148                                         array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
149                         } else {
150                                 print api_wrap_reply(API_STATUS_OK, $seq,
151                                         array("unread" => getGlobalUnread($link)));
152                         }
153                         break;
154
155                 /* Method added for ttrss-reader for Android */
156                 case "getCounters":
157
158                         /* flct (flc is the default) FIXME: document */
159                         $output_mode = db_escape_string($_REQUEST["output_mode"]);
160
161                         print api_wrap_reply(API_STATUS_OK, $seq,
162                                 getAllCounters($link, $output_mode));
163                         break;
164
165                 case "getFeeds":
166                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
167                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
168                         $limit = (int) db_escape_string($_REQUEST["limit"]);
169                         $offset = (int) db_escape_string($_REQUEST["offset"]);
170
171                         $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
172
173                         print api_wrap_reply(API_STATUS_OK, $seq, $feeds);
174
175                         break;
176
177                 case "getCategories":
178                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
179
180                         // TODO do not return empty categories, return Uncategorized and standard virtual cats
181
182                         $result = db_query($link, "SELECT
183                                         id, title FROM ttrss_feed_categories
184                                 WHERE owner_uid = " .
185                                 $_SESSION["uid"]);
186
187                         $cats = array();
188
189                         while ($line = db_fetch_assoc($result)) {
190                                 $unread = getFeedUnread($link, $line["id"], true);
191
192                                 if ($unread || !$unread_only) {
193                                         array_push($cats, array("id" => $line["id"],
194                                                 "title" => $line["title"],
195                                                 "unread" => $unread));
196                                 }
197                         }
198
199                         foreach (array(-2,-1,0) as $cat_id) {
200                                 $unread = getFeedUnread($link, $cat_id, true);
201
202                                 if ($unread || !$unread_only) {
203                                         array_push($cats, array("id" => $cat_id,
204                                                 "title" => getCategoryTitle($link, $cat_id),
205                                                 "unread" => $unread));
206                                 }
207                         }
208
209                         print api_wrap_reply(API_STATUS_OK, $seq, $cats);
210                         break;
211
212                 case "getHeadlines":
213                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
214                         $limit = (int)db_escape_string($_REQUEST["limit"]);
215                         $offset = (int)db_escape_string($_REQUEST["skip"]);
216                         $filter = db_escape_string($_REQUEST["filter"]);
217                         $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
218                         $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
219                         $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
220                         /* all_articles, unread, adaptive, marked, updated */
221                         $view_mode = db_escape_string($_REQUEST["view_mode"]);
222                         $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]);
223                         $since_id = (int)db_escape_string($_REQUEST["since_id"]);
224
225                         $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
226                                 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false,
227                                 $include_attachments, $since_id);
228
229                         print api_wrap_reply(API_STATUS_OK, $seq, $headlines);
230
231                         break;
232
233                 case "updateArticle":
234                         $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric);
235                         $mode = (int) db_escape_string($_REQUEST["mode"]);
236                         $data = db_escape_string($_REQUEST["data"]);
237                         $field_raw = (int)db_escape_string($_REQUEST["field"]);
238
239                         $field = "";
240                         $set_to = "";
241
242                         switch ($field_raw) {
243                                 case 0:
244                                         $field = "marked";
245                                         break;
246                                 case 1:
247                                         $field = "published";
248                                         break;
249                                 case 2:
250                                         $field = "unread";
251                                         break;
252                                 case 3:
253                                         $field = "note";
254                         };
255
256                         switch ($mode) {
257                                 case 1:
258                                         $set_to = "true";
259                                         break;
260                                 case 0:
261                                         $set_to = "false";
262                                         break;
263                                 case 2:
264                                         $set_to = "NOT $field";
265                                         break;
266                         }
267
268                         if ($field == "note") $set_to = "'$data'";
269
270                         if ($field && $set_to && count($article_ids) > 0) {
271
272                                 $article_ids = join(", ", $article_ids);
273
274                                 if ($field == "unread") {
275                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
276                                                 last_read = NOW()
277                                                 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
278                                 } else {
279                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
280                                                 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
281                                 }
282
283                                 $num_updated = db_affected_rows($link, $result);
284
285                                 if ($num_updated > 0 && $field == "unread") {
286                                         $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
287                                                 WHERE ref_id IN ($article_ids)");
288
289                                         while ($line = db_fetch_assoc($result)) {
290                                                 ccache_update($link, $line["feed_id"], $_SESSION["uid"]);
291                                         }
292                                 }
293
294                                 print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK",
295                                         "updated" => $num_updated));
296
297                         } else {
298                                 print api_wrap_reply(API_STATUS_ERR, $seq,
299                                         array("error" => 'INCORRECT_USAGE'));
300                         }
301
302                         break;
303
304                 case "getArticle":
305
306                         $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric));
307
308                         $query = "SELECT id,title,link,content,feed_id,comments,int_id,
309                                 marked,unread,published,
310                                 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
311                                 author
312                                 FROM ttrss_entries,ttrss_user_entries
313                                 WHERE   id IN ($article_id) AND ref_id = id AND owner_uid = " .
314                                         $_SESSION["uid"] ;
315
316                         $result = db_query($link, $query);
317
318                         $articles = array();
319
320                         if (db_num_rows($result) != 0) {
321
322                                 while ($line = db_fetch_assoc($result)) {
323
324                                         $attachments = get_article_enclosures($link, $line['id']);
325
326                                         $article = array(
327                                                 "id" => $line["id"],
328                                                 "title" => $line["title"],
329                                                 "link" => $line["link"],
330                                                 "labels" => get_article_labels($link, $line['id']),
331                                                 "unread" => sql_bool_to_bool($line["unread"]),
332                                                 "marked" => sql_bool_to_bool($line["marked"]),
333                                                 "published" => sql_bool_to_bool($line["published"]),
334                                                 "comments" => $line["comments"],
335                                                 "author" => $line["author"],
336                                                 "updated" => strtotime($line["updated"]),
337                                                 "content" => $line["content"],
338                                                 "feed_id" => $line["feed_id"],
339                                                 "attachments" => $attachments
340                                         );
341
342                                         array_push($articles, $article);
343
344                                 }
345                         }
346
347                         print api_wrap_reply(API_STATUS_OK, $seq, $articles);
348
349                         break;
350
351                 case "getConfig":
352                         $config = array(
353                                 "icons_dir" => ICONS_DIR,
354                                 "icons_url" => ICONS_URL);
355
356                         $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
357
358                         $result = db_query($link, "SELECT COUNT(*) AS cf FROM
359                                 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
360
361                         $num_feeds = db_fetch_result($result, 0, "cf");
362
363                         $config["num_feeds"] = (int)$num_feeds;
364
365                         print api_wrap_reply(API_STATUS_OK, $seq, $config);
366
367                         break;
368
369                 case "updateFeed":
370                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
371
372                         update_rss_feed($link, $feed_id, true);
373
374                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
375
376                         break;
377
378                 case "catchupFeed":
379                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
380                         $is_cat = db_escape_string($_REQUEST["is_cat"]);
381
382                         catchup_feed($link, $feed_id, $is_cat);
383
384                         print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK"));
385
386                         break;
387
388                 case "getPref":
389                         $pref_name = db_escape_string($_REQUEST["pref_name"]);
390
391                         print api_wrap_reply(API_STATUS_OK, $seq,
392                                 array("value" => get_pref($link, $pref_name)));
393                         break;
394
395                 default:
396                         print api_wrap_reply(API_STATUS_ERR, $seq,
397                                 array("error" => 'UNKNOWN_METHOD'));
398                         break;
399
400         }
401
402         db_close($link);
403
404 ?>