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