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