]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: show virtual feeds in getFeeds
[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 /* TODO: add pref key to disable/enable API
48 if ($_SESSION["uid"] && !get_pref($link, 'API_ENABLED')) {
49 print json_encode(array("error" => 'API_DISABLED'));
50 return;
51 } */
52
53 switch ($op) {
54 case "getVersion":
55 $rv = array("version" => VERSION);
56 print json_encode($rv);
57 break;
58 case "login":
59 $login = db_escape_string($_REQUEST["user"]);
60 $password = db_escape_string($_REQUEST["password"]);
61
62 if (authenticate_user($link, $login, $password)) {
63 print json_encode(array("uid" => $_SESSION["uid"]));
64 } else {
65 print json_encode(array("uid" => 0));
66 }
67
68 break;
69 case "logout":
70 logout_user();
71 print json_encode(array("uid" => 0));
72 break;
73 case "isLoggedIn":
74 print json_encode(array("status" => $_SESSION["uid"] != ''));
75 break;
76 case "getUnread":
77 $feed_id = db_escape_string($_REQUEST["feed_id"]);
78 $is_cat = db_escape_string($_REQUEST["is_cat"]);
79
80 if ($feed_id) {
81 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
82 } else {
83 print json_encode(array("unread" => getGlobalUnread($link)));
84 }
85 break;
86 case "getFeeds":
87 $cat_id = db_escape_string($_REQUEST["cat_id"]);
88 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
89
90 if (!$cat_id) {
91 $result = db_query($link, "SELECT
92 id, feed_url, cat_id, title, ".
93 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
94 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
95 } else {
96 $result = db_query($link, "SELECT
97 id, feed_url, cat_id, title, ".
98 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
99 FROM ttrss_feeds WHERE
100 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]);
101 }
102
103 $feeds = array();
104
105 while ($line = db_fetch_assoc($result)) {
106
107 $unread = getFeedUnread($link, $line["id"]);
108
109 if ($unread || !$unread_only) {
110
111 $row = array(
112 "feed_url" => $line["feed_url"],
113 "title" => $line["title"],
114 "id" => (int)$line["id"],
115 "unread" => (int)$unread,
116 "cat_id" => (int)$line["cat_id"],
117 "last_updated" => strtotime($line["last_updated"])
118 );
119
120 array_push($feeds, $row);
121 }
122 }
123
124 if (!$cat_id || $cat_id == -1) {
125 $counters = getLabelCounters($link, false, true);
126
127 foreach (array_keys($counters) as $id) {
128
129 $unread = $counters[$id]["counter"];
130
131 if ($unread || !$unread_only) {
132
133 $row = array(
134 "id" => $id,
135 "title" => $counters[$id]["description"],
136 "unread" => $counters[$id]["counter"],
137 "cat_id" => -1,
138 );
139
140 array_push($feeds, $row);
141 }
142 }
143 }
144
145 print json_encode($feeds);
146
147 break;
148 case "getCategories":
149 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
150
151 $result = db_query($link, "SELECT
152 id, title FROM ttrss_feed_categories
153 WHERE owner_uid = " .
154 $_SESSION["uid"]);
155
156 $cats = array();
157
158 while ($line = db_fetch_assoc($result)) {
159 $unread = getFeedUnread($link, $line["id"], true);
160
161 if ($unread || !$unread_only) {
162 array_push($cats, array($line["id"] =>
163 array("title" => $line["title"], "unread" => $unread)));
164 }
165 }
166
167 print json_encode($cats);
168 break;
169 case "getHeadlines":
170 $feed_id = db_escape_string($_REQUEST["feed_id"]);
171 $limit = (int)db_escape_string($_REQUEST["limit"]);
172 $filter = db_escape_string($_REQUEST["filter"]);
173 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
174 $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]);
175
176 /* do not rely on params below */
177
178 $search = db_escape_string($_REQUEST["search"]);
179 $search_mode = db_escape_string($_REQUEST["search_mode"]);
180 $match_on = db_escape_string($_REQUEST["match_on"]);
181
182 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
183 $view_mode, $is_cat, $search, $search_mode, $match_on);
184
185 $result = $qfh_ret[0];
186 $feed_title = $qfh_ret[1];
187
188 $headlines = array();
189
190 while ($line = db_fetch_assoc($result)) {
191 $is_updated = ($line["last_read"] == "" &&
192 ($line["unread"] != "t" && $line["unread"] != "1"));
193
194 $headline_row = array(
195 "id" => (int)$line["id"],
196 "unread" => sql_bool_to_bool($line["unread"]),
197 "marked" => sql_bool_to_bool($line["marked"]),
198 "updated" => strtotime($line["updated"]),
199 "is_updated" => $is_updated,
200 "title" => $line["title"],
201 "feed_id" => $line["feed_id"],
202 );
203
204 if ($show_except) $headline_row["excerpt"] = $line["content_preview"];
205
206 array_push($headlines, $headline_row);
207 }
208
209 print json_encode($headlines);
210
211 break;
212 case "updateArticle":
213 $article_id = (int) db_escape_string($_GET["article_id"]);
214 $mode = (int) db_escape_string($_REQUEST["mode"]);
215 $field_raw = (int)db_escape_string($_REQUEST["field"]);
216
217 $field = "";
218 $set_to = "";
219
220 switch ($field_raw) {
221 case 0:
222 $field = "marked";
223 break;
224 case 1:
225 $field = "published";
226 break;
227 case 2:
228 $field = "unread";
229 break;
230 };
231
232 switch ($mode) {
233 case 1:
234 $set_to = "true";
235 break;
236 case 0:
237 $set_to = "false";
238 break;
239 case 2:
240 $set_to = "NOT $field";
241 break;
242 }
243
244 if ($field && $set_to) {
245 if ($field == "unread") {
246 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
247 last_read = NOW()
248 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
249 } else {
250 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
251 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
252 }
253 }
254
255 break;
256
257 case "getArticle":
258
259 $article_id = (int)db_escape_string($_REQUEST["article_id"]);
260
261 $query = "SELECT title,link,content,feed_id,comments,int_id,
262 marked,unread,published,
263 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
264 author
265 FROM ttrss_entries,ttrss_user_entries
266 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " .
267 $_SESSION["uid"] ;
268
269 $result = db_query($link, $query);
270
271 $article = array();
272
273 if (db_num_rows($result) != 0) {
274 $line = db_fetch_assoc($result);
275
276 $article = array(
277 "title" => $line["title"],
278 "link" => $line["link"],
279 "unread" => sql_bool_to_bool($line["unread"]),
280 "marked" => sql_bool_to_bool($line["marked"]),
281 "published" => sql_bool_to_bool($line["published"]),
282 "comments" => $line["comments"],
283 "author" => $line["author"],
284 "updated" => strtotime($line["updated"]),
285 "content" => $line["content"],
286 "feed_id" => $line["feed_id"],
287 );
288 }
289
290 print json_encode($article);
291
292 break;
293 }
294
295 db_close($link);
296
297 ?>