]> git.wh0rd.org - tt-rss.git/blame - classes/api.php
Merge pull request #147 from pedros/nnttrss_api_extensions
[tt-rss.git] / classes / api.php
CommitLineData
de8260cb
AD
1<?php
2
3class API extends Handler {
4
efc6553d 5 const API_LEVEL = 5;
de8260cb
AD
6
7 const STATUS_OK = 0;
8 const STATUS_ERR = 1;
9
10 private $seq;
11
12 function before($method) {
13 if (parent::before($method)) {
12f31782 14 header("Content-Type: text/json");
de8260cb
AD
15
16 if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
17 print $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
18 return false;
19 }
20
21 if ($_SESSION["uid"] && $method != "logout" && !get_pref($this->link, 'ENABLE_API_ACCESS')) {
22 print $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
23 return false;
24 }
25
26 $this->seq = (int) $_REQUEST['seq'];
27
de8260cb
AD
28 return true;
29 }
30 return false;
31 }
32
33 function wrap($status, $reply) {
34 print json_encode(array("seq" => $this->seq,
35 "status" => $status,
36 "content" => $reply));
37 }
38
39 function getVersion() {
40 $rv = array("version" => VERSION);
41 print $this->wrap(self::STATUS_OK, $rv);
42 }
43
44 function getApiLevel() {
a3b5394a 45 $rv = array("level" => self::API_LEVEL);
de8260cb
AD
46 print $this->wrap(self::STATUS_OK, $rv);
47 }
48
49 function login() {
79bb5589 50 @session_destroy();
5160620c
AD
51 @session_start();
52
3972bf59 53 $login = db_escape_string($this->link, $_REQUEST["user"]);
de8260cb
AD
54 $password = $_REQUEST["password"];
55 $password_base64 = base64_decode($_REQUEST["password"]);
56
57 if (SINGLE_USER_MODE) $login = "admin";
58
59 $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$login'");
60
61 if (db_num_rows($result) != 0) {
62 $uid = db_fetch_result($result, 0, "id");
63 } else {
64 $uid = 0;
65 }
66
67 if (!$uid) {
68 print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
69 return;
70 }
71
72 if (get_pref($this->link, "ENABLE_API_ACCESS", $uid)) {
73 if (authenticate_user($this->link, $login, $password)) { // try login with normal password
5ba4ebc6
AD
74 print $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
75 "api_level" => self::API_LEVEL));
de8260cb 76 } else if (authenticate_user($this->link, $login, $password_base64)) { // else try with base64_decoded password
5ba4ebc6
AD
77 print $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
78 "api_level" => self::API_LEVEL));
de8260cb
AD
79 } else { // else we are not logged in
80 print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
81 }
82 } else {
83 print $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
84 }
85
86 }
87
88 function logout() {
89 logout_user();
90 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
91 }
92
93 function isLoggedIn() {
94 print $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
95 }
96
97 function getUnread() {
3972bf59
AD
98 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
99 $is_cat = db_escape_string($this->link, $_REQUEST["is_cat"]);
de8260cb
AD
100
101 if ($feed_id) {
102 print $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($this->link, $feed_id, $is_cat)));
103 } else {
104 print $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread($this->link)));
105 }
106 }
107
108 /* Method added for ttrss-reader for Android */
109 function getCounters() {
5b55e9e2 110 print $this->wrap(self::STATUS_OK, getAllCounters($this->link));
de8260cb
AD
111 }
112
0192ffe5
PS
113 function getFeedStats() {
114 $feeds = $this->api_get_feed_stats($this->link);
115 print $this->wrap(self::STATUS_OK, $feeds);
116 }
117
de8260cb 118 function getFeeds() {
3972bf59 119 $cat_id = db_escape_string($this->link, $_REQUEST["cat_id"]);
9955a134 120 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
3972bf59
AD
121 $limit = (int) db_escape_string($this->link, $_REQUEST["limit"]);
122 $offset = (int) db_escape_string($this->link, $_REQUEST["offset"]);
9955a134 123 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
de8260cb 124
04f60eb7 125 $feeds = $this->api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset, $include_nested);
de8260cb
AD
126
127 print $this->wrap(self::STATUS_OK, $feeds);
128 }
129
130 function getCategories() {
9955a134
AD
131 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
132 $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]);
b3575bd8 133 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
de8260cb
AD
134
135 // TODO do not return empty categories, return Uncategorized and standard virtual cats
136
48646336
AD
137 if ($enable_nested)
138 $nested_qpart = "parent_cat IS NULL";
139 else
140 $nested_qpart = "true";
141
de8260cb 142 $result = db_query($this->link, "SELECT
d49dfa38
AD
143 id, title, order_id, (SELECT COUNT(id) FROM
144 ttrss_feeds WHERE
7be3fcd5
AD
145 ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds,
146 (SELECT COUNT(id) FROM
147 ttrss_feed_categories AS c2 WHERE
148 c2.parent_cat = ttrss_feed_categories.id) AS num_cats
d49dfa38 149 FROM ttrss_feed_categories
48646336 150 WHERE $nested_qpart AND owner_uid = " .
de8260cb
AD
151 $_SESSION["uid"]);
152
153 $cats = array();
154
155 while ($line = db_fetch_assoc($result)) {
66ca7c30 156 if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) {
d49dfa38
AD
157 $unread = getFeedUnread($this->link, $line["id"], true);
158
159 if ($enable_nested)
160 $unread += getCategoryChildrenUnread($this->link, $line["id"]);
161
162 if ($unread || !$unread_only) {
163 array_push($cats, array("id" => $line["id"],
164 "title" => $line["title"],
165 "unread" => $unread,
166 "order_id" => (int) $line["order_id"],
167 ));
168 }
de8260cb
AD
169 }
170 }
171
172 foreach (array(-2,-1,0) as $cat_id) {
c0a08063
AD
173 if ($include_empty || !$this->isCategoryEmpty($cat_id)) {
174 $unread = getFeedUnread($this->link, $cat_id, true);
de8260cb 175
c0a08063
AD
176 if ($unread || !$unread_only) {
177 array_push($cats, array("id" => $cat_id,
178 "title" => getCategoryTitle($this->link, $cat_id),
179 "unread" => $unread));
180 }
de8260cb
AD
181 }
182 }
183
184 print $this->wrap(self::STATUS_OK, $cats);
185 }
186
187 function getHeadlines() {
3972bf59 188 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
8aa3becc 189 if ($feed_id != "") {
de8260cb 190
3972bf59 191 $limit = (int)db_escape_string($this->link, $_REQUEST["limit"]);
1a740cf6
AD
192
193 if (!$limit || $limit >= 60) $limit = 60;
194
3972bf59
AD
195 $offset = (int)db_escape_string($this->link, $_REQUEST["skip"]);
196 $filter = db_escape_string($this->link, $_REQUEST["filter"]);
9955a134
AD
197 $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
198 $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]);
199 $show_content = sql_bool_to_bool($_REQUEST["show_content"]);
de8260cb 200 /* all_articles, unread, adaptive, marked, updated */
3972bf59 201 $view_mode = db_escape_string($this->link, $_REQUEST["view_mode"]);
9955a134 202 $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]);
3972bf59 203 $since_id = (int)db_escape_string($this->link, $_REQUEST["since_id"]);
9955a134 204 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
db9e00e3 205 $sanitize_content = true;
ffd07864 206
0bbd1414 207 $override_order = false;
208 switch ($_REQUEST["order_by"]) {
209 case "date_reverse":
210 $override_order = "date_entered, updated";
211 break;
212 case "feed_dates":
213 $override_order = "updated DESC";
214 break;
215 }
ffd07864 216
3e4af5b0
AD
217 /* do not rely on params below */
218
3972bf59
AD
219 $search = db_escape_string($this->link, $_REQUEST["search"]);
220 $search_mode = db_escape_string($this->link, $_REQUEST["search_mode"]);
3e4af5b0 221
04f60eb7 222 $headlines = $this->api_get_headlines($this->link, $feed_id, $limit, $offset,
0bbd1414 223 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
ca5d9be4 224 $include_attachments, $since_id, $search, $search_mode,
db9e00e3 225 $include_nested, $sanitize_content);
de8260cb
AD
226
227 print $this->wrap(self::STATUS_OK, $headlines);
228 } else {
229 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
230 }
231 }
232
233 function updateArticle() {
3972bf59
AD
234 $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
235 $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]);
236 $data = db_escape_string($this->link, $_REQUEST["data"]);
237 $field_raw = (int)db_escape_string($this->link, $_REQUEST["field"]);
de8260cb
AD
238
239 $field = "";
240 $set_to = "";
241
242 switch ($field_raw) {
243 case 0:
244 $field = "marked";
7873d588 245 $additional_fields = ",last_marked = NOW()";
de8260cb
AD
246 break;
247 case 1:
248 $field = "published";
7873d588 249 $additional_fields = ",last_published = NOW()";
de8260cb
AD
250 break;
251 case 2:
252 $field = "unread";
7873d588 253 $additional_fields = ",last_read = NOW()";
de8260cb
AD
254 break;
255 case 3:
256 $field = "note";
257 };
258
259 switch ($mode) {
260 case 1:
261 $set_to = "true";
262 break;
263 case 0:
264 $set_to = "false";
265 break;
266 case 2:
267 $set_to = "NOT $field";
268 break;
269 }
270
271 if ($field == "note") $set_to = "'$data'";
272
273 if ($field && $set_to && count($article_ids) > 0) {
274
275 $article_ids = join(", ", $article_ids);
276
7873d588 277 $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
de8260cb
AD
278
279 $num_updated = db_affected_rows($this->link, $result);
280
281 if ($num_updated > 0 && $field == "unread") {
282 $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
283 WHERE ref_id IN ($article_ids)");
284
285 while ($line = db_fetch_assoc($result)) {
286 ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]);
287 }
288 }
289
7873d588
AD
290 if ($num_updated > 0 && $field == "published") {
291 if (PUBSUBHUBBUB_HUB) {
292 $rss_link = get_self_url_prefix() .
293 "/public.php?op=rss&id=-2&key=" .
294 get_feed_access_key($this->link, -2, false);
295
296 $p = new Publisher(PUBSUBHUBBUB_HUB);
297 $pubsub_result = $p->publish_update($rss_link);
298 }
299 }
300
de8260cb
AD
301 print $this->wrap(self::STATUS_OK, array("status" => "OK",
302 "updated" => $num_updated));
303
304 } else {
305 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
306 }
307
308 }
309
310 function getArticle() {
311
3972bf59 312 $article_id = join(",", array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_id"])), is_numeric));
de8260cb 313
87764a50 314 $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
de8260cb
AD
315 marked,unread,published,
316 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
317 author
318 FROM ttrss_entries,ttrss_user_entries
319 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
320 $_SESSION["uid"] ;
321
322 $result = db_query($this->link, $query);
323
324 $articles = array();
325
326 if (db_num_rows($result) != 0) {
327
328 while ($line = db_fetch_assoc($result)) {
329
330 $attachments = get_article_enclosures($this->link, $line['id']);
331
332 $article = array(
333 "id" => $line["id"],
334 "title" => $line["title"],
335 "link" => $line["link"],
336 "labels" => get_article_labels($this->link, $line['id']),
337 "unread" => sql_bool_to_bool($line["unread"]),
338 "marked" => sql_bool_to_bool($line["marked"]),
339 "published" => sql_bool_to_bool($line["published"]),
340 "comments" => $line["comments"],
341 "author" => $line["author"],
ef3da31c 342 "updated" => (int) strtotime($line["updated"]),
87764a50 343 "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"],
de8260cb
AD
344 "feed_id" => $line["feed_id"],
345 "attachments" => $attachments
346 );
347
0db61af8
AD
348 global $pluginhost;
349 foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_API) as $p) {
350 $article = $p->hook_render_article_api(array("article" => $article));
351 }
352
353
de8260cb
AD
354 array_push($articles, $article);
355
356 }
357 }
358
359 print $this->wrap(self::STATUS_OK, $articles);
360
361 }
362
363 function getConfig() {
364 $config = array(
365 "icons_dir" => ICONS_DIR,
366 "icons_url" => ICONS_URL);
367
368 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
369
370 $result = db_query($this->link, "SELECT COUNT(*) AS cf FROM
371 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
372
373 $num_feeds = db_fetch_result($result, 0, "cf");
374
375 $config["num_feeds"] = (int)$num_feeds;
376
377 print $this->wrap(self::STATUS_OK, $config);
378 }
379
380 function updateFeed() {
c1f6e5f8
AD
381 require_once "include/rssfuncs.php";
382
383 $feed_id = (int) db_escape_string($this->link, $_REQUEST["feed_id"]);
de8260cb
AD
384
385 update_rss_feed($this->link, $feed_id, true);
386
387 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
388 }
389
390 function catchupFeed() {
3972bf59
AD
391 $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
392 $is_cat = db_escape_string($this->link, $_REQUEST["is_cat"]);
de8260cb
AD
393
394 catchup_feed($this->link, $feed_id, $is_cat);
395
396 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
397 }
398
399 function getPref() {
3972bf59 400 $pref_name = db_escape_string($this->link, $_REQUEST["pref_name"]);
de8260cb
AD
401
402 print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name)));
403 }
404
ea1c2903 405 function getLabels() {
3972bf59 406 //$article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
ea1c2903
AD
407
408 $article_id = (int)$_REQUEST['article_id'];
409
410 $rv = array();
411
412 $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color
413 FROM ttrss_labels2
414 WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
415
416 if ($article_id)
417 $article_labels = get_article_labels($this->link, $article_id);
418 else
419 $article_labels = array();
420
421 while ($line = db_fetch_assoc($result)) {
422
423 $checked = false;
424 foreach ($article_labels as $al) {
425 if ($al[0] == $line['id']) {
426 $checked = true;
427 break;
428 }
429 }
430
431 array_push($rv, array(
432 "id" => (int)$line['id'],
433 "caption" => $line['caption'],
434 "fg_color" => $line['fg_color'],
435 "bg_color" => $line['bg_color'],
436 "checked" => $checked));
437 }
438
439 print $this->wrap(self::STATUS_OK, $rv);
440 }
441
396bfdf9
AD
442 function setArticleLabel() {
443
3972bf59
AD
444 $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
445 $label_id = (int) db_escape_string($this->link, $_REQUEST['label_id']);
446 $assign = (bool) db_escape_string($this->link, $_REQUEST['assign']) == "true";
396bfdf9 447
3972bf59 448 $label = db_escape_string($this->link, label_find_caption($this->link,
396bfdf9
AD
449 $label_id, $_SESSION["uid"]));
450
451 $num_updated = 0;
452
453 if ($label) {
454
455 foreach ($article_ids as $id) {
456
457 if ($assign)
458 label_add_article($this->link, $id, $label, $_SESSION["uid"]);
459 else
460 label_remove_article($this->link, $id, $label, $_SESSION["uid"]);
461
462 ++$num_updated;
463
464 }
465 }
466
467 print $this->wrap(self::STATUS_OK, array("status" => "OK",
468 "updated" => $num_updated));
469
470 }
471
de8260cb
AD
472 function index() {
473 print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
474 }
475
8361e724 476 function shareToPublished() {
3972bf59
AD
477 $title = db_escape_string($this->link, strip_tags($_REQUEST["title"]));
478 $url = db_escape_string($this->link, strip_tags($_REQUEST["url"]));
479 $content = db_escape_string($this->link, strip_tags($_REQUEST["content"]));
8361e724 480
50832719 481 if (Article::create_published_article($this->link, $title, $url, $content, "", $_SESSION["uid"])) {
8361e724
AD
482 print $this->wrap(self::STATUS_OK, array("status" => 'OK'));
483 } else {
484 print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
485 }
486 }
04f60eb7 487
0192ffe5
PS
488 static function api_get_feed_stats($link) {
489
490 $feeds = array();
491
492 $result = db_query($link, "SELECT ttrss_feeds.id, ttrss_feeds.title,".
493 " MIN(ttrss_entries.id) AS first, MAX(ttrss_entries.id) AS last,".
494 " COUNT(ttrss_entries.id) AS total".
495 " FROM ttrss_entries, ttrss_user_entries, ttrss_feeds".
496 " WHERE ttrss_user_entries.feed_id = ttrss_feeds.id".
497 " AND ttrss_user_entries.ref_id = ttrss_entries.id".
498 " AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"].
499 " GROUP BY ttrss_feeds.title");
500
501 while ($line = db_fetch_assoc($result)) {
502
503 $unread = getFeedUnread($link, $line["id"]);
504
505 $row = array(
506 "id" => (int)$line["id"],
507 "title" => $line["title"],
508 "first" => (int)$line["first"],
509 "last" => (int)$line["last"],
510 "total" => (int)$line["total"],
511 "unread" => (int)$unread
512 );
513
514 array_push($feeds, $row);
515 }
516
517 return $feeds;
518}
519
04f60eb7
AD
520 static function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset, $include_nested = false) {
521
522 $feeds = array();
523
524 /* Labels */
525
526 if ($cat_id == -4 || $cat_id == -2) {
527 $counters = getLabelCounters($link, true);
528
529 foreach (array_values($counters) as $cv) {
530
531 $unread = $cv["counter"];
532
533 if ($unread || !$unread_only) {
534
535 $row = array(
536 "id" => $cv["id"],
537 "title" => $cv["description"],
538 "unread" => $cv["counter"],
539 "cat_id" => -2,
540 );
541
542 array_push($feeds, $row);
543 }
544 }
545 }
546
547 /* Virtual feeds */
548
549 if ($cat_id == -4 || $cat_id == -1) {
550 foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
551 $unread = getFeedUnread($link, $i);
552
553 if ($unread || !$unread_only) {
554 $title = getFeedTitle($link, $i);
555
556 $row = array(
557 "id" => $i,
558 "title" => $title,
559 "unread" => $unread,
560 "cat_id" => -1,
561 );
562 array_push($feeds, $row);
563 }
564
565 }
566 }
567
568 /* Child cats */
569
570 if ($include_nested && $cat_id) {
571 $result = db_query($link, "SELECT
572 id, title FROM ttrss_feed_categories
573 WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
574 " ORDER BY id, title");
575
576 while ($line = db_fetch_assoc($result)) {
577 $unread = getFeedUnread($link, $line["id"], true) +
578 getCategoryChildrenUnread($link, $line["id"]);
579
580 if ($unread || !$unread_only) {
581 $row = array(
582 "id" => $line["id"],
583 "title" => $line["title"],
584 "unread" => $unread,
585 "is_cat" => true,
586 );
587 array_push($feeds, $row);
588 }
589 }
590 }
591
592 /* Real feeds */
593
594 if ($limit) {
595 $limit_qpart = "LIMIT $limit OFFSET $offset";
596 } else {
597 $limit_qpart = "";
598 }
599
600 if ($cat_id == -4 || $cat_id == -3) {
601 $result = db_query($link, "SELECT
602 id, feed_url, cat_id, title, order_id, ".
603 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
604 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
605 " ORDER BY cat_id, title " . $limit_qpart);
606 } else {
607
608 if ($cat_id)
609 $cat_qpart = "cat_id = '$cat_id'";
610 else
611 $cat_qpart = "cat_id IS NULL";
612
613 $result = db_query($link, "SELECT
614 id, feed_url, cat_id, title, order_id, ".
615 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
616 FROM ttrss_feeds WHERE
617 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
618 " ORDER BY cat_id, title " . $limit_qpart);
619 }
620
621 while ($line = db_fetch_assoc($result)) {
622
623 $unread = getFeedUnread($link, $line["id"]);
624
625 $has_icon = feed_has_icon($line['id']);
626
627 if ($unread || !$unread_only) {
628
629 $row = array(
630 "feed_url" => $line["feed_url"],
631 "title" => $line["title"],
632 "id" => (int)$line["id"],
633 "unread" => (int)$unread,
634 "has_icon" => $has_icon,
635 "cat_id" => (int)$line["cat_id"],
ef3da31c 636 "last_updated" => (int) strtotime($line["last_updated"]),
04f60eb7
AD
637 "order_id" => (int) $line["order_id"],
638 );
639
640 array_push($feeds, $row);
641 }
642 }
643
644 return $feeds;
645 }
646
647 static function api_get_headlines($link, $feed_id, $limit, $offset,
648 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
649 $include_attachments, $since_id,
ca5d9be4 650 $search = "", $search_mode = "",
04f60eb7
AD
651 $include_nested = false, $sanitize_content = true) {
652
653 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
ca5d9be4 654 $view_mode, $is_cat, $search, $search_mode,
04f60eb7
AD
655 $order, $offset, 0, false, $since_id, $include_nested);
656
657 $result = $qfh_ret[0];
658 $feed_title = $qfh_ret[1];
659
660 $headlines = array();
661
662 while ($line = db_fetch_assoc($result)) {
663 $is_updated = ($line["last_read"] == "" &&
664 ($line["unread"] != "t" && $line["unread"] != "1"));
665
666 $tags = explode(",", $line["tag_cache"]);
667 $labels = json_decode($line["label_cache"], true);
668
669 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
670 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
671
672 $headline_row = array(
673 "id" => (int)$line["id"],
674 "unread" => sql_bool_to_bool($line["unread"]),
675 "marked" => sql_bool_to_bool($line["marked"]),
676 "published" => sql_bool_to_bool($line["published"]),
ef3da31c 677 "updated" => (int) strtotime($line["updated"]),
04f60eb7
AD
678 "is_updated" => $is_updated,
679 "title" => $line["title"],
680 "link" => $line["link"],
681 "feed_id" => $line["feed_id"],
682 "tags" => $tags,
683 );
684
685 if ($include_attachments)
686 $headline_row['attachments'] = get_article_enclosures($link,
687 $line['id']);
688
689 if ($show_excerpt) {
690 $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
691 $headline_row["excerpt"] = $excerpt;
692 }
693
694 if ($show_content) {
695
696 if ($line["cached_content"] != "") {
697 $line["content_preview"] =& $line["cached_content"];
698 }
699
700 if ($sanitize_content) {
701 $headline_row["content"] = sanitize($link,
bfd61d3f
AD
702 $line["content_preview"],
703 sql_bool_to_bool($line['hide_images']),
704 false, $line["site_url"]);
04f60eb7
AD
705 } else {
706 $headline_row["content"] = $line["content_preview"];
707 }
708 }
709
710 // unify label output to ease parsing
711 if ($labels["no-labels"] == 1) $labels = array();
712
713 $headline_row["labels"] = $labels;
714
715 $headline_row["feed_title"] = $line["feed_title"];
716
717 $headline_row["comments_count"] = (int)$line["num_comments"];
718 $headline_row["comments_link"] = $line["comments"];
719
720 $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
721
583dbc56
AD
722 $headline_row["author"] = $line["author"];
723
b6604c96
AD
724 global $pluginhost;
725 foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_API) as $p) {
0db61af8 726 $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
b6604c96
AD
727 }
728
04f60eb7
AD
729 array_push($headlines, $headline_row);
730 }
731
732 return $headlines;
733 }
734
efc6553d
AD
735 function unsubscribeFeed() {
736 $feed_id = (int) db_escape_string($this->link, $_REQUEST["feed_id"]);
737
738 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
739 id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
740
741 if (db_num_rows($result) != 0) {
742 Pref_Feeds::remove_feed($this->link, $feed_id, $_SESSION["uid"]);
743 print $this->wrap(self::STATUS_OK, array("status" => "OK"));
744 } else {
745 print $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
746 }
747 }
748
749 function subscribeToFeed() {
750 $feed_url = db_escape_string($this->link, $_REQUEST["feed_url"]);
751 $category_id = (int) db_escape_string($this->link, $_REQUEST["category_id"]);
752 $login = db_escape_string($this->link, $_REQUEST["login"]);
753 $password = db_escape_string($this->link, $_REQUEST["password"]);
754
755 if ($feed_url) {
756 $rc = subscribe_to_feed($this->link, $feed_url, $category_id,
757 $login, $password, false);
758
759 print $this->wrap(self::STATUS_OK, array("status" => $rc));
760 } else {
761 print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
762 }
763 }
764
0bb5833b 765 function getFeedTree() {
b3575bd8 766 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
66ca7c30 767
0bb5833b
AD
768 $pf = new Pref_Feeds($this->link, $_REQUEST);
769
770 $_REQUEST['mode'] = 2;
66ca7c30 771 $_REQUEST['force_show_empty'] = $include_empty;
0bb5833b
AD
772
773 if ($pf){
774 $data = $pf->makefeedtree();
775 print $this->wrap(self::STATUS_OK, array("categories" => $data));
776 } else {
777 print $this->wrap(self::STATUS_ERR, array("error" =>
778 'UNABLE_TO_INSTANTIATE_OBJECT'));
779 }
780
781 }
c0a08063 782
dc5a8a21 783 // only works for labels or uncategorized for the time being
c0a08063
AD
784 private function isCategoryEmpty($id) {
785
786 if ($id == -2) {
787 $result = db_query($this->link, "SELECT COUNT(*) AS count FROM ttrss_labels2
788 WHERE owner_uid = " . $_SESSION["uid"]);
789
790 return db_fetch_result($result, 0, "count") == 0;
791
dc5a8a21
AD
792 } else if ($id == 0) {
793 $result = db_query($this->link, "SELECT COUNT(*) AS count FROM ttrss_feeds
794 WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]);
795
796 return db_fetch_result($result, 0, "count") == 0;
797
c0a08063
AD
798 }
799
800 return false;
801 }
802
803
de8260cb
AD
804}
805
806?>