]> git.wh0rd.org Git - tt-rss.git/blob - api/index.php
add per-user option to enable access to API
[tt-rss.git] / api / index.php
1 <?php
2
3         /* This is experimental JSON-based API. It has to be manually enabled:
4          * 
5          * Add define('_JSON_API_ENABLED', true) to config.php
6          */
7
8         error_reporting(E_ERROR | E_PARSE);
9
10         require_once "../config.php";
11         
12         require_once "../db.php";
13         require_once "../db-prefs.php";
14         require_once "../functions.php";
15
16         if (!defined('_JSON_API_ENABLED')) {
17                 print json_encode(array("error" => "API_DISABLED"));
18                 return;
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_start();
27
28         if (!$link) {
29                 if (DB_TYPE == "mysql") {
30                         print mysql_error();
31                 }
32                 // PG seems to display its own errors just fine by default.             
33                 return;
34         }
35
36         init_connection($link);
37
38         $op = db_escape_string($_REQUEST["op"]);
39
40 //      header("Content-Type: application/json");
41
42         if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
43                 print json_encode(array("error" => 'NOT_LOGGED_IN'));
44                 return;
45         }
46
47         if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
48                 print json_encode(array("error" => 'API_DISABLED'));
49                 return;
50         } 
51
52         switch ($op) {
53                 case "getVersion":
54                         $rv = array("version" => VERSION);
55                         print json_encode($rv);
56                 break;
57                 case "login":
58                         $login = db_escape_string($_REQUEST["user"]);
59                         $password = db_escape_string($_REQUEST["password"]);
60
61                         if (authenticate_user($link, $login, $password)) {
62                                 print json_encode(array("uid" => $_SESSION["uid"]));
63                         } else {
64                                 print json_encode(array("error" => "LOGIN_ERROR"));
65                         }
66
67                         break;
68                 case "logout":
69                         logout_user();
70                         print json_encode(array("uid" => 0));
71                         break;
72                 case "isLoggedIn":
73                         print json_encode(array("status" => $_SESSION["uid"] != ''));
74                         break;
75                 case "getUnread":
76                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
77                         $is_cat = db_escape_string($_REQUEST["is_cat"]);
78
79                         if ($feed_id) {
80                                 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
81                         } else {
82                                 print json_encode(array("unread" => getGlobalUnread($link)));
83                         }
84                         break;
85                 case "getFeeds":
86                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
87                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
88
89                         if (!$cat_id) {
90                                 $result = db_query($link, "SELECT 
91                                         id, feed_url, cat_id, title, ".
92                                                 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
93                                                 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
94                         } else {
95                                 $result = db_query($link, "SELECT 
96                                         id, feed_url, cat_id, title, ".
97                                                 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
98                                                 FROM ttrss_feeds WHERE 
99                                                         cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
100                         }
101
102                         $feeds = array();
103
104                         while ($line = db_fetch_assoc($result)) {
105
106                                 $unread = getFeedUnread($link, $line["id"]);
107
108                                 if ($unread || !$unread_only) {
109
110                                         $row = array(
111                                                         "feed_url" => $line["feed_url"],
112                                                         "title" => $line["title"],
113                                                         "id" => (int)$line["id"],
114                                                         "unread" => (int)$unread,
115                                                         "cat_id" => (int)$line["cat_id"],
116                                                         "last_updated" => strtotime($line["last_updated"])
117                                                 );
118         
119                                         array_push($feeds, $row);
120                                 }
121                         }
122
123                         /* Labels */
124
125                         if (!$cat_id || $cat_id == -2) {
126                                 $counters = getLabelCounters($link, false, true);
127
128                                 foreach (array_keys($counters) as $id) {
129
130                                         $unread = $counters[$id]["counter"];
131         
132                                         if ($unread || !$unread_only) {
133         
134                                                 $row = array(
135                                                                 "id" => $id,
136                                                                 "title" => $counters[$id]["description"],
137                                                                 "unread" => $counters[$id]["counter"],
138                                                                 "cat_id" => -2,
139                                                         );
140         
141                                                 array_push($feeds, $row);
142                                         }
143                                 }
144                         }
145
146                         /* Virtual feeds */
147
148                         if (!$cat_id || $cat_id == -1) {
149                                 foreach (array(-1, -2, -3, -4) as $i) {
150                                         $unread = getFeedUnread($link, $i);
151
152                                         if ($unread || !$unread_only) {
153                                                 $title = getFeedTitle($link, $i);
154
155                                                 $row = array(
156                                                                 "id" => $i,
157                                                                 "title" => $title,
158                                                                 "unread" => $unread,
159                                                                 "cat_id" => -1,
160                                                         );
161                                                 array_push($feeds, $row);
162                                         }
163
164                                 }
165                         }
166
167                         print json_encode($feeds);
168
169                         break;
170                 case "getCategories":
171                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
172
173                         $result = db_query($link, "SELECT 
174                                         id, title FROM ttrss_feed_categories 
175                                 WHERE owner_uid = " . 
176                                 $_SESSION["uid"]);
177
178                         $cats = array();
179
180                         while ($line = db_fetch_assoc($result)) {
181                                 $unread = getFeedUnread($link, $line["id"], true);
182
183                                 if ($unread || !$unread_only) {
184                                         array_push($cats, array("id" => $line["id"],
185                                                 "title" => $line["title"], 
186                                                 "unread" => $unread));
187                                 }
188                         }
189
190                         print json_encode($cats);
191                         break;
192                 case "getHeadlines":
193                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
194                         $limit = (int)db_escape_string($_REQUEST["limit"]);
195                         $filter = db_escape_string($_REQUEST["filter"]);
196                         $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
197                         $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
198
199                         /* do not rely on params below */
200
201                         $search = db_escape_string($_REQUEST["search"]);
202                         $search_mode = db_escape_string($_REQUEST["search_mode"]);
203                         $match_on = db_escape_string($_REQUEST["match_on"]);
204                         
205                         $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
206                                 $view_mode, $is_cat, $search, $search_mode, $match_on);
207
208                         $result = $qfh_ret[0];
209                         $feed_title = $qfh_ret[1];
210
211                         $headlines = array();
212
213                         while ($line = db_fetch_assoc($result)) {
214                                 $is_updated = ($line["last_read"] == "" && 
215                                         ($line["unread"] != "t" && $line["unread"] != "1"));
216
217                                 $headline_row = array(
218                                                 "id" => (int)$line["id"],
219                                                 "unread" => sql_bool_to_bool($line["unread"]),
220                                                 "marked" => sql_bool_to_bool($line["marked"]),
221                                                 "updated" => strtotime($line["updated"]),
222                                                 "is_updated" => $is_updated,
223                                                 "title" => $line["title"],
224                                                 "feed_id" => $line["feed_id"],
225                                         );
226
227                                 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
228                         
229                                 array_push($headlines, $headline_row);
230                         }
231
232                         print json_encode($headlines);
233
234                         break;
235                 case "updateArticle":
236                         $article_id = (int) db_escape_string($_GET["article_id"]);
237                         $mode = (int) db_escape_string($_REQUEST["mode"]);
238                         $field_raw = (int)db_escape_string($_REQUEST["field"]);
239
240                         $field = "";
241                         $set_to = "";
242
243                         switch ($field_raw) {
244                                 case 0:
245                                         $field = "marked";
246                                         break;
247                                 case 1:
248                                         $field = "published";
249                                         break;
250                                 case 2:
251                                         $field = "unread";
252                                         break;
253                         };
254
255                         switch ($mode) {
256                                 case 1:
257                                         $set_to = "true";
258                                         break;
259                                 case 0:
260                                         $set_to = "false";
261                                         break;
262                                 case 2:
263                                         $set_to = "NOT $field";
264                                         break;
265                         }
266
267                         if ($field && $set_to) {
268                                 if ($field == "unread") {
269                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
270                                                 last_read = NOW()
271                                                 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
272                                 } else {
273                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
274                                                 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
275                                 }
276                         }
277
278                         break;
279
280                 case "getArticle":
281
282                         $article_id = (int)db_escape_string($_REQUEST["article_id"]);
283
284                         $query = "SELECT title,link,content,feed_id,comments,int_id,
285                                 marked,unread,published,
286                                 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
287                                 author
288                                 FROM ttrss_entries,ttrss_user_entries
289                                 WHERE   id = '$article_id' AND ref_id = id AND owner_uid = " . 
290                                         $_SESSION["uid"] ;
291
292                         $result = db_query($link, $query);
293
294                         $article = array();
295                         
296                         if (db_num_rows($result) != 0) {
297                                 $line = db_fetch_assoc($result);
298         
299                                 $article = array(
300                                         "title" => $line["title"],
301                                         "link" => $line["link"],
302                                         "labels" => get_article_labels($link, $article_id),
303                                         "unread" => sql_bool_to_bool($line["unread"]),
304                                         "marked" => sql_bool_to_bool($line["marked"]),
305                                         "published" => sql_bool_to_bool($line["published"]),
306                                         "comments" => $line["comments"],
307                                         "author" => $line["author"],
308                                         "updated" => strtotime($line["updated"]),
309                                         "content" => $line["content"],
310                                         "feed_id" => $line["feed_id"],                  
311                                 );
312                         }
313
314                         print json_encode($article);
315
316                         break;
317         }
318
319         db_close($link);
320         
321 ?>