]> git.wh0rd.org - tt-rss.git/blob - api/index.php
api: updateArticle: mention need to update ccache
[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 // FIXME: find out which feeds reference this article id and do ccache_update() on them
275 }
276
277 break;
278
279 case "getArticle":
280
281 $article_id = db_escape_string($_REQUEST["article_id"]);
282
283 $query = "SELECT id,title,link,content,feed_id,comments,int_id,
284 marked,unread,published,
285 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
286 author
287 FROM ttrss_entries,ttrss_user_entries
288 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
289 $_SESSION["uid"] ;
290
291 $result = db_query($link, $query);
292
293 if (db_num_rows($result) != 0) {
294
295 while ($line = db_fetch_assoc($result)) {
296
297 $attachments = get_article_enclosures($link, $line['id']);
298
299 $article = array(
300 "id" => $line["id"],
301 "title" => $line["title"],
302 "link" => $line["link"],
303 "labels" => get_article_labels($link, $line['id']),
304 "unread" => sql_bool_to_bool($line["unread"]),
305 "marked" => sql_bool_to_bool($line["marked"]),
306 "published" => sql_bool_to_bool($line["published"]),
307 "comments" => $line["comments"],
308 "author" => $line["author"],
309 "updated" => strtotime($line["updated"]),
310 "content" => $line["content"],
311 "feed_id" => $line["feed_id"],
312 "attachments" => $attachments
313 );
314
315 print json_encode($article);
316 }
317 }
318
319 break;
320
321 case "getConfig":
322 $config = array(
323 "icons_dir" => ICONS_DIR,
324 "icons_url" => ICONS_URL);
325
326 if (ENABLE_UPDATE_DAEMON) {
327 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
328 }
329
330 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
331 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
332
333 $num_feeds = db_fetch_result($result, 0, "cf");
334
335 $config["num_feeds"] = (int)$num_feeds;
336
337 print json_encode($config);
338
339 break;
340
341 case "updateFeed":
342 $feed_id = db_escape_string($_REQUEST["feed_id"]);
343
344 update_rss_feed($link, $feed_id, true);
345
346 print json_encode(array("status" => "OK"));
347
348 break;
349
350 case "catchupFeed":
351 $feed_id = db_escape_string($_REQUEST["feed_id"]);
352 $is_cat = db_escape_string($_REQUEST["category"]);
353
354 catchup_feed($link, $feed_id, $is_cat);
355
356 print json_encode(array("status" => "OK"));
357
358 break;
359
360 case "getPref":
361 $pref_name = db_escape_string($_REQUEST["pref_name"]);
362 print json_encode(array("value" => get_pref($link, $pref_name)));
363 break;
364
365 /* Method added for ttrss-reader for Android */
366 case "getArticles":
367 $isCategory = (int)db_escape_string($_REQUEST["is_category"]);
368 $id = (int)db_escape_string($_REQUEST["id"]);
369 $displayUnread = (int)db_escape_string($_REQUEST["unread"]);
370 $limit = (int)db_escape_string($_REQUEST["limit"]);
371 $feeds = array();
372
373 if ($isCategory > 0) {
374 // Get Feeds of the category
375
376 if ($id == 0) {
377 $category_part = "cat_id is NULL";
378 } else {
379 $category_part = "cat_id = '$id'";
380 }
381
382 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE ".
383 $category_part." AND owner_uid = '".$_SESSION["uid"]."'");
384
385 while ($line = db_fetch_assoc($result)) {
386 array_push($feeds, $line["id"]);
387 }
388
389 // Virtual feeds
390 $match_part = "";
391 if ($id == -1) {
392 $match_part = "marked = true";
393 array_push($feeds, -1);
394 } else if ($id == -2) {
395 $match_part = "published = true";
396 array_push($feeds, -2);
397 } else if ($id == -3) {
398 $match_part = "unread = true";
399 array_push($feeds, -3);
400
401 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
402
403 if (DB_TYPE == "pgsql") {
404 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
405 } else {
406 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
407 }
408 } else if ($id == -4) {
409 $match_part = "true";
410 array_push($feeds, -4);
411 }
412 } else {
413 // Only add one feed
414 array_push($feeds, $id);
415 }
416
417 $ret = array();
418
419 if (DB_TYPE == "mysql") {
420 $limit_part = " LIMIT 0,".$limit;
421 } else if (DB_TYPE == "pgsql") {
422 $limit_part = " LIMIT ".$limit;
423 } else {
424 $limit_part = "";
425 }
426
427 // Fetch articles for the feeds
428 foreach ($feeds as $feed) {
429
430 if ($match_part) {
431 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
432 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
433
434 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
435 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
436 FROM $from_qpart WHERE
437 ttrss_user_entries.ref_id = ttrss_entries.id AND
438 $feeds_qpart ($match_part) AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]." ORDER BY updated DESC".$limit_part;
439
440 $result = db_query($link, $query);
441 } else {
442 $query = "SELECT ttrss_entries.id,ttrss_entries.title,link,content,feed_id,comments,int_id,
443 marked,unread,published,".SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
444 FROM ttrss_entries,ttrss_user_entries
445 WHERE feed_id = '".$feed."' AND ref_id = id AND owner_uid = ".
446 $_SESSION["uid"]." AND unread >= '".$displayUnread."' ORDER BY updated DESC".$limit_part;
447
448 $result = db_query($link, $query);
449 }
450
451 $articles = array();
452 $i=0;
453 while ($i < mysql_numrows($result)) {
454
455 $article_id = db_fetch_result($result, $i, "id");
456
457 $attachments = get_article_enclosures($link, $article_id);
458
459 $article = array(
460 "id" => db_fetch_result($result, $i, "ttrss_entries.id"),
461 "title" => db_fetch_result($result, $i, "ttrss_entries.title"),
462 "link" => db_fetch_result($result, $i, "link"),
463 "labels" => get_article_labels($link, $article_id),
464 "unread" => sql_bool_to_bool(db_fetch_result($result, $i, "unread")),
465 "marked" => sql_bool_to_bool(db_fetch_result($result, $i, "marked")),
466 "published" => sql_bool_to_bool(db_fetch_result($result, $i, "published")),
467 "comments" => db_fetch_result($result, $i, "comments"),
468 "author" => db_fetch_result($result, $i, "author"),
469 "updated" => strtotime(db_fetch_result($result, $i, "updated")),
470 "content" => db_fetch_result($result, $i, "content"),
471 "feed_id" => db_fetch_result($result, $i, "feed_id"),
472 "attachments" => $attachments
473 );
474
475 array_push($ret, $article);
476
477 $i++;
478 }
479 }
480
481 print json_encode($ret);
482 break;
483
484 /* Method added for ttrss-reader for Android */
485 case "getNewArticles":
486 $time = (int) db_escape_string($_REQUEST["time"]);
487 // unread=1 zeigt alle an, unread=0 nur ungelesene
488 $displayUnread = (int) db_escape_string($_REQUEST["unread"]);
489
490 if (DB_TYPE == "mysql") {
491 $db_time_function = " AND last_updated > FROM_UNIXTIME(".$time.")";
492 } else if (DB_TYPE == "pgsql") {
493 $db_time_function = " AND last_updated > to_timestamp(".$time.")";
494 } else {
495 $db_time_function = "";
496 }
497
498 if (DB_TYPE == "mysql") {
499 $db_time_function2 = " AND updated > FROM_UNIXTIME(".$time.")";
500 } else if (DB_TYPE == "pgsql") {
501 $db_time_function2 = " AND updated > to_timestamp(".$time.")";
502 } else {
503 $db_time_function2 = "";
504 }
505
506 $cats = array();
507
508
509 // Add uncategorized feeds
510 $unread = getFeedUnread($link, 0, true);
511 if ($unread || $displayUnread > 0) {
512 $feeds = array();
513 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
514 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
515 "FROM ttrss_feeds WHERE cat_id IS null AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
516
517 while ($line_feeds = db_fetch_assoc($result_0)) {
518 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
519 if ($unread || $displayUnread > 0) {
520
521 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
522 marked,unread,published,".
523 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
524 FROM ttrss_entries,ttrss_user_entries
525 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
526 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
527
528 $articles = array();
529 while ($line_articles = db_fetch_assoc($result_1)) {
530 $article_id = db_fetch_result($result, $i, "id");
531 $attachments = get_article_enclosures($link, $article_id);
532 array_push($articles, $article = array(
533 "id" => $line_articles["id"],
534 "title" => $line_articles["title"],
535 "link" => $line_articles["link"],
536 "labels" => $article_id,
537 "unread" => $line_articles["unread"],
538 "marked" => $line_articles["marked"],
539 "published" => $line_articles["published"],
540 "comments" => $line_articles["comments"],
541 "author" => $line_articles["author"],
542 "updated" => strtotime($line_articles["updated"]),
543 "content" => $line_articles["content"],
544 "feed_id" => $line_articles["feed_id"],
545 "attachments" => $attachments));
546 }
547
548 array_push($feeds, array(
549 "feed_url" => $line_feeds["feed_url"],
550 "title" => $line_feeds["title"],
551 "id" => (int)$line_feeds["id"],
552 "unread" => (int)$unread_feed,
553 "has_icon" => $has_icon,
554 "cat_id" => (int)$line_feeds["cat_id"],
555 "last_updated" => strtotime($line_feeds["last_updated"]),
556 "articles" => $articles
557 ));
558 }
559 }
560
561 array_push($cats,
562 array(
563 "id" => 0,
564 "title" => "Uncategorized Feeds",
565 "unread" => $unread,
566 "feeds" => $feeds));
567 }
568
569
570 $result = db_query($link, "SELECT id, title FROM ttrss_feed_categories WHERE owner_uid = " . $_SESSION["uid"]);
571 while ($line = db_fetch_assoc($result)) {
572 $unread = getFeedUnread($link, $line["id"], true);
573
574 if ($unread || $displayUnread > 0) {
575 $feeds = array();
576 $result_0 = db_query($link, "SELECT id, feed_url, cat_id, title, ".
577 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated ".
578 "FROM ttrss_feeds WHERE cat_id = '".
579 $line["id"]."' AND owner_uid = '".$_SESSION["uid"]."'" . $db_time_function);
580
581 while ($line_feeds = db_fetch_assoc($result_0)) {
582 $unread_feed = getFeedUnread($link, $line_feeds["id"], false);
583 if ($unread_feed || $displayUnread > 0) {
584
585 $result_1 = db_query($link, "SELECT id,title,link,content,feed_id,comments,int_id,
586 marked,unread,published,".
587 SUBSTRING_FOR_DATE."(updated,1,16) as updated,author
588 FROM ttrss_entries,ttrss_user_entries
589 WHERE feed_id = '".$line_feeds["id"]."' AND ref_id = id AND owner_uid = " .
590 $_SESSION["uid"]." AND unread >= '".$displayUnread."'" . $db_time_function2);
591
592 $articles = array();
593 while ($line_articles = db_fetch_assoc($result_1)) {
594 $article_id = db_fetch_result($result, $i, "id");
595 $attachments = get_article_enclosures($link, $article_id);
596 array_push($articles, $article = array(
597 "id" => $line_articles["id"],
598 "title" => $line_articles["title"],
599 "link" => $line_articles["link"],
600 "labels" => $article_id,
601 "unread" => $line_articles["unread"],
602 "marked" => $line_articles["marked"],
603 "published" => $line_articles["published"],
604 "comments" => $line_articles["comments"],
605 "author" => $line_articles["author"],
606 "updated" => strtotime($line_articles["updated"]),
607 "content" => $line_articles["content"],
608 "feed_id" => $line_articles["feed_id"],
609 "attachments" => $attachments));
610 }
611
612 array_push($feeds, array(
613 "feed_url" => $line_feeds["feed_url"],
614 "title" => $line_feeds["title"],
615 "id" => (int)$line_feeds["id"],
616 "unread" => (int)$unread_feed,
617 "cat_id" => (int)$line_feeds["cat_id"],
618 "last_updated" => strtotime($line_feeds["last_updated"]),
619 "articles" => $articles
620 ));
621
622 }
623 }
624
625 array_push($cats,
626 array(
627 "id" => $line["id"],
628 "title" => $line["title"],
629 "unread" => $unread,
630 "feeds" => $feeds));
631 }
632 }
633 print json_encode($cats);
634 break;
635
636 default:
637 print json_encode(array("error" => 'UNKNOWN_METHOD'));
638 break;
639
640 }
641
642 db_close($link);
643
644 ?>