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