]> git.wh0rd.org - tt-rss.git/blob - api/index.php
4fbf296e723f022d9656fcfbea2aab22cbb02ebc
[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 $counters = array();
108
109 $result = db_query($link, "SELECT
110 id FROM ttrss_feed_categories
111 WHERE owner_uid = " .
112 $_SESSION["uid"]);
113
114 $cats = array();
115
116 while ($line = db_fetch_assoc($result)) {
117 array_push($cats, $line["id"]);
118 }
119 array_push($cats, "0");
120 array_push($cats, "-1");
121 array_push($cats, "-2");
122 array_push($cats, "-3");
123 array_push($cats, "-4");
124
125 foreach ($cats as $cat) {
126
127 $cat_part = "cat_id = '$cat'";
128 if ($cat == 0) {
129 $cat_part = "cat_id IS null";
130 }
131
132 $result = db_query($link, "SELECT
133 id FROM ttrss_feeds WHERE ".
134 $cat_part." AND owner_uid = " . $_SESSION["uid"]);
135
136 $feeds = array();
137
138 while ($line = db_fetch_assoc($result)) {
139
140 $unread = getFeedArticles($link, $line["id"], false, true, $_SESSION["uid"]);
141
142 if ($unread) {
143 $row = array(
144 "feed_id" => (int)$line["id"],
145 "unread" => (int)$unread
146 );
147 array_push($feeds, $row);
148 }
149 }
150
151 $is_cat = true;
152 if ($cat < 0) {
153 $is_cat = false;
154 }
155
156 $unread_cat = getFeedArticles($link, $cat, $is_cat, true, $_SESSION["uid"]);
157
158 if ($feeds) {
159 $count = array(
160 "cat_id" => $cat,
161 "unread" => $unread_cat,
162 "feeds" => $feeds
163 );
164 } else {
165 $count = array(
166 "cat_id" => $cat,
167 "unread" => $unread_cat
168 );
169 }
170
171 array_push($counters, $count);
172 }
173 print json_encode($counters);
174 break;
175
176 case "getFeeds":
177 $cat_id = db_escape_string($_REQUEST["cat_id"]);
178 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
179 $limit = (int) db_escape_string($_REQUEST["limit"]);
180 $offset = (int) db_escape_string($_REQUEST["offset"]);
181
182 $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset);
183
184 print json_encode($feeds);
185
186 break;
187
188 case "getCategories":
189 $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
190
191 $result = db_query($link, "SELECT
192 id, title FROM ttrss_feed_categories
193 WHERE owner_uid = " .
194 $_SESSION["uid"]);
195
196 $cats = array();
197
198 while ($line = db_fetch_assoc($result)) {
199 $unread = getFeedUnread($link, $line["id"], true);
200
201 if ($unread || !$unread_only) {
202 array_push($cats, array("id" => $line["id"],
203 "title" => $line["title"],
204 "unread" => $unread));
205 }
206 }
207
208 print json_encode($cats);
209 break;
210
211 case "getHeadlines":
212 $feed_id = db_escape_string($_REQUEST["feed_id"]);
213 $limit = (int)db_escape_string($_REQUEST["limit"]);
214 $offset = (int)db_escape_string($_REQUEST["skip"]);
215 $filter = db_escape_string($_REQUEST["filter"]);
216 $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
217 $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
218 $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
219 /* all_articles, unread, adaptive, marked, updated */
220 $view_mode = db_escape_string($_REQUEST["view_mode"]);
221
222 $headlines = api_get_headlines($link, $feed_id, $limit, $offset,
223 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false);
224
225 print json_encode($headlines);
226
227 break;
228
229 case "updateArticle":
230 $article_ids = split(",", db_escape_string($_REQUEST["article_ids"]));
231 $mode = (int) db_escape_string($_REQUEST["mode"]);
232 $field_raw = (int)db_escape_string($_REQUEST["field"]);
233
234 $field = "";
235 $set_to = "";
236
237 switch ($field_raw) {
238 case 0:
239 $field = "marked";
240 break;
241 case 1:
242 $field = "published";
243 break;
244 case 2:
245 $field = "unread";
246 break;
247 };
248
249 switch ($mode) {
250 case 1:
251 $set_to = "true";
252 break;
253 case 0:
254 $set_to = "false";
255 break;
256 case 2:
257 $set_to = "NOT $field";
258 break;
259 }
260
261 if ($field && $set_to && count($article_ids) > 0) {
262
263 $article_ids = join(", ", $article_ids);
264
265 if ($field == "unread") {
266 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
267 last_read = NOW()
268 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
269 } else {
270 $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
271 WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
272 }
273 }
274
275 break;
276
277 case "getArticle":
278
279 $article_id = db_escape_string($_REQUEST["article_id"]);
280
281 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
282 marked,unread,published,
283 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
284 author
285 FROM ttrss_entries,ttrss_user_entries
286 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
287 $_SESSION["uid"] ;
288
289 $result = db_query($link, $query);
290
291 if (db_num_rows($result) != 0) {
292
293 while ($line = db_fetch_assoc($result)) {
294
295 $attachments = get_article_enclosures($link, $line['id']);
296
297 $article = array(
298 "id" => $line["id"],
299 "title" => $line["title"],
300 "link" => $line["link"],
301 "labels" => get_article_labels($link, $line['id']),
302 "unread" => sql_bool_to_bool($line["unread"]),
303 "marked" => sql_bool_to_bool($line["marked"]),
304 "published" => sql_bool_to_bool($line["published"]),
305 "comments" => $line["comments"],
306 "author" => $line["author"],
307 "updated" => strtotime($line["updated"]),
308 "content" => $line["content"],
309 "feed_id" => $line["feed_id"],
310 "attachments" => $attachments
311 );
312
313 print json_encode($article);
314 }
315 }
316
317 break;
318
319 case "getConfig":
320 $config = array(
321 "icons_dir" => ICONS_DIR,
322 "icons_url" => ICONS_URL);
323
324 if (ENABLE_UPDATE_DAEMON) {
325 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
326 }
327
328 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
329 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
330
331 $num_feeds = db_fetch_result($result, 0, "cf");
332
333 $config["num_feeds"] = (int)$num_feeds;
334
335 print json_encode($config);
336
337 break;
338
339 case "updateFeed":
340 $feed_id = db_escape_string($_REQUEST["feed_id"]);
341
342 update_rss_feed($link, $feed_id, true);
343
344 print json_encode(array("status" => "OK"));
345
346 break;
347
348 case "catchupFeed":
349 $feed_id = db_escape_string($_REQUEST["feed_id"]);
350 $is_cat = db_escape_string($_REQUEST["category"]);
351
352 catchup_feed($link, $feed_id, $is_cat);
353
354 print json_encode(array("status" => "OK"));
355
356 break;
357
358 case "getPref":
359 $pref_name = db_escape_string($_REQUEST["pref_name"]);
360 print json_encode(array("value" => get_pref($link, $pref_name)));
361 break;
362
363 /* Method added for ttrss-reader for Android */
364 case "getArticles":
365 $isCategory = (int)db_escape_string($_REQUEST["is_category"]);
366 $id = (int)db_escape_string($_REQUEST["id"]);
367 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
368 $limit = (int)db_escape_string($_REQUEST["limit"]);
369 $feeds = array();
370
371 if ($isCategory > 0) {
372 // Get Feeds of the category
373
374 if ($id == 0) {
375 $category_part = "cat_id is NULL";
376 } else {
377 $category_part = "cat_id = '$id'";
378 }
379
380 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
381 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
382
383 while ($line = db_fetch_assoc($result)) {
384 array_push($feeds, $line["id"]);
385 }
386
387 // Virtual feeds
388 $match_part = "";
389 if ($id == -1) {
390 $match_part = "marked = true";
391 array_push($feeds, -1);
392 } else if ($id == -2) {
393 $match_part = "published = true";
394 array_push($feeds, -2);
395 } else if ($id == -3) {
396 $match_part = "unread = true";
397 array_push($feeds, -3);
398
399 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
400
401 if (DB_TYPE == "pgsql") {
402 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
403 } else {
404 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
405 }
406 } else if ($id == -4) {
407 $match_part = "true";
408 array_push($feeds, -4);
409 }
410 } else {
411 // Only add one feed
412 array_push($feeds, $id);
413 }
414
415 $ret = array();
416
417 if (DB_TYPE == "mysql") {
418 $limit_part = " LIMIT 0,".$limit;
419 } else if (DB_TYPE == "pgsql") {
420 $limit_part = " LIMIT ".$limit;
421 } else {
422 $limit_part = "";
423 }
424
425 // Fetch articles for the feeds
426 foreach ($feeds as $feed) {
427
428 if ($match_part) {
429 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
430 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
431
432 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
433 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
434 FROM $from_qpart WHERE
435 ttrss_user_entries.ref_id = ttrss_entries.id AND
436 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
437
438 $result = db_query($link, $query);
439 } else {
440 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
441 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
442 FROM ttrss_entries,ttrss_user_entries
443 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
444 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
445
446 $result = db_query($link, $query);
447 }
448
449 $articles = array();
450 $i=0;
451 while ($i < mysql_numrows($result)) {
452
453 $article_id = db_fetch_result($result, $i, "id");
454
455 $attachments = get_article_enclosures($link, $article_id);
456
457 $article = array(
458 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
459 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
460 "link" => db_fetch_result($result, $i, "link"),
461 "labels" => get_article_labels($link, $article_id),
462 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
463 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
464 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
465 "comments" => db_fetch_result($result, $i, "comments"),
466 "author" => db_fetch_result($result, $i, "author"),
467 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
468 "content" => db_fetch_result($result, $i, "content"),
469 "feed_id" => db_fetch_result($result, $i, "feed_id"),
470 "attachments" => $attachments
471 );
472
473 array_push($ret, $article);
474
475 $i++;
476 }
477 }
478
479 print json_encode($ret);
480 break;
481
482 /* Method added for ttrss-reader for Android */
483 case "getNewArticles":
484 $time = (int) db_escape_string($_REQUEST["time"]);
485 // unread=1 zeigt alle an, unread=0 nur ungelesene
486 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
487
488 if (DB_TYPE == "mysql") {
489 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
490 } else if (DB_TYPE == "pgsql") {
491 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
492 } else {
493 $db_time_function = "";
494 }
495
496 if (DB_TYPE == "mysql") {
497 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
498 } else if (DB_TYPE == "pgsql") {
499 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
500 } else {
501 $db_time_function2 = "";
502 }
503
504 $cats = array();
505
506
507 // Add uncategorized feeds
508 $unread = getFeedUnread($link, 0, true);
509 if ($unread || $displayUnread > 0) {
510 $feeds = array();
511 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
512 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
513 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
514
515 while ($line_feeds = db_fetch_assoc($result_0)) {
516 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
517 if ($unread || $displayUnread > 0) {
518
519 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
520 marked,unread,published,".
521 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
522 FROM ttrss_entries,ttrss_user_entries
523 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
524 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
525
526 $articles = array();
527 while ($line_articles = db_fetch_assoc($result_1)) {
528 $article_id = db_fetch_result($result, $i, "id");
529 $attachments = get_article_enclosures($link, $article_id);
530 array_push($articles, $article = array(
531 "id" => $line_articles["id"],
532 "title" => $line_articles["title"],
533 "link" => $line_articles["link"],
534 "labels" => $article_id,
535 "unread" => $line_articles["unread"],
536 "marked" => $line_articles["marked"],
537 "published" => $line_articles["published"],
538 "comments" => $line_articles["comments"],
539 "author" => $line_articles["author"],
540 "updated" => strtotime($line_articles["updated"]),
541 "content" => $line_articles["content"],
542 "feed_id" => $line_articles["feed_id"],
543 "attachments" => $attachments));
544 }
545
546 array_push($feeds, array(
547 "feed_url" => $line_feeds["feed_url"],
548 "title" => $line_feeds["title"],
549 "id" => (int)$line_feeds["id"],
550 "unread" => (int)$unread_feed,
551 "has_icon" => $has_icon,
552 "cat_id" => (int)$line_feeds["cat_id"],
553 "last_updated" => strtotime($line_feeds["last_updated"]),
554 "articles" => $articles
555 ));
556 }
557 }
558
559 array_push($cats,
560 array(
561 "id" => 0,
562 "title" => "Uncategorized Feeds",
563 "unread" => $unread,
564 "feeds" => $feeds));
565 }
566
567
568 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
569 while ($line = db_fetch_assoc($result)) {
570 $unread = getFeedUnread($link, $line["id"], true);
571
572 if ($unread || $displayUnread > 0) {
573 $feeds = array();
574 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
575 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
576 "FROM ttrss_feeds WHERE cat_id = '".
577 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
578
579 while ($line_feeds = db_fetch_assoc($result_0)) {
580 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
581 if ($unread_feed || $displayUnread > 0) {
582
583 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
584 marked,unread,published,".
585 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
586 FROM ttrss_entries,ttrss_user_entries
587 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
588 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
589
590 $articles = array();
591 while ($line_articles = db_fetch_assoc($result_1)) {
592 $article_id = db_fetch_result($result, $i, "id");
593 $attachments = get_article_enclosures($link, $article_id);
594 array_push($articles, $article = array(
595 "id" => $line_articles["id"],
596 "title" => $line_articles["title"],
597 "link" => $line_articles["link"],
598 "labels" => $article_id,
599 "unread" => $line_articles["unread"],
600 "marked" => $line_articles["marked"],
601 "published" => $line_articles["published"],
602 "comments" => $line_articles["comments"],
603 "author" => $line_articles["author"],
604 "updated" => strtotime($line_articles["updated"]),
605 "content" => $line_articles["content"],
606 "feed_id" => $line_articles["feed_id"],
607 "attachments" => $attachments));
608 }
609
610 array_push($feeds, array(
611 "feed_url" => $line_feeds["feed_url"],
612 "title" => $line_feeds["title"],
613 "id" => (int)$line_feeds["id"],
614 "unread" => (int)$unread_feed,
615 "cat_id" => (int)$line_feeds["cat_id"],
616 "last_updated" => strtotime($line_feeds["last_updated"]),
617 "articles" => $articles
618 ));
619
620 }
621 }
622
623 array_push($cats,
624 array(
625 "id" => $line["id"],
626 "title" => $line["title"],
627 "unread" => $unread,
628 "feeds" => $feeds));
629 }
630 }
631 print json_encode($cats);
632 break;
633
634 default:
635 print json_encode(array("error" => 'UNKNOWN_METHOD'));
636 break;
637
638 }
639
640 db_close($link);
641
642 ?>