]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: rewrite getCounters to use getAllCounters()
[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 if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) {
11 ob_start("ob_gzhandler");
12 }
13
14 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
15
16 $session_expire = SESSION_EXPIRE_TIME; //seconds
17 $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api";
18
19 session_name($session_name);
20
21 if ($_REQUEST["sid"]) {
22 session_id($_REQUEST["sid"]);
23 }
24
25 session_start();
26
27 if (!$link) {
28 if (DB_TYPE == "mysql") {
29 print mysql_error();
30 }
31 // PG seems to display its own errors just fine by default.
32 return;
33 }
34
35 init_connection($link);
36
37 $op = db_escape_string($_REQUEST["op"]);
38
39 // header("Content-Type: application/json");
40
41 if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") {
42 print json_encode(array("error" => 'NOT_LOGGED_IN'));
43 return;
44 }
45
46 if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) {
47 print json_encode(array("error" => 'API_DISABLED'));
48 return;
49 }
50
51 switch ($op) {
52
53 case "getVersion":
54 $rv = array("version" => VERSION);
55 print json_encode($rv);
56 break;
57
58 case "login":
59 $login = db_escape_string($_REQUEST["user"]);
60 $password = db_escape_string($_REQUEST["password"]);
61 $password_base64 = db_escape_string(base64_decode($_REQUEST["password"]));
62
63 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'");
64
65 if (db_num_rows($result) != 0) {
66 $uid = db_fetch_result($result, 0, "id");
67 } else {
68 $uid = 0;
69 }
70
71 if ($uid && get_pref($link, "ENABLE_API_ACCESS", $uid)) {
72 if (authenticate_user($link, $login, $password)) { // try login with normal password
73 print json_encode(array("session_id" => session_id()));
74 } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password
75 print json_encode(array("session_id" => session_id()));
76 } else { // else we are not logged in
77 print json_encode(array("error" => "LOGIN_ERROR"));
78 }
79 } else {
80 print json_encode(array("error" => "API_DISABLED"));
81 }
82
83 break;
84
85 case "logout":
86 logout_user();
87 print json_encode(array("status" => "OK"));
88 break;
89
90 case "isLoggedIn":
91 print json_encode(array("status" => $_SESSION["uid"] != ''));
92 break;
93
94 case "getUnread":
95 $feed_id = db_escape_string($_REQUEST["feed_id"]);
96 $is_cat = db_escape_string($_REQUEST["is_cat"]);
97
98 if ($feed_id) {
99 print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat)));
100 } else {
101 print json_encode(array("unread" => getGlobalUnread($link)));
102 }
103 break;
104
105 /* Method added for ttrss-reader for Android */
106 case "getCounters":
107
108 /* flct (flc is the default) FIXME: document */
109 $output_mode = db_escape_string($_REQUEST["output_mode"]);
110
111 print json_encode(getAllCounters($link, $output_mode));
112 break;
113
114 case "getFeeds":
115 $cat_id = db_escape_string($_REQUEST["cat_id"]);
116 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
117 $limit = (int) db_escape_string($_REQUEST["limit"]);
118 $offset = (int) db_escape_string($_REQUEST["offset"]);
119
120 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
121
122 print json_encode($feeds);
123
124 break;
125
126 case "getCategories":
127 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
128
129 $result = db_query($link, "SELECT
130 id, title FROM ttrss_feed_categories
131 WHERE owner_uid = " .
132 $_SESSION["uid"]);
133
134 $cats = array();
135
136 while ($line = db_fetch_assoc($result)) {
137 $unread = getFeedUnread($link, $line["id"], true);
138
139 if ($unread || !$unread_only) {
140 array_push($cats, array("id" => $line["id"],
141 "title" => $line["title"],
142 "unread" => $unread));
143 }
144 }
145
146 print json_encode($cats);
147 break;
148
149 case "getHeadlines":
150 $feed_id = db_escape_string($_REQUEST["feed_id"]);
151 $limit = (int)db_escape_string($_REQUEST["limit"]);
152 $offset = (int)db_escape_string($_REQUEST["skip"]);
153 $filter = db_escape_string($_REQUEST["filter"]);
154 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
155 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
156 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
157 /* all_articles, unread, adaptive, marked, updated */
158 $view_mode = db_escape_string($_REQUEST["view_mode"]);
159
160 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
161 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
162
163 print json_encode($headlines);
164
165 break;
166
167 case "updateArticle":
168 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
169 $mode = (int) db_escape_string($_REQUEST["mode"]);
170 $field_raw = (int)db_escape_string($_REQUEST["field"]);
171
172 $field = "";
173 $set_to = "";
174
175 switch ($field_raw) {
176 case 0:
177 $field = "marked";
178 break;
179 case 1:
180 $field = "published";
181 break;
182 case 2:
183 $field = "unread";
184 break;
185 };
186
187 switch ($mode) {
188 case 1:
189 $set_to = "true";
190 break;
191 case 0:
192 $set_to = "false";
193 break;
194 case 2:
195 $set_to = "NOT $field";
196 break;
197 }
198
199 if ($field && $set_to && count($article_ids) > 0) {
200
201 $article_ids = join(", ", $article_ids);
202
203 if ($field == "unread") {
204 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
205 last_read = NOW()
206 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
207 } else {
208 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
209 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
210 }
211
212 // FIXME: find out which feeds reference this article id and do ccache_update() on them
213 }
214
215 break;
216
217 case "getArticle":
218
219 $article_id = db_escape_string($_REQUEST["article_id"]);
220
221 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
222 marked,unread,published,
223 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
224 author
225 FROM ttrss_entries,ttrss_user_entries
226 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
227 $_SESSION["uid"] ;
228
229 $result = db_query($link, $query);
230
231 if (db_num_rows($result) != 0) {
232
233 while ($line = db_fetch_assoc($result)) {
234
235 $attachments = get_article_enclosures($link, $line['id']);
236
237 $article = array(
238 "id" => $line["id"],
239 "title" => $line["title"],
240 "link" => $line["link"],
241 "labels" => get_article_labels($link, $line['id']),
242 "unread" => sql_bool_to_bool($line["unread"]),
243 "marked" => sql_bool_to_bool($line["marked"]),
244 "published" => sql_bool_to_bool($line["published"]),
245 "comments" => $line["comments"],
246 "author" => $line["author"],
247 "updated" => strtotime($line["updated"]),
248 "content" => $line["content"],
249 "feed_id" => $line["feed_id"],
250 "attachments" => $attachments
251 );
252
253 print json_encode($article);
254 }
255 }
256
257 break;
258
259 case "getConfig":
260 $config = array(
261 "icons_dir" => ICONS_DIR,
262 "icons_url" => ICONS_URL);
263
264 if (ENABLE_UPDATE_DAEMON) {
265 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
266 }
267
268 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
269 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
270
271 $num_feeds = db_fetch_result($result, 0, "cf");
272
273 $config["num_feeds"] = (int)$num_feeds;
274
275 print json_encode($config);
276
277 break;
278
279 case "updateFeed":
280 $feed_id = db_escape_string($_REQUEST["feed_id"]);
281
282 update_rss_feed($link, $feed_id, true);
283
284 print json_encode(array("status" => "OK"));
285
286 break;
287
288 case "catchupFeed":
289 $feed_id = db_escape_string($_REQUEST["feed_id"]);
290 $is_cat = db_escape_string($_REQUEST["category"]);
291
292 catchup_feed($link, $feed_id, $is_cat);
293
294 print json_encode(array("status" => "OK"));
295
296 break;
297
298 case "getPref":
299 $pref_name = db_escape_string($_REQUEST["pref_name"]);
300 print json_encode(array("value" => get_pref($link, $pref_name)));
301 break;
302
303 /* Method added for ttrss-reader for Android */
304 case "getArticles":
305 $isCategory = (int)db_escape_string($_REQUEST["is_category"]);
306 $id = (int)db_escape_string($_REQUEST["id"]);
307 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
308 $limit = (int)db_escape_string($_REQUEST["limit"]);
309 $feeds = array();
310
311 if ($isCategory > 0) {
312 // Get Feeds of the category
313
314 if ($id == 0) {
315 $category_part = "cat_id is NULL";
316 } else {
317 $category_part = "cat_id = '$id'";
318 }
319
320 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
321 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
322
323 while ($line = db_fetch_assoc($result)) {
324 array_push($feeds, $line["id"]);
325 }
326
327 // Virtual feeds
328 $match_part = "";
329 if ($id == -1) {
330 $match_part = "marked = true";
331 array_push($feeds, -1);
332 } else if ($id == -2) {
333 $match_part = "published = true";
334 array_push($feeds, -2);
335 } else if ($id == -3) {
336 $match_part = "unread = true";
337 array_push($feeds, -3);
338
339 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
340
341 if (DB_TYPE == "pgsql") {
342 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
343 } else {
344 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
345 }
346 } else if ($id == -4) {
347 $match_part = "true";
348 array_push($feeds, -4);
349 }
350 } else {
351 // Only add one feed
352 array_push($feeds, $id);
353 }
354
355 $ret = array();
356
357 if (DB_TYPE == "mysql") {
358 $limit_part = " LIMIT 0,".$limit;
359 } else if (DB_TYPE == "pgsql") {
360 $limit_part = " LIMIT ".$limit;
361 } else {
362 $limit_part = "";
363 }
364
365 // Fetch articles for the feeds
366 foreach ($feeds as $feed) {
367
368 if ($match_part) {
369 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
370 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
371
372 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
373 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
374 FROM $from_qpart WHERE
375 ttrss_user_entries.ref_id = ttrss_entries.id AND
376 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
377
378 $result = db_query($link, $query);
379 } else {
380 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
381 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
382 FROM ttrss_entries,ttrss_user_entries
383 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
384 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
385
386 $result = db_query($link, $query);
387 }
388
389 $articles = array();
390 $i=0;
391 while ($i < mysql_numrows($result)) {
392
393 $article_id = db_fetch_result($result, $i, "id");
394
395 $attachments = get_article_enclosures($link, $article_id);
396
397 $article = array(
398 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
399 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
400 "link" => db_fetch_result($result, $i, "link"),
401 "labels" => get_article_labels($link, $article_id),
402 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
403 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
404 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
405 "comments" => db_fetch_result($result, $i, "comments"),
406 "author" => db_fetch_result($result, $i, "author"),
407 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
408 "content" => db_fetch_result($result, $i, "content"),
409 "feed_id" => db_fetch_result($result, $i, "feed_id"),
410 "attachments" => $attachments
411 );
412
413 array_push($ret, $article);
414
415 $i++;
416 }
417 }
418
419 print json_encode($ret);
420 break;
421
422 /* Method added for ttrss-reader for Android */
423 case "getNewArticles":
424 $time = (int) db_escape_string($_REQUEST["time"]);
425 // unread=1 zeigt alle an, unread=0 nur ungelesene
426 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
427
428 if (DB_TYPE == "mysql") {
429 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
430 } else if (DB_TYPE == "pgsql") {
431 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
432 } else {
433 $db_time_function = "";
434 }
435
436 if (DB_TYPE == "mysql") {
437 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
438 } else if (DB_TYPE == "pgsql") {
439 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
440 } else {
441 $db_time_function2 = "";
442 }
443
444 $cats = array();
445
446
447 // Add uncategorized feeds
448 $unread = getFeedUnread($link, 0, true);
449 if ($unread || $displayUnread > 0) {
450 $feeds = array();
451 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
452 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
453 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
454
455 while ($line_feeds = db_fetch_assoc($result_0)) {
456 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
457 if ($unread || $displayUnread > 0) {
458
459 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
460 marked,unread,published,".
461 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
462 FROM ttrss_entries,ttrss_user_entries
463 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
464 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
465
466 $articles = array();
467 while ($line_articles = db_fetch_assoc($result_1)) {
468 $article_id = db_fetch_result($result, $i, "id");
469 $attachments = get_article_enclosures($link, $article_id);
470 array_push($articles, $article = array(
471 "id" => $line_articles["id"],
472 "title" => $line_articles["title"],
473 "link" => $line_articles["link"],
474 "labels" => $article_id,
475 "unread" => $line_articles["unread"],
476 "marked" => $line_articles["marked"],
477 "published" => $line_articles["published"],
478 "comments" => $line_articles["comments"],
479 "author" => $line_articles["author"],
480 "updated" => strtotime($line_articles["updated"]),
481 "content" => $line_articles["content"],
482 "feed_id" => $line_articles["feed_id"],
483 "attachments" => $attachments));
484 }
485
486 array_push($feeds, array(
487 "feed_url" => $line_feeds["feed_url"],
488 "title" => $line_feeds["title"],
489 "id" => (int)$line_feeds["id"],
490 "unread" => (int)$unread_feed,
491 "has_icon" => $has_icon,
492 "cat_id" => (int)$line_feeds["cat_id"],
493 "last_updated" => strtotime($line_feeds["last_updated"]),
494 "articles" => $articles
495 ));
496 }
497 }
498
499 array_push($cats,
500 array(
501 "id" => 0,
502 "title" => "Uncategorized Feeds",
503 "unread" => $unread,
504 "feeds" => $feeds));
505 }
506
507
508 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
509 while ($line = db_fetch_assoc($result)) {
510 $unread = getFeedUnread($link, $line["id"], true);
511
512 if ($unread || $displayUnread > 0) {
513 $feeds = array();
514 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
515 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
516 "FROM ttrss_feeds WHERE cat_id = '".
517 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
518
519 while ($line_feeds = db_fetch_assoc($result_0)) {
520 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
521 if ($unread_feed || $displayUnread > 0) {
522
523 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
524 marked,unread,published,".
525 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
526 FROM ttrss_entries,ttrss_user_entries
527 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
528 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
529
530 $articles = array();
531 while ($line_articles = db_fetch_assoc($result_1)) {
532 $article_id = db_fetch_result($result, $i, "id");
533 $attachments = get_article_enclosures($link, $article_id);
534 array_push($articles, $article = array(
535 "id" => $line_articles["id"],
536 "title" => $line_articles["title"],
537 "link" => $line_articles["link"],
538 "labels" => $article_id,
539 "unread" => $line_articles["unread"],
540 "marked" => $line_articles["marked"],
541 "published" => $line_articles["published"],
542 "comments" => $line_articles["comments"],
543 "author" => $line_articles["author"],
544 "updated" => strtotime($line_articles["updated"]),
545 "content" => $line_articles["content"],
546 "feed_id" => $line_articles["feed_id"],
547 "attachments" => $attachments));
548 }
549
550 array_push($feeds, array(
551 "feed_url" => $line_feeds["feed_url"],
552 "title" => $line_feeds["title"],
553 "id" => (int)$line_feeds["id"],
554 "unread" => (int)$unread_feed,
555 "cat_id" => (int)$line_feeds["cat_id"],
556 "last_updated" => strtotime($line_feeds["last_updated"]),
557 "articles" => $articles
558 ));
559
560 }
561 }
562
563 array_push($cats,
564 array(
565 "id" => $line["id"],
566 "title" => $line["title"],
567 "unread" => $unread,
568 "feeds" => $feeds));
569 }
570 }
571 print json_encode($cats);
572 break;
573
574 default:
575 print json_encode(array("error" => 'UNKNOWN_METHOD'));
576 break;
577
578 }
579
580 db_close($link);
581
582 ?>